A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯晓骏 中级黑马   /  2013-12-6 15:44  /  1057 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.util.Date;

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

  8. /**
  9. * @author Shawn
  10. *
  11. */
  12. class Heart{
  13.         private String left;
  14.         private String right;
  15.         public String getLeft() {
  16.                 return left;
  17.         }
  18.         public void setLeft(String left) {
  19.                 this.left = left;
  20.         }
  21.         public String getRight() {
  22.                 return right;
  23.         }
  24.         public void setRight(String right) {
  25.                 this.right = right;
  26.         }
  27.        
  28. }
  29. class Person{
  30.         private Heart heart;
  31.         private Date birthday;
  32.         public Date getBirthday() {
  33.                 return birthday;
  34.         }
  35.         public void setBirthday(Date birthday) {
  36.                 this.birthday = birthday;
  37.         }
  38.         public Heart getHeart() {
  39.                 return heart;
  40.         }
  41.         public void setHeart(Heart heart) {
  42.                 this.heart = heart;
  43.         }
  44. }
  45. public class BeanUtilsTest2 {

  46.         /**
  47.          * @param args
  48.          * @throws NoSuchMethodException
  49.          * @throws InvocationTargetException
  50.          * @throws IllegalAccessException
  51.          */
  52.         public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  53.                 // TODO Auto-generated method stub
  54.                 Person p = new Person();
  55.                 BeanInfo beanInfo = null;
  56.                 try {
  57.                         beanInfo = Introspector.getBeanInfo(Person.class);
  58.                 } catch (IntrospectionException e) {
  59.                         // TODO Auto-generated catch block
  60.                         e.printStackTrace();
  61.                 }
  62.                 PropertyDescriptor[] pds = null;
  63.                 try {
  64.                         pds = beanInfo.getPropertyDescriptors();
  65.                 } catch (Exception e) {
  66.                         // TODO Auto-generated catch block
  67.                         e.printStackTrace();
  68.                 }
  69.                 for(PropertyDescriptor pd : pds){
  70.                         System.out.println(pd.getName());
  71.                 }
  72. //                System.out.println(BeanUtils.getProperty(p, "heart"));
  73.         }

  74. }
复制代码
打印结果:
birthday
class
heart


我这里实现了人和心脏的类,然后想通过BeanUtils的级联操作,设置heart属性,但是发现会报错,测试了一下,用内省打印了一下获取到的属性名,发现多了一个class,谁知道这个class哪里来的?

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

6 个回复

倒序浏览
static BeanInfo getBeanInfo(Class<?> beanClass)
          在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件。
    Person是个类,类也是他的属性,你这种方法会过滤掉Object继承的方法,不过既然是javabean继承的方法也没用
回复 使用道具 举报
王家胜 发表于 2013-12-6 16:27
static BeanInfo getBeanInfo(Class beanClass)
          在 Java Bean 上进行内省,了解其所有属性、公 ...

不是很明白你的意思,我觉得问题出在PropertyDescriptor上吧,属性描述符为什么会多出一个叫做class的东西?
回复 使用道具 举报
冯晓骏 发表于 2013-12-6 18:23
不是很明白你的意思,我觉得问题出在PropertyDescriptor上吧,属性描述符为什么会多出一个叫做class的东 ...

我想我知道了,是Object里有一个属性class,代表的是类的字节码文件对象是吧?那我最后注释的那一句,用BeanUtils包的操作为啥没有成功,获取不到heart的属性方法
回复 使用道具 举报
冯晓骏 发表于 2013-12-6 18:26
我想我知道了,是Object里有一个属性class,代表的是类的字节码文件对象是吧?那我最后注释的那一句,用B ...

好吧,搞明白了,把Person和Heart类都放在一个单独java文件中,用public声明一下就行了,也就是说,JavaBean要求类的访问权限是public的,至少我的测试是这样的
回复 使用道具 举报
  1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.MethodDescriptor;
  5. import java.beans.PropertyDescriptor;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.util.Date;

  8. /**
  9. * @author Shawn
  10. *
  11. */
  12. class Heart
  13. {
  14.         private String left;
  15.         private String right;

  16.         public String getLeft()
  17.         {
  18.                 return left;
  19.         }

  20.         public void setLeft(String left)
  21.         {
  22.                 this.left = left;
  23.         }

  24.         public String getRight()
  25.         {
  26.                 return right;
  27.         }

  28.         public void setRight(String right)
  29.         {
  30.                 this.right = right;
  31.         }

  32. }

  33. class Person
  34. {
  35.         private Heart heart;
  36.         private Date birthday;

  37.         public Date getBirthday()
  38.         {
  39.                 return birthday;
  40.         }

  41.         public void setBirthday(Date birthday)
  42.         {
  43.                 this.birthday = birthday;
  44.         }

  45.         public Heart getHeart()
  46.         {
  47.                 return heart;
  48.         }

  49.         public void setHeart(Heart heart)
  50.         {
  51.                 this.heart = heart;
  52.         }
  53. }

  54. public class BeanUtilsTest2
  55. {

  56.         /**
  57.          * @param args
  58.          * @throws NoSuchMethodException
  59.          * @throws InvocationTargetException
  60.          * @throws IllegalAccessException
  61.          */
  62.         public static void main(String[] args) throws IllegalAccessException,
  63.                         InvocationTargetException, NoSuchMethodException
  64.         {
  65.                 // TODO Auto-generated method stub
  66.                 Person p = new Person();
  67.                 BeanInfo beanInfo = null;
  68.                 try
  69.                 {
  70.                         beanInfo = Introspector.getBeanInfo(Heart.class);
  71.                 } catch (IntrospectionException e)
  72.                 {
  73.                         // TODO Auto-generated catch block
  74.                         e.printStackTrace();
  75.                 }
  76.                 PropertyDescriptor[] pds = null;
  77.                 try
  78.                 {
  79.                         pds = beanInfo.getPropertyDescriptors();
  80.                 } catch (Exception e)
  81.                 {
  82.                         // TODO Auto-generated catch block
  83.                         e.printStackTrace();
  84.                 }
  85.                 for (PropertyDescriptor pd : pds)
  86.                 {
  87.                         System.out.println(pd.getName());
  88.                 }
  89.                 System.out.println("-----------------------------------------");
  90.                 MethodDescriptor[] md=null;
  91.                 try
  92.                 {
  93.                         md=beanInfo.getMethodDescriptors();
  94.                 }
  95.                 catch(Exception ex)
  96.                 {
  97.                        
  98.                 }
  99.                 for(int i=0;i<md.length;i++)
  100.                 {
  101.                         MethodDescriptor mm=md[i];
  102.                         System.out.println(mm.getName());
  103.                 }
  104.                 // System.out.println(BeanUtils.getProperty(p, "heart"));
  105.         }

  106. }
复制代码

就没有了,你那是打印属性
我这是打方法,方法含父类方法,但没有class属性
如果还不懂结合程序看上面

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
对类有class属性
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马