JavaBeean

First:由内省引出JavaBean的讲解

1、  什么是JavaBean?

JavaBean是一种特殊的java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。

2、  如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JAVABean中,这种JavaBean的实例对象通常称之为值对象(VO,Value Object)。

3、  使用JavaBean的好处?

①        大家都在用!

②        JDK中提供了对JavaBean进行操作的一些API,这套API称为内省。

 

Second:对JavaBean的简单内省操作

主要用到了PropertyDescriptor类。

例子:

public class Point {

   private intnumber;

   private Stringname;

   public Point(int number, String name) {

       super();

       this.number= number;

       this.name= name;

    }

   public int getNumber() {

      return number;

    }

   public void setNumber(intnumber) {

       this.number= number;

    }

   public String getName() {

      return name;

    }

   public voidsetName(String name) {

       this.name= name;

    }

}

 

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

 

public class IntroSpector  {

   public staticvoid main(String[] args)throws Exception {

       Point pt =new Point(52751,"kuangcheng");

       String propertyName2 ="name";

       String propertyName1 ="number";

       PropertyDescriptor pd1 =new PropertyDescriptor(propertyName1, Point.class);

       System.out.printf("d\n",pd1.getReadMethod().invoke(pt)); //该方法返回Object,即打印对象

      

       PropertyDescriptor pd2 =new PropertyDescriptor(propertyName2, Point.class);

       pd2.getWriteMethod().invoke(pt,"SB");

       System.out.println(pt.getName());

    }

}

想一想:对于第二段代码,该如何优化?怎样在ecplise中快速优化?

public class IntroSpector  {

   public staticvoid main(String[] args)throws Exception {

       Point pt =new Point(52751,"kuangcheng");

       String propertyName2 ="name";

       String propertyName1 ="number";

      getProperty(pt, propertyName1);

      

       String newVal ="SB";

      setProperty(pt, propertyName2, newVal);

       System.out.println(pt.getName());

    }

 

   private staticvoid setProperty(Point pt, String propertyName2,

           String newVal)throws IntrospectionException,

           IllegalAccessException, InvocationTargetException {

       PropertyDescriptor pd2 =new PropertyDescriptor(propertyName2, Point.class);

       pd2.getWriteMethod().invoke(pt, newVal);

    }

 

   private staticvoid getProperty(Point pt, String propertyName1)

          throws IntrospectionException, IllegalAccessException,

           InvocationTargetException {

       PropertyDescriptor pd1 =new PropertyDescriptor(propertyName1, Point.class);

       System.out.printf("d\n",pd1.getReadMethod().invoke(pt)); //该方法返回Object,即打印对象

    }

}

 

Third:复杂例子:

采用遍历BeanInfo的所有属性方式来查找和设置某个RefectPoint对象的x属性。

在程序中把一个类当做JavaBean来看,就是调用IntroSpector.getBeanInfo方法,得到的BeanInfo对象封装了把这个类当做JavaBean看的结果信息。

 

Four:使用BeanUtils工具包操作JavaBean

1、  怎么在Ecplise中增加新的架?

①     增加到整个工作间(或ecplise)

②     增加到整个工程( 新建fold,将架包放入此,然后再增加到buildpath即可)

2、  使用该架。(需要另外再加载logging包)

例如:

System.out.println(BeanUtils.getProperty(pt,"name"));

BeanUtils.setProperty(pt,"number", "52751");

还有PropertyUtils等类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值