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寻找之路,如下。
然后还是失败了!
然后继续运行,发现,仍然很尴尬的失败了。
然后就去找吧,最后竟然在更低版本的BeanUtils包里面给找到了。真的是很尴尬的一件事,然后就能正常的运行了。3.2版本的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.
希望大家引以为戒。虽然导包是个老生常谈的问题,但是还是应该注意一下吧在导包的时候注意一下。
相关文章推荐
- 【转】抛弃EF,20分构建一个属于自己的ORM框架
- <c:url>标签相关知识点
- JAVASE总结笔记
- c/c++错误及解决方法
- c 语言的 While 没有花括号的用法
- 使用POI为生成的excel添加文件属性
- bean的加载(二) FactoryBean的使用
- jenkins 自动送信的实现
- 【leetcode】20. Valid Parentheses
- uefi与win8 (根据网络资料整理)
- 网站访问分析中常见的指标
- SCons的使用
- Windows下新建Cocos2d-x V3.10项目(cocos2d-x lua)
- saltstack配置安装和windowns应用
- eclipse 安卓项目导入library失败
- Linux内核源码分析--内核启动
- C#利用正则判断输入是否为纯数字、容器类
- Linux 下 strace 命令用法总结
- CodeForces 559C Gerald and Giant Chess
- 自动拆/装箱