黑马程序员_内省

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

内省

内省概述

内省对应的英文单词为IntroSpector,它主要用于对JavaBean进行操作,JavaBean是一种特殊的Java类,其中的某些方法符合某种命名规则,如果一个Java类中的一些方法符合某种命名规则,则可以把它当作JavaBean来使用。请问:一个JavaBean可以当做普通Java类来使用吗?一个普通Java类可以当做JavaBean来使用吗?

 

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

·如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,这种JavaBean的实例对象通常称之为值对象(Value Object,简称VO)。这些信息在类中用私有字段来存储,如果读取或设置这些字段的值,则需要通过一些相应的方法来访问。

 

·JavaBean的属性是根据其中的setter和getter方法来确定的,而不是根据其中的成员变量。如果方法名为setId,中文意思即为设置id,至于你把它存到哪个变量上,则不用管。如果方法名为getId,中文意思即为获取id,至于你从哪个变量上取,也不用管。去掉set前缀,剩余部分就是属性名,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。

例如:
setId()的属性名:id
isLast()的属性名:
last
setCPU的属性名:
CPU
getUPS的属性名:UPS

总之,一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量。


·一个符合JavaBean特点的类可以当作普通类一样进行使用,但把它当JavaBean用肯定需要带来一些额外的好处,我们才会去了解和应用JavaBean!

好处如下:
1-在Java EE开发中,经常要使用到JavaBean。很多环境就要求按JavaBean方式进行操作,别人都这么用和要求这么做,那你就没什么挑选的余地!
2-JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。如果要你自己去通过getX方法来访问私有的x,怎么做,有一定难度吧?用内省这套api操作JavaBean比用普通类的方式更方便。

 

JavaBean的简单内省操作

通过内省的方式对ReflectPoint对象中的成员变量进行读写操作。

示例:

ReflectTest.java
package com.itheima.day1;

public class ReflectPoint {
       private int x ;
       private int y ;
      
       public ReflectPoint(int x, int y) {
             super();
             this.x = x;
             this.y = y;
      }

      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;
      }
}

ReflectTest.java
package com.itheima.day1;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class ReflectTest {

       public static void main(String[] args) throws Exception {
         ReflectPoint pt1 = new ReflectPoint(3, 5);
        
         String propertyName = "x";
         PropertyDescriptor pd1 = new PropertyDescriptor(propertyName, pt1.getClass());
         Method readMethod = pd1.getReadMethod();
         Object retVal = readMethod.invoke(pt1);
         System. out.println(retVal);
         //结果:3
        
         PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, pt1.getClass());
         Method writeMethod = pd2.getWriteMethod();
         Object value = 7;
         writeMethod.invoke(pt1,value);
         System. out.println(pt1.getX());
         //结果:7
       }
}

上面的通过反射对某个属性进行读写操作的代码可以抽取为通用方法,通过eclipse可以非常轻松实现这个功能。

步骤如下:

选中需要重构的代码,右击-->Refactor-->Extract Method...。

给抽取后的方法取一个名字,然后点击OK。

效果如下:

ReflectTest.java

package com.itheima.day1;

 

import java.beans.IntrospectionException;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

 

public class ReflectTest {

 

       public static void main(String[] args) throws Exception {

         ReflectPoint pt1 = newReflectPoint(3, 5);

        

         String propertyName ="x";

         Object retVal = getProperty(pt1, propertyName);

         System.out.println(retVal);

        

         Object value = 7;

         setProperty(pt1, propertyName, value);

         System.out.println(pt1.getX());

       }

 

      privatestaticvoidsetProperty(Object pt1, String propertyName,

                  Object value)throwsIntrospectionException,

                  IllegalAccessException, InvocationTargetException {

         PropertyDescriptor pd2 =newPropertyDescriptor(propertyName, pt1.getClass());

         Method writeMethod = pd2.getWriteMethod();

         writeMethod.invoke(pt1,value);

      }

 

      privatestaticObject getProperty(Object pt1, String propertyName)

                  throwsIntrospectionException, IllegalAccessException,

                  InvocationTargetException {

         PropertyDescriptor pd1 =newPropertyDescriptor(propertyName, pt1.getClass());

         Method readMethod = pd1.getReadMethod();

         Object retVal = readMethod.invoke(pt1);

         returnretVal;

      }

}

 

注意:

抽取方法之后,为了使抽取后的方法具备通用性,一定要把方法中某些参数设置为Object类型。

