使用beanUtils控制javabean
使用BeanUtils设置/读取属性的值以及默认支持的自动转化:
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Test
public void test1() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{ Person p=new Person(); BeanUtils.setProperty(p, "username", "李四"); System.out.println(BeanUtils.getProperty(p, "username");); BeanUtils.setProperty(p,"age", "123"); System.out.println(p.getAge()); }
|
注册已有的转化器来完成复杂类型的自动转化:
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Test
public void test3() throws IllegalAccessException, InvocationTargetException{ Person p=new Person(); String birthday="1995-05-05"; ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getBirthday()); }
|
Apache已有的时间转化器中不能很好地过滤空字符串,若待转换字符串为空则会抛出异常;而现实业务非常复杂,Apache无法提供给我们所有的类型转化方法,需要时我们可以注册自己需要的转换器完成业务需求。
注册自己的转换器完成时间转化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @Test
public void test2() throws IllegalAccessException, InvocationTargetException{ Person p=new Person(); String birthday="1995-05-05"; ConvertUtils.register(new Converter(){ @SuppressWarnings({ "unchecked", "rawtypes" }) public Object convert(Class type,Object value){ if(value==null){ return null; } if(!(value instanceof String)){ throw new ConversionException("只支持String类型的转换"); } String str=(String) value; if(str.trim().equals("")){ return null; } SimpleDateFormat dateformate=new SimpleDateFormat("yyyy-MM-dd"); try { return dateformate.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } }, Date.class); BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getBirthday()); }
|
直接使用map对象填充类:
```java
@Test
//直接使用map对象填充类
public void test4() throws Exception{
HashMap<String, String> map=new HashMap<String,String>();
map.put(“username”,”李四”);
map.put(“password”,”lisi”);
map.put(“age”,”26”);
map.put(“birthday”,”1990-05-05”);
ConvertUtils.register(new DateLocaleConverter() , Date.class);
Person p=new Person();
BeanUtils.populate(p, map);
System.out.println(p.getUsername());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}