JavaBeean

来源:互联网 发布:java软件开发工作描述 编辑:程序博客网 时间:2024/06/15 18:56

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 {

   privateintnumber;

   privateStringname;

   public Point(intnumber, String name) {

       super();

       this.number= number;

       this.name= name;

    }

   publicint getNumber() {

      returnnumber;

    }

   publicvoid setNumber(intnumber) {

       this.number= number;

    }

   publicString getName() {

      returnname;

    }

   publicvoidsetName(String name) {

       this.name= name;

    }

}

 

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

 

public class IntroSpector  {

   publicstaticvoid main(String[] args)throws Exception {

       Point pt =newPoint(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  {

   publicstaticvoid main(String[] args)throws Exception {

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

       String propertyName2 ="name";

       String propertyName1 ="number";

      getProperty(pt, propertyName1);

      

       String newVal ="SB";

      setProperty(pt, propertyName2, newVal);

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

    }

 

   privatestaticvoid setProperty(Point pt, String propertyName2,

           String newVal)throwsIntrospectionException,

           IllegalAccessException, InvocationTargetException {

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

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

    }

 

   privatestaticvoid getProperty(Point pt, String propertyName1)

          throwsIntrospectionException, 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等类

原创粉丝点击