使用BeanUtils操作Bean的各種屬性

     內省是 Java 語言對 Bean 類屬性、事件的一種處理方法(也就是說給定一個javabean對象,我們就可以得到/調用它的所有的get/set方法)。
    例如類 A 中有屬性 name, 那我們可以通過 getName,setName 來得到其值或者設置新的值。通過 getName/setName 來訪問 name 屬性,這就是默認的規則。
    Java 中提供了一套 API 用來訪問某個屬性的 getter/setter 方法,通過這些 API 可以使你不需要了解這個規則,這些 API 存放於包 java.beans 中。
    一般的做法是通過類 Introspector 的 getBeanInfo方法 來獲取某個對象的 BeanInfo 信息,然後通過 BeanInfo 來獲取屬性的描述器(PropertyDescriptor),通過這個屬性描述器就可以獲取某個屬性對應的 getter/setter 方法,然後我們就可以通過反射機制來調用這些方法。  

首先建立一個bean

package com.fts.beanutils;

import java.util.Date;

public class Person {
	private String name;
	private int passwd;
	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 void setName(String name) {
		this.name = name;
	}
	public int getPasswd() {
		return passwd;
	}
	public void setPasswd(int passwd) {
		this.passwd = passwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}
使用beanutils操作bean

package com.fts.beanutils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
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, "name", "fengtansong");
	    System.out.println(p.getName());
	}

	@Test
	public void test2() throws Exception{
	    String name="fts";
	    String passwd="888888";
	    String age="100";
	    Person p=new Person();
	    //數據轉換隻支持八種基本數據類型
	    BeanUtils.setProperty(p, "name", name);
	    BeanUtils.setProperty(p, "passwd", passwd);
	    BeanUtils.setProperty(p, "age", age);
	    System.out.println(p.getName());
	    System.out.println(p.getPasswd());
	    System.out.println(p.getAge());
		
	}
	

	@Test
	public void test3() throws Exception{
	    String name="fts";
	    String passwd="888888";
	    String age="100";
	    String birthday="1992-02-28";
	    Person p=new Person();
	    //ConvertUtils.register(new DateLocaleConverter(),Date.class);
	    ConvertUtils.register(new Converter(){

			public Object convert(Class type, Object value) {
				// TODO Auto-generated method stub
				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");
				try {
					return df.parse(str);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					throw new RuntimeException(e);
				}
				
			}
	    	
	    }, Date.class);
	    
	    
	    //數據轉換隻支持八種基本數據類型
	    BeanUtils.setProperty(p, "name", name);
	    BeanUtils.setProperty(p, "passwd", passwd);
	    BeanUtils.setProperty(p, "age", age);
	    BeanUtils.setProperty(p, "birthday", birthday);
	    
	    System.out.println(p.getName());
	    System.out.println(p.getPasswd());
	    System.out.println(p.getAge());
	    System.out.println(p.getBirthday());
		
	}
	
	@Test
	public void test4() throws Exception{
		Map map=new HashMap();
		map.put("name","kkk");
		map.put("passwd","666666");
		map.put("age","100");
		map.put("birthday", "1992-02-28");
		Person bean=new Person();
		ConvertUtils.register(new DateLocaleConverter(),Date.class);
		BeanUtils.populate(bean, map);		
		System.out.println(bean.getName());
	    System.out.println(bean.getPasswd());
	    System.out.println(bean.getAge());
	    System.out.println(bean.getBirthday());
	}
	
	
}
用junit測試的結果如下

fengtansong
fts
888888
100
fts
888888
100
Fri Feb 28 00:00:00 GMT+08:00 1992
kkk
666666
100
Fri Feb 28 00:00:00 GMT+08:00 1992



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章