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 ...
随机推荐
- Android View Attributes
ImageView android:adjustViewBounds Set this to true if you want the ImageView to adjust its bounds ...
- TypeScript 映射类型
typescript支持定义类型加入推导式后产生新的类型 属性不变 但会改变对象的使用方式 这个是类型Person中加入ReadOnly推导出的新类型 他的属性全部是只读的 这个是推导出部分属性 这是 ...
- C++ opencv高速样例学习——读图显示
1.关键函数 1. 读入图片 imread(图片或位置,显示格式)默觉得:IMREAD_COLOR 显示格式: IMREAD_UNCHANGED =-1 // 8bit, color or no ...
- mapper.xml文件
1. 概述 mybatis的真正强大在于它的映射语句.由于它的异常强大,映射器的XML文件就显得相对简单,如果拿它跟具有相同功能的JDBC代码进行对比,省掉将近95%的代码.mybatis是针对SQL ...
- Atitit.sql where条件表达式的原理 attilax概括
Atitit.sql where条件表达式的原理 attilax概括 1. 数据查询接口sql api标准化1 2. Sql接口的问题2 2.1. 虽然sql是结构化的dsl,但是任然是编程语言类型 ...
- [svc]caffe安装笔记-显卡购买
caffe,这是是数据组需要做一些大数据模型的训练(深度学习), 要求 服务器+显卡(运算卡), 刚开始老板让买的牌子是泰坦的(这是2年前的事情了). 后来买不到这个牌子的,(jd,tb)看过丽台的, ...
- 1.文件I/O
一. open()&close() #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h& ...
- 【flink training】 打车热点区域实时统计PopularPlaces
http://training.data-artisans.com/是Apache Flink商业公司DataArtisans提供的一个flink学习平台,主要提供了一些业务场景和flink api结 ...
- 380. Intersection of Two Linked Lists【medium】
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- python学习之join()
str.join(iterable) 该方法用来分隔字符串的. 例子 >>> b':'.join((b'leo',b'999')) b'leo:999' >>> ' ...