</pre><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">定义:java语言对bean类属性,时间的一种缺省的处理方法。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">正常处理方式:类A中有属性name,那么我们可以通过getName/setName来得到name的值或设置新值,通过 getName/setName来访问name属<span style="white-space:pre"> </span>性,这是默认规则。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">内省处理方式:java中提供了一套API用来访问某个属性的getter/setter方法,通过这些API可以使你不需要了解这 些规则,这些API存放在包java.bean<span style="white-space:pre"> </span>中,一般的做法是通过类Introspector来获取某个对象的BeanInfo信息, 通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属<span style="white-space:pre"> </span>性描述器就可以获取某个属性对应的 getter/setter方法,然后我们就可以通过反射来调用这些方法</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">PropertyDescriptor类:表示javaBean类通过存储器导出一个属性,主要方法:</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>1、getPropertyType(),获得属性的Class对象。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>2、getReadMethod(),获得用于读取属性值得方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>3、getWriteMethod(),获得用于写入属性值得方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>4、hashCode(),获取对象的哈希值;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>5、setReadMethod(Method readMethod),设置读取属性值的方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre"> </span>6、setWriteMethod(Method writeMethod),设置用于写入属性值的方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;">实例代码如下:</span></div><pre name="code" class="java"><pre name="code" class="java">package test.com;
class UserInfo {
private String username;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}
public class PropertyDescriptorDemo {
public static void main(String[] args) {
UserInfo userInfo = new UserInfo();
setProperty(userInfo, "username");
getProperty(userInfo, "username");
}
public static void setProperty(User user, String username) {
PropertyDescriptor pd = new PropertyDescriptor(username, User.class);
Method method = pd.getWriteMethod();
method.invoke(user, "fanjunkai");
System.out.println("username ==:" + user.getUsername());
}
public static void getProperty(User user, String username) {
PropertyDescriptor pd = new PropertyDescriptor(username, User.class);
Method method = pd.getReadMethod();
Object obj = method.invoke(user);
System.out.println(obj.toString());
}
}
<div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">Introspector类:</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">1、将JavaBean中的属性封装起来进行操作,程序一个类当做JavaBean来看,就是调用 Introspector.getBeanInfo()方法,BeanInfo对象把这个类当做JavaBean来看。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">2、getPropertyDescriptors()获得属性的描述,可以采用遍历BeanInfo的方法,来查找、设置类的属性。</div>
public static void setPropertyByIntrospector(UserInfo userInfo, String username) throws Exception {
BeanInfo beaninfo = Introspector.getBeanInfo(UserInfo.class);
PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();
if (pds != null && pds.length > 0) {
for (PropertyDescriptor pd : pds) {
if (pd.getName().equals(username)) {
Method method = pd.getWriteMethod();
method.invoke(userinfo, "fanjunkai");
}
}
}
}
public static void getPropertyByIntrospector(UserInfo userinfo, String username) throws Exception {
BeanInfo beaninfo = Introspector.getBeanInfo(UserInfo.class);
PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();
if (pds != null && pds.length) {
for (PropertyDescriptor pd : pds) {
if (pd.getName().equals(username)) {
Method method = pd.getReadMethod();
Object obj = method.invoke(username);
System.out.println(obj.toString());
}
}
}
}