当前位置: 移动技术网 > IT编程>开发语言>Java > Java内省实例解析

Java内省实例解析

2019年07月19日  | 移动技术网IT编程  | 我要评论

图像中轮廓的个数,里面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内省实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网