`

44、commons-beanutils

阅读更多

一、BeanUtils

 

  1. Sun公司的内省API过于繁琐,
  2. 所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API——BeanUtils
  3. BeanUtils提供对 Java反射和自省API的包装。
  4. 其主要目的是利用反射机制对JavaBean进行各种操作
  5. BeanUtils操作的JavaBean类必须声明为public
  6. 为了使用BeanUtils,需要导入commons-beanutils-1.8.3.jar和commons-logging-1.1.2.jar
  7. commons-beanutils下载地址:http://commons.apache.org/proper/commons-beanutils//download_beanutils.cgi
  8. commons-logging下载地址:http://commons.apache.org/proper/commons-logging//download_logging.cgi
  9. BeanUtils更强的功能是直接访问内嵌对象的属性,只要使用点号分隔。

二、示例

 

  • BeanUtils
  • get属性时返回的结果为字符串,set属性时可以接受任意类型的对象,通常是字符串
  • PropertyUtils
  • get属性时返回的结果为该属性本来的类型,set属性时只接受该属性本来的类型
//JavaBean类

import java.util.Date;

public class Person
{
	private String name;
	private int age;
	private Date birthday = new Date();
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	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;
	}
}

 

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilsDemo {

	public static void main(String[] args)throws Exception {
		Person p = new Person("李四",20);
		
		String propertyName = "age";
		
		//BeanUtils设置值时,值通常用字符串,也可以是任意数据类型的
		BeanUtils.setProperty(p, propertyName, "30");
		
		Object obj = BeanUtils.getProperty(p, propertyName);
		System.out.println(obj);//30
		System.out.println(obj.getClass());//class java.lang.String
		
		//PropertyUtils设置值时,值只能用属性的本来数据类型
		PropertyUtils.setProperty(p, propertyName, 5);
		
		obj = PropertyUtils.getProperty(p, propertyName);
		System.out.println(obj);//5
		System.out.println(obj.getClass().getName());//java.lang.Integer
		
		//BeanUtils设置内嵌对象的属性值
		BeanUtils.setProperty(p, "birthday.time", "111");
		
		obj = BeanUtils.getProperty(p, "birthday.time");
		System.out.println(obj);
	}
}

 

 

三、BeanUtils

 

 

public class BeanUtils
{
	//全是静态方法

	//Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.
	public static Object cloneBean(Object bean)
                        throws IllegalAccessException,
                               InstantiationException,
                               InvocationTargetException,
                               NoSuchMethodException{}

	//Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
	public static void copyProperties(Object dest,
                                  Object orig)
                           throws IllegalAccessException,
                                  InvocationTargetException{}

	public static String getProperty(Object bean,
                                 String name)
                          throws IllegalAccessException,
                                 InvocationTargetException,
                                 NoSuchMethodException{}

	public static void setProperty(Object bean,
                               String name,
                               Object value)
                        throws IllegalAccessException,
                               InvocationTargetException{}

	//Return the entire set of properties for which the specified bean provides a read method.
	public static Map describe(Object bean)
                    throws IllegalAccessException,
                           InvocationTargetException,
                           NoSuchMethodException{}

	//Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.
	public static void populate(Object bean,
                            Map properties)
                     throws IllegalAccessException,
                            InvocationTargetException{}
}

 

import java.util.*;

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsDemo {

	public static void main(String[] args)throws Exception {
		Person p = new Person("李四",20);
		
		Map map = BeanUtils.describe(p);
		printMap(map);
		
		BeanUtils.setProperty(map, "name", "王武");
		System.out.println(p.getName());
		printMap(map);
	}

	private static void printMap(Map map) {
		Iterator it = map.keySet().iterator();
		while(it.hasNext())
		{
			Object obj = it.next();
			System.out.println(obj+"="+map.get(obj));
		}
	}
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics