什么时候会用到beanutil?

解决方案 »

  1.   

    是Common BeanUtils吗?如果是的话,就是在你 用javaBean的时候呀!
      

  2.   

    However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed. Example use cases include:
    Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework).
    Building template language processors for web presentation and similar uses (such as JSP or Velocity).
    Building custom tag libraries for JSP and XSP environments (such as Jakarta Taglibs, Struts, Cocoon).
    Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat's server.xml file).
      

  3.   

    操作Bean的工具,其实现原理就是采用了反射,在servlet处理请求参数,处理ResultSet数据中经常用到,在不用MVC框架下可以很大的优化代码用MVC框架下,还需要用beanutil吗?
      

  4.   

    BeanUtils主要提供了对于JavaBean进行各种操作。它里面提供的方法都是静态的,直接通过类名.方法名就使用。它具有以下功能:1、  将Map转成Bean对象,它可以为通过Map给对象实例。  @Test
     public void testMap2Bean() throws IllegalAccessException, InvocationTargetException{
      Map<String,String> map = new HashMap<String,String>();
      map.put("name","wsh");
      map.put("address", "河南郑州");
      Person person = new Person();
      BeanUtils.populate(person, map);
      System.out.println(person.getAddress()+ "   " + person.getName());
     }2、  对象的克隆。 @Test
     public void testClone() throws IllegalAccessException, InstantiationException,   InvocationTargetException, NoSuchMethodException{
      Person person = new Person();
      person.setAddress("湖南湘潭");
      person.setName("wsh");
      
      Person person2 = (Person) BeanUtils.cloneBean(person);
      System.out.println(person2.getAddress()+ "   " + person2.getName());
      System.out.println(person.equals(person2));
     }3、  属性动态的getter和setter@Test
     public void testGetSet() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
      Person person = new Person();
      person.setAddress("湖南湘潭");
      person.setName("wsh");
      String name = BeanUtils.getProperty(person,"name");
      System.out.println(name);
      
      BeanUtils.setProperty(person, "address", "火星");
      System.out.println(person.getAddress());
     }4、  动态读取Collection,Map
      

  5.   

    JavaBean之间如果属性名称相同,用BeanUtils传递值很方便。
    public class BeanSource {
    private String name;
    private int age;
    private String sex; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String getSex() {
    return sex;
    } public void setSex(String sex) {
    this.sex = sex;
    }}
    public class BeanDest {
    private String name;
    private int age;
    private String sex;
    private String education;
    private String experience; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String getSex() {
    return sex;
    } public void setSex(String sex) {
    this.sex = sex;
    } public String getEducation() {
    return education;
    } public void setEducation(String education) {
    this.education = education;
    } public String getExperience() {
    return experience;
    } public void setExperience(String experience) {
    this.experience = experience;
    }}
    import org.apache.commons.beanutils.BeanMap;public class TestII { public static void main(String[] args) { BeanDest dest = new BeanDest();
    BeanSource source = new BeanSource();
    source.setAge(24);
    source.setName("tomcat");
    source.setSex("female"); BeanMap beanMap = new BeanMap(dest);
    beanMap.putAllWriteable(new BeanMap(source));
    System.out.println(dest.getAge() + ":" + dest.getName() + ":" + dest.getSex());
    }}