BeanUtils Exception 之 FastHashMap
来源:互联网 发布:美国人学编程难吗 编辑:程序博客网 时间:2024/06/11 13:03
这里仅仅是为了记录一件十分奇怪的事情,在使用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
.
希望大家引以为戒。虽然导包是个老生常谈的问题,但是还是应该注意一下吧在导包的时候注意一下。
0 0
- BeanUtils Exception 之 FastHashMap
- Exception: BeanUtils.populate, argument type mismatch
- BeanUtils之个人理解
- 【exception】java.lang.NoSuchMethodError: org.springframework.beans.Beans.BeanUtils
- 学习笔记之 BeanUtils组件
- javabean优化之BeanUtils组件
- beanutils
- BeanUtils
- BeanUtils
- BeanUtils
- beanutils
- beanutils
- BeanUtils
- BeanUtils
- BeanUtils
- BeanUtils
- Beanutils
- BeanUtils
- 关于iOS常用的26中公共方法,可copy的代码
- 常见的web服务器有哪些?
- iOS9 画中画 Picture in Picture
- 【学习笔录】Mysql多表删除语句
- 算法复习(1)-算法设计要求
- BeanUtils Exception 之 FastHashMap
- nginx代理服务器
- 我的Redis集群搭建之路
- Swift - 是时候学习Swift了
- 十分钟认识单例模式的多种姿势
- leetcode:Longest Increasing Subsequence
- 迷宫求解
- Spring整合Shiro做权限控制模块详细案例分析
- 14.9.4 COMPACT and REDUNDANT Row Formats