利用BeanUtils工具类给JavaBean设置和取值

import java.util.Date;

public class Point {

	private int x;
	
	private Date birthday = new Date();

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}
	
}
 
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanutilTest {
	public static void main(String[] args) throws Exception{
		Point p = new Point();
		
		BeanUtils.setProperty(p, "x", 7);
		System.out.println(BeanUtils.getProperty(p, "x"));
		
		BeanUtils.setProperty(p, "birthday.time", "2011101");
		System.out.println(BeanUtils.getProperty(p, "birthday.time"));
		
		PropertyUtils.setProperty(p, "x", 9);
		System.out.println(PropertyUtils.getProperty(p, "x"));
		
	}

}
 

你可能感兴趣的:(apache)