示例:
ReflectTest.java
package com.itheima.day1;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {

       public static void main(String[] args) throws Exception {
         ReflectPoint pt1 = new ReflectPoint(3, 5);
        
         String propertyName = "x";
         Object retVal = getProperty(pt1, propertyName);
         System. out.println(retVal);
        
         Object value = 7;
         setProperty(pt1, propertyName, value);
         System. out.println(pt1.getX());
       }

       private static void setProperty(Object pt1, String propertyName,
                  Object value) throws IntrospectionException,
                  IllegalAccessException, InvocationTargetException {
         BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
         PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
         for (PropertyDescriptor pd : pds){
                if (pd.getName().equals(propertyName)){
                     Method writeMethod = pd.getWriteMethod();
                     writeMethod.invoke(pt1,value);
               }
         }
      }

       private static Object getProperty(Object pt1, String propertyName)
                   throws IntrospectionException, IllegalAccessException,
                  InvocationTargetException {
         BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
         PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
         Object retVal = null ;
         for (PropertyDescriptor pd : pds){
                if (pd.getName().equals(propertyName)){
                     Method readMethod = pd.getReadMethod();
                     retVal = readMethod.invoke(pt1);
               }
         }
         return retVal;
       }
}

使用BeanUtils工具包操作JavaBean

由于对javabean的设值获取使用频率较高,开源的勇士们做一些方便的工具类(专门针对javabean的操作)提供给开发人员使用。

使用BeanUtils工具包首先要导入jar包。

 

首先在工程下创建一个lib文件夹-->将beanutils的jar包拷贝到其中-->右击该jar包-->Build Path-->Add to Build Path。

 

当看到beanutils的jar包变成奶瓶的时候说明jar包已经添加成功。

 

需要注意的是beanutils工具包依赖于logging包,因此按照上面的方式将commons-logging也添加到工程中。

 

效果如下:

 

编写代码,可以发现使用BeanUtils工具包可以很容易实现与上面的代码相同的效果。

示例:

ReflectTest.java

package com.itheima.day1;

 

import org.apache.commons.beanutils.BeanUtils;

 

public class ReflectTest {

 

       public static void main(String[] args) throws Exception {

         ReflectPoint pt1 = newReflectPoint(3, 5);

        

         System.out.println(BeanUtils.getProperty(pt1,"x"));

         BeanUtils. setProperty(pt1,"x","7");

         System.out.println(pt1.getX());

       }

}

 

注意:

1-BeanUtils工具类在对对象的属性进行操作的时候,会自动进行类型转换。例如ReflectPoint类中的x属性为int类型,但是设置属性值的时候传入的参数却可以是String类型,这是因为内部发生了自动类型转换。

 

2-BeanUtils工具类可以对属性进行级联操作,例如Date(java.util.Date)类中有setTime方法,那么也就相当于Date类型对象有一个time属性,BeanUtils就可以对其进行操作。

示例:

ReflectPoint.java

package com.itheima.day1;

 

import java.util.Date;

 

public class ReflectPoint {

       private int x ;

       private int y ;

       privateDatebirthday=newDate();

      

       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;

      }

 

      publicDate getBirthday() {

            returnbirthday;

      }

 

      publicvoidsetBirthday(Date birthday) {

            this.birthday= birthday;

      }

      

}

 

ReflectTest.java

package com.itheima.day1;

 

import org.apache.commons.beanutils.BeanUtils;

 

public class ReflectTest {

 

       public static void main(String[] args) throws Exception {

         ReflectPoint pt1 = newReflectPoint(3, 5);

        

         BeanUtils. setProperty(pt1,"birthday.time","111");

         System.out.println(BeanUtils.getProperty(pt1,"birthday.time"));

        //结果:111

       }

}

 

3-PropertyUtils类也可以操作对象的属性,但是与BeanUtils不同的是它不能进行自动类型转换。

例如ReflectPoint类中的x属性为int类型,但是设置属性值的时候传入的参数就不可以是String类型。

示例:

ReflectTest.java

package com.itheima.day1;

 

import org.apache.commons.beanutils.PropertyUtils;

 

public class ReflectTest {

 

       public static void main(String[] args) throws Exception {

         ReflectPoint pt1 = newReflectPoint(3, 5);

        

         System.out.println(PropertyUtils.getProperty(pt1,"x"));

         PropertyUtils. setProperty(pt1,"x","7");

         System.out.println(pt1.getX());

       }

}

 

就会曝出如下错误:

Exception in thread "main" java.lang.IllegalArgumentException : Cannot invoke com.itheima.day1.ReflectPoint.setX on bean class 'class com.itheima.day1.ReflectPoint' - argument type mismatch - had objects of type "java.lang.String" but expected signature "int"

      at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)

      at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2097)

      at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1903)

      at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2010)

      at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:896)

      at com.itheima.day1.ReflectTest.main( ReflectTest.java:12)

Caused by: java.lang.IllegalArgumentException: argument type mismatch

      at sun.reflect.NativeMethodAccessorImpl.invoke0( Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

      at java.lang.reflect.Method.invoke( Method.java:597)

      at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2116)

      ... 5 more

 

设置传入的属性值参数为int类型就不会报错了。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值