java 内省的使用

来源:互联网 发布:网络cn是什么意思啊 编辑:程序博客网 时间:2024/06/15 09:41

Apache组织开发了一套用于操作JavaBeanAPI,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。

在工程下新建lib目录,导入commons-beanutils-1.8.3.jar和支持包commons-logging-1.1.1.jar

选中两个包,右键build path/addto build path

Beanutils工具包的常用类:

BeanUtils

PropertyUtils

ConvertUtils.regsiter(Converter convert,Class clazz)

自定义转换器

package cn.itcast.beanutils;

 

import java.util.Date;

 

public class Person {

         private  String name;

         private  String password;

         private  int age;

         private  Date birthday;

         public  Date getBirthday() {

                   return  birthday;

         }

         public  void setBirthday(Date birthday) {

                   this.birthday  = birthday;

         }

         public  String getName() {

                   return  name;

         }

         public  String getPassword() {

                   return  password;

         }

         public  int getAge() {

                   return  age;

         }

         public  void setName(String name) {

                   this.name  = name;

         }

         public  void setPassword(String password) {

                   this.password  = password;

         }

         public  void setAge(int age) {

                   this.age  = age;

         }

 

}

package cn.itcast.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.Locale;

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;

 

//使用beanUtils操纵bean的属性 (第三方)

public class Demo1 {

         @Test

         public  void test1() throws Exception{

                   Person  p=new Person();

                   BeanUtils.setProperty(p,  "age", 456);

                   System.out.println(p.getAge());//456

         }

         @Test

         public  void test2() throws Exception{

                   String  name="aaaa";

                   String  age="123";

                   String  password="pw";

                                    

                   Person  p=new Person();

                   //支持8种基本类型自动转换

                   BeanUtils.setProperty(p,  "name", name);

                   BeanUtils.setProperty(p,  "age", age);

                   BeanUtils.setProperty(p,  "password", password);

                  

                   System.out.println(p.getName());//aaaa

                   System.out.println(p.getAge());//123

                   System.out.println(p.getPassword());//pw

         }

         @Test

         public  void test3() throws Exception{

                   String  birthday="1983-12-1";          

                   //为了让日期赋值到beanbirthday属性上,给beanUtils注册一个日期转换器

                   //ConvertUtils.register(converter,  clazz);

                   ConvertUtils.register(new  Converter(){                   

                            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  df=new SimpleDateFormat("yyyy-MM-dd",Locale.US);

                                     try  {

                                               return  df.parse(str);

                                     }  catch (ParseException e) {

                                               throw  new RuntimeException(e);                     

                                     }

                            }

                   },  Date.class);

                   Person  p=new Person();

                   BeanUtils.setProperty(p,  "birthday", birthday);

                   System.out.println(p.getBirthday());//pw

                   System.out.println("___"+BeanUtils.getProperty(p,  "birthday"));

         }

         public  void test5() throws Exception {

                   Map  map=new HashMap();

                   map.put("name",  "aaa");

                   map.put("password",  "123");

                   map.put("brithday",  "1980-09-09");

                   ConvertUtils.register(new  DateLocaleConverter(), Date.class);

                   Person  p=new Person();

         //map集合填充bean属性,map关键字和bean属性要一致

                   BeanUtils.populate(p,  map);

         }

}