什么是java内省_Java内省实例解析

图像中轮廓的个数,里面vector的size代表了轮廓上点的个数。了解JavaBean

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

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

如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,这种JavaBean的实例对象通常称之为值对象(ValueObject,简称VO)。这些信息在类中用私有字段来存储,如果读取或设置这些字段的值,则需要通过一些相应的方法来访问,大家觉得这些方法的名称叫什么好呢?JavaBean的属性是根据其中的setter和getter方法来确定的,而不是根据其中的成员变量。如果方法名为setId,中文意思即为设置id,至于你把它存到哪个变量上,用管吗?如果方法名为getId,中文意思即为获取id,至于你从哪个变量上取,用管吗?去掉set前缀,剩余部分就是属性名,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。

例如:

setId()的属性名-->id

isLast()的属性名-->last

setCPU的属性名是什么?-->CPU

getUPS的属性名是什么?-->UPS

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

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

在JavaEE开发中,经常要使用到JavaBean。很多环境就要求按JavaBean方式进行操作,别人都这么用和要求这么做,那你就没什么挑选的余地!

JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。如果要你自己去通过getX方法来访问私有的x,怎么做,有一定难度吧?用内省这套api操作JavaBean比用普通类的方式更方便。

对JavaBean的简单内省操作

主要用到了java.beans.PropertyDescriptor类,用来得到某个Class对象属性集中的某个JavaBean属性,然后调用getReadMethod()、getWriteMethod()方法获得相应的get、set方法。

代码示例:

Domain类:

[cpp]viewplaincopy

intmain()

package ustc.lichunchun.bean;

import java.util.Date;

public class ReflectPoint {

private Date birthday = new Date();

private int x;

public int y;

public String str1 = "ball";

public String str2 = "basketball";

public String str3 = "itcast";

public ReflectPoint(int x, int y) {

super();

this.x = x;

this.y = y;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + x;

result = prime * result + y;

return result;

}

@Override

public Boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

final ReflectPoint other = (ReflectPoint) obj;

if (x != other.x)

return false;

if (y != other.y)

return false;

return true;

}

@Override

public String toString(){

return str1 + ":" + str2 + ":" + str3;

}

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;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

简单内省操作:

package ustc.lichunchun.bean;

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 IntroSpectorTest {

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

ReflectPoint pt1 = new ReflectPoint(3, 5);

String propertyName = "x";

//"x"-->"X"-->"getX"-->MethodGetX-->

getProperty(pt1, propertyName);

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 {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodSetX = pd.getWriteMethod();

methodSetX.invoke(pt1, value);

}

private static Object getProperty(Object pt1, String propertyName)

throws IntrospectionException, IllegalAccessException, InvocationTargetException {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodGetX = pd.getReadMethod();

methodGetX.invoke(pt1);

}

}

对JavaBean的复杂内省操作

采用遍历BeanInfo的所有属性方式来查找和设置某个RefectPoint对象的x属性。在程序中把一个类当作JavaBean来看,就是调用IntroSpector.getBeanInfo方法,得到的BeanInfo对象封装了把这个类当作JavaBean看的结果信息。

复杂内省操作:

package ustc.lichunchun.bean;

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 IntroSpectorTest {

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

ReflectPoint pt1 = new ReflectPoint(3, 5);

String propertyName = "x";

//"x"-->"X"-->"getX"-->MethodGetX-->

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 {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodSetX = pd.getWriteMethod();

methodSetX.invoke(pt1, value);

}

private static Object getProperty(Object pt1, String propertyName)

throws IntrospectionException, IllegalAccessException, InvocationTargetException {

/*

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodGetX = pd.getReadMethod();

methodGetX.invoke(pt1);

*/

BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());

PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

Object retVal = null;

for (PropertyDescriptor pd : pds){

if(pd.getName().equals(propertyName)){

Method methodGetX = pd.getReadMethod();

retVal = methodGetX.invoke(pt1);

break;

}

}

return retVal;

}

}

使用BeanUtils工具包操作JavaBean

在前面内省例子的基础上,用BeanUtils类先get原来设置好的属性,再将其set为一个新值。get属性时返回的结果为字符串,set属性时可以接受任意类型的对象,通常使用字符串。

用PropertyUtils类先get原来设置好的属性,再将其set为一个新值。get属性时返回的结果为该属性本来的类型,set属性时只接受该属性本来的类型。

注意:用这两个类之前,需要在eclipse工程的lib文件夹中导入commons-beanutils.jar、commons-logging-1.1.jar两个jar包,并且AddtoBuildPath。

代码示例:

package ustc.lichunchun.bean;

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;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.PropertyUtils;

public class IntroSpectorTest {

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

ReflectPoint pt1 = new ReflectPoint(3, 5);

String propertyName = "x";

//"x"-->"X"-->"getX"-->MethodGetX-->

Object retVal = getProperty(pt1, propertyName);

System.out.println(retVal);

Object value = 7;

setProperty(pt1, propertyName, value);

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

//String

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

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

/*

Map map = {name:"zxx",age:18};//java7的新特性

BeanUtils.setProperty(map, "name", "lcc");

*/

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

//支持属性链

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

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

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

//Integer

/*

BeanUtils和PropertyUtils的区别:

BeanUtils以字符串形式对JavaBean进行操作,也可以操作Map类,并且可以讲JavaBean和Map进行互相转换(describe、populate)

PropertyUtils以JavaBean属性本身的数据类型进行操作

*/

}

private static void setProperty(Object pt1, String propertyName, Object value)

throws IntrospectionException, IllegalAccessException, InvocationTargetException {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodSetX = pd.getWriteMethod();

methodSetX.invoke(pt1, value);

}

private static Object getProperty(Object pt1, String propertyName)

throws IntrospectionException, IllegalAccessException, InvocationTargetException {

/*

PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());

Method methodGetX = pd.getReadMethod();

methodGetX.invoke(pt1);

*/

BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());

PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

Object retVal = null;

for (PropertyDescriptor pd : pds){

if(pd.getName().equals(propertyName)){

Method methodGetX = pd.getReadMethod();

retVal = methodGetX.invoke(pt1);

break;

}

}

return retVal;

}

}

总结

以上就是本文关于Java内省实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值