您的位置:首页 > 其它

BeanUtils Exception 之 FastHashMap

2016-07-05 10:35 211 查看
这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了,

common logging

common collections

···

在为boolean 这种基本的数据类型添加Property的时候,然而还是出现了:

java.lang.ClassNotFoundException: org.apache.commons.collections.FastHashMap
然后我就开始了Bug寻找之路,如下。

第一步

一开始我以为是转换类没有做好,然后尝试着在其帮助文档中搜索相关的转换类。

BooleanConverter
: 看起来貌似是我想要的,于是在

BeanUtils.setProperty(bean,name,value)
之前注册了一个

ConvertUtils.register(new BooleanConverter(),java.lang.Boolean.class)


然后还是失败了!

第二步

既然自带的转换器不能正常的工作,那我就尝试着写一个自定义的转换器吧。按照官网上的步骤,需要:

ConvertUtils.register(new Converter(){

@Override
public Boolean  convert(Class type, Object value) {
return (Boolean)value;
}
}, java.lang.Boolean.class);


然后继续运行,发现,仍然很尴尬的失败了。

第三步

根据异常提示,是由于

java.lang.ClassNotFoundException:      org.apache.commons.collections.FastHashMap


然后就去找吧,最后竟然在更低版本的BeanUtils包里面给找到了。真的是很尴尬的一件事,然后就能正常的运行了。3.2版本的BeanUtils包下载地址

我的测试代码如下:

package beanutils;

import java.util.Date;

public class MyBean {

private String name;
private int age;
private Date birthday;
private boolean isAlive;

public MyBean() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public boolean isAlive() {
return isAlive;
}

public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}

}


package beanutils;

import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class TestMyBean {

/**
* 首先测试的是基本的数据类型
*
* @throws Exception
*/
@Test
public void test1() throws Exception {
String name = "Mark";
int age = 20;
boolean isAlive = true;

MyBean myBean = new MyBean();
BeanUtils.setProperty(myBean, "name", name);
BeanUtils.setProperty(myBean, "age", age);
// 对于boolean貌似不能进行转换
// BeanUtils.setProperty(myBean, "isAlive", isAlive);
System.out.println("Print the result by BeanUtils");
System.out.println("Name:" + myBean.getName());
System.out.println("Age:" + myBean.getAge());
System.out.println("IsAlive:" + myBean.isAlive());
}

/**
* 对Date进行转换测试
*
* @throws Exception
*/
@Test
public void test2() throws Exception {
String date = "2016-07-05";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
MyBean myBean = new MyBean();
BeanUtils.setProperty(myBean, "birthday", date);
System.out.println("Birthday:" + myBean.getBirthday());

}

/**
* 测试boolean类型的数据
*
* @throws Exception
*/
@Test
public void test3() throws Exception {

boolean isalive = true;
ConvertUtils.register(new BooleanConverter(), Boolean.class);
MyBean myBean = new MyBean();
BeanUtils.setProperty(myBean, "isAlive", isalive);
System.out.println("IsAlive:" + myBean.isAlive());
}
}


最后一点

在测试Date这个数据类型的时候买也是出现了一点错误。调试了很久也没能找到错误,最后发现时导包的时候导错了。因为Date应该导
java.util.Date
而不是
java.sql.Date
.

希望大家引以为戒。虽然导包是个老生常谈的问题,但是还是应该注意一下吧在导包的时候注意一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: