BeanUtils用法大全--应用反射技术

package com.beanutils;

import java.util.*;

public class Person ...{
private String name;
private String sex;
private int age;
private Address address;
private List goods;
private Map contact;

public Person() ...{
}


public Person(String name) ...{
 
this.name = name;
}


public Person(String name,String sex) ...{
 
this.name = name;
 
this.sex = sex;
}


public String getName() ...{
 
return this.name;
}


public void setName(String name) ...{
 
this.name = name;
}


public String getSex() ...{
 
return this.sex;
}


public void setSex(String sex) ...{
 
this.sex = sex;
}


public int getAge() ...{
 
return this.age;
}


public void setAge(int age) ...{
 
this.age = age;
}


public Address getAddress() ...{
 
return this.address;
}


public void setAddress(Address address) ...{
 
this.address = address;
}


public List getGoods() ...{
 
return this.goods;
}


public void setGoods(List goods) ...{
 
this.goods = goods;
}


public Map getContact() ...{
 
return this.contact;
}


public void setContact(Map contact) ...{
 
this.contact = contact;
}


public void sayHello() ...{
  System.out.println(
"Hello World!");
}


public void f(String str) ...{
  System.out.println(
"Person.f()..." + str);
}


public String toString() ...{
 
return "Person.toString()...";
}

}

=========================================================================================

package com.beanutils;

public class Address ...{
private String province;
private String city;
private String street;
private String zipcode;

public String getProvince() ...{
 
return this.province;
}


public void setProvince(String province) ...{
 
this.province = province;
}


public String getCity() ...{
 
return this.city;
}


public void setCity(String city) ...{
 
this.city = city;
}


public String getStreet() ...{
 
return this.street;
}


public void setStreet(String street) ...{
 
this.street = street;
}


public String getZipcode() ...{
 
return this.zipcode;
}


public void setZipcode(String zipcode) ...{
 
this.zipcode = zipcode;
}

}

============================================================================

一个测试类

package com.beanutils;

import java.util.*;
import org.apache.commons.beanutils.*;

public class BeanUtilsTest ...{
public static void main(String[] args) throws Exception ...{
     Address address
= new Address();
     address.setProvince(
"hunan");
     address.setCity(
"changsha");
     address.setStreet(
"wuyilu");
     address.setZipcode(
"410000");
    
     List goods
= new ArrayList();
     goods.add(
"sporting");
     goods.add(
"singing");
     goods.add(
"dancing");
    
     Map contact
= new HashMap();
     contact.put(
"student","Tom");
     contact.put(
"teacher","Lucy");
    
     Person person
= new Person();
     person.setName(
"Mike");
     person.setSex(
"male");
     person.setAge(
25);
     person.setAddress(address);
     person.setGoods(goods);
     person.setContact(contact);
    
    
//BeanUtilsTest.cloneBean(person);
    
//BeanUtilsTest.copyProperties(person);
    
//BeanUtilsTest.propertyUtilsCopyProperties(person);
    
//BeanUtilsTest.copyProperty(person);
    
//BeanUtilsTest.describe(person);
    
//BeanUtilsTest.propertyUtilsDescribe(person);
    
//BeanUtilsTest.getArrayProperty(person);
    
//BeanUtilsTest.getIndexedProperty(person);
    
//BeanUtilsTest.getMappedProperty(person);
    
//BeanUtilsTest.getNestedProperty(person);
    
//BeanUtilsTest.populate(person);
    
    
//得到一个bean中指定属性的值,相当于getter
//     System.out.println(BeanUtils.getProperty(person,"name"));
//     //修改一个bean中指定属性的值,相当于setter
//     BeanUtils.setProperty(person,"name","Tom");
//     System.out.println(BeanUtils.getProperty(person,"name"));
//     System.out.println("--------------------------------------");
//    
     BeanUtilsTest.invokeMethod();
//     System.out.println("--------------------------------------");
//     BeanUtilsTest.getAccessibleMethod();
//     System.out.println("--------------------------------------");
//     BeanUtilsTest.invokeConstructor();
//     System.out.println("--------------------------------------");
     BeanUtilsTest.getAccessibleConstructor();
    
    }

   
   
//BeanUtils.cloneBean()--克隆目标bean
    public static void cloneBean(Person person) throws Exception ...{
     Person p
= (Person)BeanUtils.cloneBean(person);
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     p.getAddress().setCity(
"hengyang");
     System.out.println(
"---------------------------");
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     System.out.println(p.getAddress()
== person.getAddress());
    }

   
   
//BeanUtils.copyProperties()--拷贝目标bean中属性的值,能进行类型转换
    public static void copyProperties(Person person) throws Exception ...{
     Person p
= new Person();
     BeanUtils.copyProperties(p,person);
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     p.getAddress().setCity(
"hengyang");
     System.out.println(
"---------------------------");
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     System.out.println(p.getAddress()
== person.getAddress());
    }

   
   
//PropertyUtils.copyProperties()--拷贝目标bean中属性的值,不能进行类型转换
    public static void propertyUtilsCopyProperties(Person person) throws Exception ...{
     Person p
= new Person();
     PropertyUtils.copyProperties(p,person);
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     p.getAddress().setCity(
"hengyang");
     System.out.println(
"---------------------------");
     System.out.println(p.getAddress().getCity());
     System.out.println(person.getAddress().getCity());
     System.out.println(p.getAddress()
== person.getAddress());
    }

   
   
//BeanUtils.copyProperty()--拷贝一个值给目标Bean的一个属性,能进行类型转换
    public static void copyProperty(Person person) throws Exception ...{
     Person p
= new Person();
     BeanUtils.copyProperty(p,
"age","12");
     System.out.println(p.getAge());
    }

   
   
//BeanUtils.describe()--得到一个bean的所有描述,返回的是一个map
    public static void describe(Person person) throws Exception ...{
     Map map
= BeanUtils.describe(person);
     Set set
= map.keySet();
    
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
      Object o
= it.next();
      System.out.println(o
+ ": " + map.get(o));
     }

     System.out.println(
"--------------------------------------");
//     Collection c = map.values();
//     for(Object o : c) {
//      System.out.println(o);
//     }
    }

   
   
   
//PropertyUtils.describe()--得到一个bean的所有描述,返回的是一个map
    public static void propertyUtilsDescribe(Person person) throws Exception ...{
     Map map
= PropertyUtils.describe(person);
     Set set
= map.keySet();
    
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
      Object o
= it.next();
      System.out.println(o
+ ": " + map.get(o));
     }

     System.out.println(
"--------------------------------------");
//     Collection c = map.values();
//     for(Object o : c) {
//      System.out.println(o);
//     }
//     System.out.println("----------------------------");
    }

   
   
//BeanUtils.populate()--使用一个map为bean赋值
    public static void populate(Person person) throws Exception ...{
     Map map
= PropertyUtils.describe(person);
     Person p
= new Person();
     BeanUtils.populate(p,map);
     System.out.println(p.getName());
     System.out.println(p.getSex());
     System.out.println(p.getAge());
     System.out.println(p.getAddress().getProvince());
     System.out.println(p.getGoods().get(
1));
     System.out.println(p.getContact().get(
"student"));
     System.out.println(
"--------------------------------------");
    }

   
   
//BeanUtils.getArrayProperty()--将目标bean的一个属性的值转换成一个字符串数组
    public static void getArrayProperty(Person person) throws Exception ...{
     String[] array
= BeanUtils.getArrayProperty(person,"goods");
    
for(String str : array) ...{
      System.out.println(str);
     }

    }

   
   
//BeanUtils.getIndexedProperty()--可以检索目标bean中数组或集合类型的属性指定索引的值
    public static void getIndexedProperty(Person person) throws Exception ...{
     System.out.println(BeanUtils.getIndexedProperty(person,
"goods[0]"));
     System.out.println(BeanUtils.getIndexedProperty(person,
"goods",1));
    }

   
   
//BeanUtils.getMappedProperty()--可以检索目标bean中具有map类型的属性中对应键的值
    public static void getMappedProperty(Person person) throws Exception ...{
     System.out.println(BeanUtils.getMappedProperty(person,
"contact(student)"));
     System.out.println(BeanUtils.getMappedProperty(person,
"contact","teacher"));
    }

   
   
//BeanUtils.getNestedProperty()--得到目标bean中嵌套属性的值
    public static void getNestedProperty(Person person) throws Exception ...{
     System.out.println(BeanUtils.getNestedProperty(person,
"address.province"));
    }

   
   
//MethodUtils.invokeMethod()--动态调用方法
    public static void invokeMethod() throws Exception ...{
    
//MethodUtils.invokeMethod(Object arg1,String arg2,Object[] arg3)
    
//arg1 一个bean的名称
    
//arg2 指定bean的方法名
    
//arg3 方法中的参数列表
     MethodUtils.invokeMethod(new Person(),"sayHello",new Object[]...{});
    
//MethodUtils.invokeMethod(Object arg1,String arg2,Object arg3)
    
//arg1 一个bean的名称
    
//arg2 指定bean的方法名
    
//arg3 方法中的参数
     MethodUtils.invokeExactMethod(new Person(),"f","hello");;
    }

   
   
//MethodUtils.getAccessibleMethod()--得到方法
    public static void getAccessibleMethod() throws Exception ...{
    
//MethodUtils.getAccessibleMethod(Class clazz,String methodName,Class[] parameterTypes)
    
//clazz 一个bean的类对象
    
//methodName bean中的方法名
    
//parameterTypes 方法中参数的类型列表
     System.out.println(MethodUtils.getAccessibleMethod(Person.class,"f",new Class[]...{String.class}));
     System.out.println(MethodUtils.getAccessibleMethod(Person.
class,"f",String.class));
    }

   
   
//ConstructorUtils.invokeConstructor()
    public static void invokeConstructor() throws Exception ...{
    
//ConstructorUtils.invokeConstructor(Class clazz,Object[] args)
    
//clazz 一个bean的类对象
    
//args bean的构造函数的参数列表
     Person p1 = (Person)ConstructorUtils.invokeConstructor(Person.class,new Object[]...{});
     Person p2
= (Person)ConstructorUtils.invokeConstructor(Person.class,"Tom");
     System.out.println(p2.getName());
    }

   
   
//ConstructorUtils.getAccessibleConstructor()
    public static void getAccessibleConstructor() throws Exception ...{
    
//ConstructorUtils.getAccessibleConstructor(Class clazz,Class[] parameterTypes)
    
//clazz 一个bean的类对象
    
//parameterTypes bean的构造函数的参数类型列表
     System.out.println(ConstructorUtils.getAccessibleConstructor(Person.class,String.class));
     System.out.println(ConstructorUtils.getAccessibleConstructor(Person.
class,new Class[]...{String.class,String.class}));
    }

}

===========================================================================

package com.beanutils;

import java.util.*;
import org.apache.commons.beanutils.*;

public class BasicDynaBeanTest ...{
public static void main(String[] args) throws Exception ...{
 
//创建一个动态属性数组
     DynaProperty[] properties = new DynaProperty[] ...{
      
new DynaProperty("name",String.class),
      
new DynaProperty("sex",String.class),
      
new DynaProperty("age",int.class),
      
new DynaProperty("address",Map.class)
     }
;
     
    
//创建一个BasicDynaClass的实例
    
//第一个参数设置动态bean的名称
    
//第二个参数设置BasicDynaBean的类对象
    
//第三个参数设置一个动态属性数组
     BasicDynaClass dynaClass = new BasicDynaClass("customer",BasicDynaBean.class,properties);
    
//通过dynaClass得到动态bean的一个实例
     DynaBean customer = dynaClass.newInstance();
    
    
//给属性设值
     Map map = new HashMap();
     map.put(
"province","hunan");
     map.put(
"city","changsha");
     map.put(
"street","wuyilu");
     map.put(
"zipcode","410000");
     customer.set(
"name","Tom");
     customer.set(
"sex","male");
     customer.set(
"age",25);
     customer.set(
"address",map);
    
     System.out.println(
"name: " + customer.get("name"));
     System.out.println(
"sex: " + customer.get("sex"));
     System.out.println(
"age: " + customer.get("age"));
     System.out.println(
"address: " + ((Map)customer.get("address")).get("province")
    
+ " " + ((Map)customer.get("address")).get("city")
    
+ " " + ((Map)customer.get("address")).get("street")
    
+ " " + ((Map)customer.get("address")).get("zipcode"));
    }

}

========================================================================

package com.beanutils;

import org.apache.commons.beanutils.*;

public class ConvertUtilsTest ...{
public static void main(String[] args) ...{
  Person person
= new Person();
 
//将任意的实例转变为String,用法非常简单相当与调用toString()方法
  System.out.println(ConvertUtils.convert(person));
 
  String str
="123456";
  String ary
= "45789";
 
//int ss = Integer.parseInt(str);
  Integer s =(Integer)ConvertUtils.convert(str,Integer.class);
 
  String[] values
= ...{"123","456","789","aafs"};
  Object o
= ConvertUtils.convert(values,Integer.class);
  System.out.println(o);
    }

}

================================================================================

package com.beanutils;

import java.util.*;
import org.apache.commons.beanutils.*;

public class LazyDynaBeanTest ...{
public static void main(String[] args) ...{
 
//创建一个LazyDynaBean的实例,它就和普通的bean一样
     LazyDynaBean customer = new LazyDynaBean();
     customer.set(
"name","Tom");
     customer.set(
"sex","male");
     customer.set(
"age",new Integer(25));
    
//以map方式给属性赋值,第一个参数是属性名,第二个参数是键,第三个参数是值
     customer.set("address","province","hunan");
    
//如果属性是一个数组或集合,可以以索引方式赋值
    
//第一个参数是数组名或集合名,第二个参数是索引,第三个参数是数组和集合中存的值
     customer.set("orders",0,"order001");
     customer.set(
"orders",1,"order002");
    
     System.out.println(
"name: " + customer.get("name"));
     System.out.println(
"sex: " + customer.get("sex"));
     System.out.println(
"age: " + customer.get("age"));
     System.out.println(
"address: " + ((Map)customer.get("address")).get("province"));
     System.out.println(
"orders: " + customer.get("orders",0) + " " + customer.get("orders",1));
    }
posted @ 2014-01-24 21:18  dlzh  阅读(350)  评论(0编辑  收藏  举报