您的位置:首页 > 编程语言 > Java开发

java的javabean的初步理解

2014-07-21 11:22 232 查看
1.首先说下什么是JavaBean?有什么特征?

(1)符合特定规则的类

(2)JavaBean分二类:

1>侠义的JavaBean

.私有的字段(Field)

.对私有字段提供存取方法(读写方法)

2>广义的JavaBean

.私有的字段(Field)

.对私有字段提供存取方法(读写方法)

.数量任意的业务方法

2.内省API(SUN公司开发)


(1)直接调用bean的setXXX或getXXX方法。

通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式。

(2)通过PropertyDescriptor类操作Bean的属性

(3)通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描
述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的
getter/setter 方法,然后通过反射机制来调用这些方法。


3.jdk中的两个重要类PropertyDescriptor和Introspector的介绍(jdk1.6)

(1)PropertyDescriptor 描述 Java Bean 通过一对存储器方法导出的一个属性。

(2)Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法。

对于这三种信息,Introspector 将分别分析 bean 的类和超类,寻找显式或隐式信息,使用这些信息构建一个全面描述目标 bean 的 BeanInfo 对象。

对于每个 "Foo" 类,如果存在相应的 "FooBeanInfo" 类,显式信息可能是可用的,查询这些信息时,FooBeanInfo 类会提供一个非 null 值。通过获得目标 bean 类的完全受限定包名称并追加 "BeanInfo" 形成一个新类,首先查找 BeanInfo 类。如果此操作失败,则采用此名称的最终类名称组件,在 BeanInfo 包搜索路径中指定的每个包中搜索该类。

因此对于某个类,比如说 "sun.xyz.OurButton",首先需要查找称为 "sun.xyz.OurButtonBeanInfo" 的 BeanInfo 类,如果失败,则查找 BeanInfo 搜索路径中的每个包来搜索 OurButtonBeanInfo 类。对于默认搜索路径,这意味着将查找 "sun.beans.infos.OurButtonBeanInfo"。

如果某个类提供有关其自身的显式 BeanInfo,则将它添加到从分析所有派生类得到的 BeanInfo 信息中,并将显式信息视为当前类及其基类的确定的信息,无需进一步深入超类链进行分析。

如果没有在某个类上发现显式 BeanInfo,则使用低层次的反射来研究类的方法,并应用标准设计模式来标识属性存储器、事件源或公共方法。然后深入分析类的超类,从它那里(可能在超类链的顶部)添加信息。

因为 Introspector 缓存 BeanInfo 类来获得更好的性能,所以如果在使用多个类加载器的应用程序中使用它,则需小心谨慎。通常,在破坏一个已用于 introspect 类的
ClassLoader
时,应使用
Introspector.flushCaches

Introspector.flushFromCaches
方法从缓存中清除所有内省的类。

4.代码练习

JavaBean类(Student.java):

package cn.wwh.www.java.introspector;

/**

*类的作用:JavaBean

*

*

*@author 一叶扁舟

*@version 1.0

*@创建时间: 2014-7-21 上午10:04:03

*/

public class Student {

private String name;

private int age;

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the age

*/

public int getAge() {

return age;

}

/**

* @param age the age to set

*/

public void setAge(int age) {

this.age = age;

}

}

测试代码(Demo1.java):

package cn.wwh.www.java.introspector;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

import org.junit.Test;

/**

*类的作用:主要是用来jdk中提供的方法,练习javabean

*

*

*@author 一叶扁舟

*@version 1.0

*@创建时间: 2014-7-21 上午10:04:15

*/

public class Demo1 {

@Test

public void testProperty() throws Exception {

// 通过反射得到对象

Class clazz = Class.forName("cn.wwh.www.java.introspector.Student");

Constructor con = clazz.getConstructor(null);

Student student = (Student) con.newInstance(null);

PropertyDescriptor property = new PropertyDescriptor("name",

Student.class);

property.setName("一叶扁舟");

String name = property.getName();

System.out.println("name:" + name);

// 下面的两步相当于-->student.setName("无悔");

Method method = property.getWriteMethod();

method.invoke(student, "无悔");

// 下面的两步相当于-->student.getName();

Method me = property.getReadMethod();

String nam = (String) me.invoke(student, null);

System.out.println("名字为:" + nam);

// ----------------------------

property = new PropertyDescriptor("age",Student.class);

property.setName("21");

System.out.println("age:--->"+property.getName());

}

@Test

public void testIntrospector() throws Exception{

System.out.println("<----------------------------->testIntrospector()");

BeanInfo beanInfo =
Introspector.getBeanInfo(Student.class);

// 返回一个属性描述的集合

PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor propertyDescriptor : pds) {

System.out.println("propertyDescriptor:"+propertyDescriptor.getName());

}

}

}

代码运行效果图:



5.注意:(1)原本Student中的属性只有两个,即name和age,但是输出的结果却是:
age,class,name,三个属性,这主要是因为,java中所有类的父类都是Object,其中Object
中的隐含着一个属性class,所以输出时也将这个隐含属性也输出来了。

(2)如果将Student中的属性全部删除,并且去掉set方法,调用testIntrospector()方法
得到的仍然是age,name,class三个属性,即对javaBean操作时,属性和getXxxxx()有
关,同时必须有返回值。(实际代码的测试结果)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: