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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zfgrinm 中级黑马   /  2015-3-5 23:44  /  1138 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package zhangTech;

  2. import java.beans.IntrospectionException;
  3. import java.beans.PropertyDescriptor;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;

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

  8. /*
  9. *  定义一个标准的JavaBean,名叫Person,包含属性name、age。使用反射的方式创

  10. 建一个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进

  11. 行设置,不使用setAge方法直接使用反射方式对age赋值。
  12. */
  13. class Person
  14. {
  15.         public String name;
  16.         private int age;
  17.         public Person(String name, int age) {
  18.                 this.name = name;
  19.                 this.age = age;
  20.         }
  21.         public String getName() {
  22.                 return name;
  23.         }
  24.         public void setName(String name) {
  25.                 this.name = name;
  26.         }
  27.         public int getAge() {
  28.                 return age;
  29.         }
  30.         public void setAge(int age) {
  31.                 this.age = age;
  32.         }
  33.        
  34. }
  35. public class BeanDemo {

  36.         public static void main(String[] args) {
  37.                 // TODO Auto-generated method stub
  38. Class<Person> cls=Person.class;//获取字节码
  39.                
  40.                 String propertyName="name";
  41.                 String name="yu";
  42.                 String propertyAge="age";
  43.                 int age=22;
  44.                 Person p;               
  45.                
  46.                 try{
  47.                         Constructor<Person> con=cls.getConstructor(String.class,int.class);
  48.                         p=con.newInstance("zhang",20);
  49.                
  50.                         //调用方法设置年龄和姓名属性值
  51.                         mSetName( p, propertyName, name);
  52.                         mSetAge(p ,propertyAge,age );
  53.                         System.out.println(p.getName()+"--"+p.getAge());
  54.                        
  55.                         BeanUtils.setProperty(p, propertyName, name);//这里一用就报错啊,为啥?
  56.                         System.out.println(BeanUtils.getProperty(p, propertyName));//这列也是报错,求指导
  57.                 }catch(Exception e){
  58.                         e.printStackTrace();
  59.                 }       
  60.        
  61.         }
  62.         private static void mSetName(Person p ,String propertyName, String name
  63.                         ) throws IntrospectionException, IllegalAccessException,
  64.                         InvocationTargetException {
  65.                                 PropertyDescriptor pd=new PropertyDescriptor(propertyName, p.getClass());
  66.                                 Method methodSetName=pd.getWriteMethod();
  67.                                 methodSetName.invoke(p, name);
  68.         }
  69.         private static void mSetAge(Person p,String propertyAge,int age)
  70.                         throws IntrospectionException, IllegalAccessException,
  71.                         InvocationTargetException {
  72.                                 PropertyDescriptor pd=new PropertyDescriptor(propertyAge, p.getClass());
  73.                                 Method methodSetAge=pd.getWriteMethod();
  74.                                 methodSetAge.invoke(p, age);
  75.         }
  76. }
复制代码
为什么用BeanUtils就报错啊,搞不明白了下,面是错误信息:求指导啊
  1. yu--22
  2. java.lang.reflect.InvocationTargetException: Cannot set name
  3.         at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
  4.         at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
  5.         at zhangTech.BeanDemo.main(BeanDemo.java:61)
  6. Caused by: java.lang.NoSuchMethodException: Property 'name' has no setter method in class 'class zhangTech.Person'
  7.         at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2128)
  8.         at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
  9.         at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
  10.         at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
  11.         ... 2 more
复制代码



1312.png (13.92 KB, 下载次数: 7)

1312.png

1 个回复

倒序浏览
求指导啊,怎么都没人看:sleepy:
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马