java基础增强---JavaBean的内省操作

来源:互联网 发布:淘宝数据收集 编辑:程序博客网 时间:2024/06/05 09:26

  ——- android培训、java培训、期待与您交流! ———-

JavaBean
一、 由内省引出JavaBean
1、内省(IntroSpector),主要用于对JavaBean进行操作。
2、JavaBean的概念
JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于
访问私有的字段,且方法名称符合某种命名规则。
如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,
这种JavaBean的实例对象通常称之为值对象(Value Object,就是值对象,简称VO)。
这些信息在类中用私有字段来存储,如果读取或者设置这些字段的值,
则需要通过一些相应的方法来访问,大家觉得这些方法的名称叫什么好呢?
JavaBean的属性是根据其中的setter和getter方法来确定的,而不是根据其中的成员变量。
如果方法名为setId,就认为是设置id,至于把它存到哪个变量上,不用管,
如果方法名为getId,就认为是获取Id,至于是从哪个变量上取的,不用管。
简单说就是在一个Java类中,某些方法名的前面都是set或者get开头,
像符合这种特定的规则的java类就称为JavaBean。

class Person{    peivate int x;    public int getAge(){        return x;    }    public void estAge(int age){        this.x = age;       }}

如果把Person类看作是JavaBean,那么它有一个名称为age的属性,不能说是x。
因为根本看不到x,我们设置的是age这个属性,不能说是设置x。
3、JavaBean的属性命名规则
比如用上面的例子来说
1)先把set或get去掉后得到Age
2)把Age变为小写,但有前提条件
如果Age的第二个字母是小写,那么首字母就改为小写,反之大写
比如:
方法名 属性名
gettime –> time
getTime –> time
getCPU –> CPU
总之JavaBean的属性名是根据方法名称推断出来的,它根本看不到java类内部的成员变量。
4、JavaBean的好处
一个符合JavaBean特点的类可以当作普通类一样进行使用,那么JavaBean有什么好处?
1)在Java EE开发中,都经常用带JavaBean,很多环境都要求用JavaBean来操作。
2)JDK中提供对JavaBean操作的一些API,这套API就爱称为内省,用它来操作JavaBean就很方便了。
二、 对JavaBean的简单内省操作
PropertyDescriptor 类
是描述Java Bean 通过一对存储器方法导出的一个属性。
构造方法
PropertyDescriptor(String propertyName, Class

package zxx.enhance;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;class IntroSpaector {    private int x;    private int y;    IntroSpaector(int x, int y){        this.x = x;        this.y = y;    }    //为这两变量生产set和get方法    //方法1:右键-->Source-->Generate Getter and Setter    //方法2:快捷键Alt+Shift+S-->R    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }}//把上面那个类当做JavaBean进行操作public class D30_IntroSpaectorTest {    public static void main(String[] args) throws Exception {        //创建对象        IntroSpaector is = new IntroSpaector(3,5);        String propertyName = "x";        //获得JavaBean的属性        Object retVal = getProperty(is, propertyName);        System.out.println(retVal);        //设置JavaBean属性        Object value = 7;        setProperties(is, propertyName, value);        System.out.println(retVal);    }    //获得JavaBean属性    private static Object getProperty(Object is, String propertyName) throws Exception {        PropertyDescriptor pd = new PropertyDescriptor(propertyName,is.getClass());        Method methodGetX = pd.getReadMethod();        //并不知道返回的类型是上面,所以用Object        Object retVal = methodGetX.invoke(is);        return retVal;    }    //设置JavaBean属性    private static void setProperties(Object is, String propertyName, Object value) throws Exception{        PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,is.getClass());        Method methodSetX = pd1.getWriteMethod();        methodSetX.invoke(is,value);    }}/*结果33*/

三、对JavaBean的复杂内省操作
不建议使用这种复杂的操作。
1、概述
采用遍历BeanInfo的所有属性方式来查找和设置某个RefectPoint对象的x属性,
在程序中把一个类当做JavaBean来看,就是调用IntroSpector.getBeanInfo方法,
得到的BeanInfo对象封装了把这个类当做JavaBean看的结果信息。
2、Introspector 类
该类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法。
对于这三种信息,Introspector 将分别分析 bean 的类和超类,寻找显式或隐式信息,使用这些
信息构建一个全面描述目标 bean 的 BeanInfo 对象。
该类没有构造方法,都是静态方法
方法:
static BeanInfo getBeanInfo(Class

package zxx.enhance;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;class ReflectPoint {    private int x;    private int y;    ReflectPoint(int x, int y){        this.x = x;        this.y = y;    }    //为这两变量生产set和get方法    //方法1:右键-->Source-->Generate Getter and Setter    //方法2:快捷键Alt+Shift+S-->R    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }}//把上面那个类当做JavaBean进行操作public class D31_IntroSpaectorTest2 {    public static void main(String[] args) throws Exception {        //创建对象        ReflectPoint rp = new ReflectPoint(3,5);        String propertyName = "x";        //获得JavaBean的属性         Object retVal = getProperty(rp, propertyName);        System.out.println(retVal);        //设置JavaBean属性        Object value = 7;        setProperties(rp, propertyName, value);        System.out.println(retVal);    }    //设置JavaBean属性,内部使用的方法就私有修饰    private static void setProperties(Object is, String propertyName, Object value) throws Exception{        PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,is.getClass());        Method methodSetX = pd1.getWriteMethod();        methodSetX.invoke(is,value);    }    //获得JavaBean属性    private static Object getProperty(Object rp, String propertyName) throws Exception {        /*        //对JavaBean进行简单内省操作        PropertyDescriptor pd = new PropertyDescriptor(propertyName,is.getClass());        Method methodGetX = pd.getReadMethod();        //并不知道返回的类型是上面,所以用Object        Object retVal = methodGetX.invoke(is);        */        //对JavaBean进行复杂内省操作--实际开发中当然不建议用这种复杂方法。        //在JavaBean上进行内省,了解其所有属性、公开的方法和事件。返回一个BeanInfo对象        BeanInfo beaninfo = Introspector.getBeanInfo(rp.getClass());        //获得所有属性,返回一个属性数组        PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();        Object retVal = null;        //遍历该属性数组        for(PropertyDescriptor pd : pds){            //如果获得的属性名和传进来的属性名相同,说明就是要操作的属性。            if(pd.getName().equals(propertyName)){                //通过属性pd读取它的值后就跳出循环。                Method methodGetX = pd.getReadMethod();                retVal = methodGetX.invoke(rp);                break;            }        }        return retVal;//返回得到的属性值    }}/*结果33*/

四、使用BeanUtils工具包操作JavaBean
利用Java反射API,方便对属性的getter和setter Java对象操作。
方法
static Object getProperty(Object bean, String name)
Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.
返回指定属性的值指定的bean
static void setProperty(Object bean, String name, Object value)
Set the specified property value, performing type conversions as required to conform to the type of the destination property.
设置指定的属性值

package zxx.enhance;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;/** * 32_使用BeanUtil工具包操作JavaBean * @author Administrator */public class D32_IntroSpaectorTest {    public static void main(String[] args) throws Exception {        ReflectPoint rp = new ReflectPoint(3,5);        //获得JavaBean的属性         System.out.println(BeanUtils.getProperty(rp, "x"));        System.out.println(BeanUtils.getProperty(rp, "x").getClass().getName());        System.out.println(rp.getX());        //设置JavaBean属性--设置x这个属性的值为9        //BeanUtils设置的x时候是以String设置的,获取也是以String获取的,为什么呢?        //因为用户输入的年龄提交到服务器是一String提交的,而目标是int,那么BeanUtils        //就先转换成int后才设置的,那么用户在获取年龄时也是先把int的年龄转换成String后        //才返回给用户的。        BeanUtils.setProperty(rp, "x", 9);          BeanUtils.setProperty(rp, "x", "9");            System.out.println(rp.getX());        BeanUtils.setProperty(rp, "birthday.time", "111");        System.out.println(BeanUtils.getProperty(rp, "birthday.time"));        /*        //java7的新特性        //对Map来说,BeanUtils还有个好处,假设我有个Map对象        Map map = (name:"wgx",age:25);        BeanUtils.setProperty(rp, "name", "jiewin");        */        PropertyUtils.setProperty(rp, "x", 88);        System.out.println(rp.getX());    }}/*结果3java.lang.String3911188*/

  ——- android培训、java培训、期待与您交流! ———-

0 0
原创粉丝点击