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 ...
随机推荐
- centos7防火墙的关闭
从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig. #systemctl list-unit-files|grep firewalld.servic ...
- 转:微软分布式云计算框架Orleans
http://www.cnblogs.com/ants/p/5122068.html 一种构建分布式. 高规模(伸缩)的应用程序 微软对奥尔良计划(Project Orleans)云计算框架开源.奥尔 ...
- laravel的模板嵌套循环可以这样调试
@foreach($first as $value) {{ dump($value) }} @endforeach
- python学习之with...as语句
python中的with...as...语句类似于try...finally...语句: # -*- coding: utf-8 -*- # """ with...as. ...
- Apache伪静态Rewrite详解
一.Rewrite规则简介:Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于 Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要 ...
- Unity 扩展编辑器
扩展Inspector界面 继承自Editor,添加CustomEditorAttribute,传入定制的类型 定制显示的类型要求: 类型中所有的public 字段都会自动暴露给Inspector编辑 ...
- linux学习笔记12--命令less
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- easy ui 验证框的使用
Easy ui 插件之validatebox missingMessage:未填写时显示的信息validType:验证类型见下示例invalidMessage:无效的数据类型时显示的信息require ...
- landa语法
sg_orm看当前sql信息 db.IsEnableLogEvent = true; db.LogEventStarting = (sql, pa) => { var t = 0; }; //出 ...
- c#检查网络文件是否存在
public bool IsExist(string uri) { HttpWebRequest req = null; HttpWebResponse res = null; try { req = ...