俺要跳槽啦 后天面试Android 哦也 这几天java基础过一遍 明天android基础过一遍 哦也 后天期待成功
BeanUtils工具包下载地址:http://commons.apache.org/proper/commons-beanutils/ BeanUtils中还引用了一个包logging下载地址:
http://commons.apache.org/proper/commons-logging/
实例如下
package com.java.xiong.beanUtils0302;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
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 Test1 {
@Test
public void test1() throws IllegalAccessException,
InvocationTargetException, IllegalArgumentException,
SecurityException, InstantiationException, NoSuchMethodException,
ClassNotFoundException {
String name = "xiong";
String age = "24";
String brday = "1990-09-09";
Person p = (Person) (Class.forName(
"com.java.xiong.beanUtils0302.Person").getConstructor()
.newInstance());
// 因为BeanUtils只支持8种基本类型 其余的类型需要注册
ConvertUtils.register(new Converter() {
@Override
public
T convert(Class
type, Object value) { if (value == null) { return null; } if (!(value instanceof String)) { throw new ConversionException("只支持String类型"); } if (value.toString().trim().equals("")) { return null; } try { Date date = null; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); date = df.parse(value.toString()); return (T) date; } catch (ParseException e) { throw new RuntimeException(e); } } }, Date.class); // 调用setProperty为beal赋值 BeanUtils.setProperty(p, "name", name); BeanUtils.setProperty(p, "age", age); BeanUtils.setProperty(p, "brday", brday); System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getBrday()); } @Test public void test2() throws Exception { Class
cl = Class.forName("com.java.xiong.beanUtils0302.Person"); Constructor
con = cl.getConstructor(); // 获取Person对象 Person p = (Person) con.newInstance(); String name = "xiong"; String age = "24"; String brday = "1990-09-09"; ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.setProperty(p, "name", name); BeanUtils.setProperty(p, "age", age); BeanUtils.setProperty(p, "brday", brday); System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(p.getBrday()); } @Test public void test3() throws Exception { Map
map = new LinkedHashMap
(); map.put("name", "xiong"); map.put("age", "24"); map.put("brday", "1990-09-09"); Person p = (Person) (Class.forName( "com.java.xiong.beanUtils0302.Person").getConstructor() .newInstance()); ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.populate(p, map); System.out.println(p.getName()); System.out.println(p.getAge()); System.out.println(DateFormat.getDateInstance().format(p.getBrday())); } }