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 ...
随机推荐
- C# 使用TASK处理多任务同时处理
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#调用Windows CMD命令并,返回输出结果或错误信息
public static string InvokeExcute(string Command) { Command = Command.Trim().TrimEnd('&') + &quo ...
- ExtJS学习------Ext.define的继承extend,用javascript实现相似Ext的继承
(1)Ext.define的继承extend 详细实例: Ext.onReady(function(){ //Sup Class 父类 Ext.define('Person',{ config:{ n ...
- Varnish 简介
Varnish是高性能开源的反向代理服务器和HTTP缓存服务器 Varnish的功能与Squid服务器相似,都可以用来做HTTP缓存 Squid是从硬盘读取缓存的数据,而Varnish把数据存放在内存 ...
- atitit.无为而治在企业管理,国家治理,教育领域的具体思想与实践
atitit.无为而治在企业管理,国家治理,教育领域的具体思想与实践 1. 什么是无为而治 1 2. 无为而治的三个原则 1 3. 抓大放小 1 4. 治理国家 2 5. 企业管理 2 6. 教育领域 ...
- [elk]es增删改查最佳实战
PUT app01 GET app01/_settings GET _all/_settings PUT app01/_settings { "number_of_replicas" ...
- 将json数组字符串转换为json数组对象(值是json对象的数组)
var str1 ='[{"name":"kevin","age":18},{"name":"rose&quo ...
- flink checkpoint 源码分析 (二)
转发请注明原创地址http://www.cnblogs.com/dongxiao-yang/p/8260370.html flink checkpoint 源码分析 (一)一文主要讲述了在JobMan ...
- Linux编程之判断磁盘空间大小
一.引言 在开发过程中,经常会碰到这样的情况,在往指定目录下拷贝文件时,需要考虑到磁盘空间的大小是否足够来决定什么时候暂停自己的程序 二.用的函数 <sys/statfs.h> int s ...
- Github上Fork代码,及源码修改
iOS开发中经常遇到这种情况,你使用的第三方库不能完全满足自己项目需要,只能修改源码来解决. 我们以前的解决办法是,添加到项目中直接修改源码.这样就有一个问题,不能和源库同步,当作者更新后你不能(po ...