160704、commons-beanutils.jar常用方法
package com.test.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class TestBeanUtils {
/**BeanUtils.setProperty设置属性*/
@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
Person person = new Person();
BeanUtils.setProperty(person, "age", 23);
System.out.println(person.getAge());
}
/**BeanUtils.cloneBean克隆一个bean*/
@Test
public void test2() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException{
Person person = new Person("lucy", 18, "xxxxx@qq.com");
Person cloneBean = (Person) BeanUtils.cloneBean(person);
System.out.println(cloneBean.toString());
}
/**注册转换器*/
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
String str = (String) value;
if(null == value){
return null;
}
if(!(value instanceof String)){
System.out.println("格式不正确");
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parse = sdf.parse(str);
return parse;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}, Date.class);
Person person = new Person();
BeanUtils.setProperty(person, "birthday", "2016-07-01");
System.out.println(person.getBirthday());
}
/**将map转换成bean并注册转换器*/
@Test
public void test4() throws IllegalAccessException, InvocationTargetException{
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "test");
map.put("birthday", "2016-07-01");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person person = new Person();
BeanUtils.populate(person, map);
System.out.println(person.getBirthday());
}
/**转换器*/
@Test
public void test5(){
Integer num = (Integer) ConvertUtils.convert("123", Integer.class);
System.out.println(num);
}
}
160704、commons-beanutils.jar常用方法的更多相关文章
- Beanutils工具常用方法
BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品.其主要目的是利用反射机制对JavaBean的属性进行处理. BeanUtils工具一般可以方便ja ...
- myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...
- 再续前缘-apache.commons.beanutils的补充
title: 再续前缘-apache.commons.beanutils的补充 toc: true date: 2016-05-32 02:29:32 categories: 实在技巧 tags: 插 ...
- 报错处理 java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean
java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean at org.apache.catalina.loade ...
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
- Apache Commons Beanutils对象属性批量复制(pseudo-singleton)
Apache Commons Beanutils为开源软件,可在Apache官网http://commons.apache.org/proper/commons-beanutils/download_ ...
- Commons Beanutils使用setProperty() - 就是爱Java
有时不能只依靠getter/setter操作bean,如:需要名字动态取得的,或是访问bean内的field,甚至是集合或数组内bean的field,利用反射机制对bean的field进行处理,这时候 ...
- org.apache.commons.beanutils.BeanMap简单使用例子
一.org.apache.commons.beanutils.BeanMap; 将一个java bean允许通过map的api进行调用, 几个支持的操作接口: Object get(Object ke ...
随机推荐
- 折叠表格思路及遇到的问题(tableView:viewForHeaderInSection:的section从1开始,不是从0开始)
项目需要做了一个类似qq联系人的折叠表格,思路很简单:设置每个section的header,在header上显示组名等信息,然后根据折叠与否,设置每个section中cell的数量,如果折叠,则将之设 ...
- ASDASASD
测试 markdown 随笔 asdsdf sdf
- [转]PHP运行出现Notice : Use of undefined constant 的完美解决方案
Notice: Use of undefined constant title - assumed 'title' in F:\wamp\www\load_myweb.php on line 22No ...
- Application Architecture Determines Application Performance
 Application Architecture Determines Application Performance Randy Stafford AppliCATion ARCHiTECTuR ...
- codility上的问题(34) Fluorum 2014
好久没写codility的题了.一来没时间,二来有的题目不太好分析.这个题比較有意思,我还没有给出很严格的证明.
- yum中查找程序由哪个包提供
有时候知道程序的名称,却不知道由那个包提供,也就是说不知道安装那个包,可以使用这个命令. 我们由provides关键字可以使用. 举例:semanage是SELinux的一个管理工具,可是我使用:yu ...
- socket failed:EACCES(Permission denied)
1. 权限问题 安卓端写的TCP协议软件报错原因是建立的套接字没有限权对外连接. 在AndroidManifest.xml中,加上这一句话,取得权限. <uses-permission andr ...
- 通过ip查看主机名和端口占用情况
1. 知道对方ip查看对方的计算机名 方法:开始->运行->cmd->net view 对方ip 或者 开始->运行->cmd->nbtstat -a 对方ip ...
- cocos2d-JS (二)Cocos Creater
我觉得我们开发使用最新的工具.这对于我们非常有帮助. 由于新的工具有着新的功能,简化了我们开发的模式. 站在巨人的肩膀上~~~~~~~~~~~~~ CocosCreator.最大的两个特点 组件化 ...
- oozie搭建手册
环境准备 mave安装 1.下载并上传maven,然后解压 tar -zxvf apache-maven-3.3.9-bin.tar.gz -C /home 2.配置环境变量 vi /etc/prof ...