JavaBean类必须有一个没有参数的构造函数。
JavaBean类中定义函数setXxx() 和getXxx()来对属性进行操作。其中Xxx是首字母大写的私有变量名称。
以上 转自:http://blog.csdn.net/zdwzzu2006/article/details/5151788
好 ,下面 是自己的学习总结
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class IntroSpectorTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3, 5);
String propertyName = "x";
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodGetX = pd.getReadMethod();// 得到 X 属性的读方法。
Object retVal = methodGetX.invoke(pt1);
System.out.println(retVal);
Method methodSetX = pd.getWriteMethod();// 得到 X 属性的读方法。
methodSetX.invoke(pt1,9);
System.out.println(pt1.getX());
}
}
package com.itm.day1;
import java.util.Date;
public class ReflectPoint {
private Date birthday = new Date();
private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString(){
return str1 + ":" + str2 + ":" + str3;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
结果如下:
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IntroSpectorTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3, 5);
String propertyName = "x";
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodSetX = pd2.getWriteMethod();// 得到 X 属性的读方法。
methodSetX.invoke(pt1,9);
System.out.println(pt1.getX());
}
private static Object getProperty(ReflectPoint pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodGetX = pd.getReadMethod();// 得到 X 属性的读方法。
Object retVal = methodGetX.invoke(pt1);
return retVal;
}
}
定义为 Object value = 9.
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IntroSpectorTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3, 5);
String propertyName = "x";
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
Object value = 9;
setProperty(pt1, propertyName, value);
System.out.println(pt1.getX());
}
private static void setProperty(ReflectPoint pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodSetX = pd2.getWriteMethod();// 得到 X 属性的读方法。
methodSetX.invoke(pt1,value);
}
private static Object getProperty(ReflectPoint pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodGetX = pd.getReadMethod();// 得到 X 属性的读方法。
Object retVal = methodGetX.invoke(pt1);
return retVal;
}
}
// 利用 eclipse 的 重构方法。牛逼了。
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
/*PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodGetX = pd.getReadMethod();// 得到 X 属性的读方法。
Object retVal = methodGetX.invoke(pt1);*/
// 复杂的方式:
BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
Object retVal = null;
for(PropertyDescriptor pd : pds){
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();// 得到 X 属性的读方法。
retVal = methodGetX.invoke(pt1);
break;
}
}
return retVal;
}
BeanUtils,以及java7的新特性。
package com.itm.day1;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class CopyOfIntroSpectorTest2 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3, 5);
BeanUtils.setProperty(pt1, "x", "10");
System.out.println(pt1.getX());
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());
System.out.println("-------------------------------------");
/*
//java7的新特性
Map map = {name:"zxx",age:18};
BeanUtils.setProperty(map, "name", "lhm");
*/
// birthday.time
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
}
}
下面是:变量本身的类型,而 BeanUtils 是可以类型转化的,这个不会。
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println("PropertyUtils :" + PropertyUtils.getProperty(pt1, "x").getClass().getName());
运行结果:
PropertyUtils :java.lang.Integer
更多BeanUtils详解:http://xubindehao.iteye.com/blog/754807