1、如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,这种JavaBean的实例对象通常称之为值对象(Value Object,简称VO)。这些信息在类中用私有字段来存储
Alt+Shift+S右键菜单选R是添加get,set
2、内省综合案例
IntroSpectorTest.java
package cn.itcast.day1;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IntroSpectorTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
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;
setProperties(pt1, propertyName, value);
System.out.println(pt1.getX());
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2=new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX=pd2.getWriteMethod();
methodSetX.invoke(pt1,value);
}
private static Object getProperty(ReflectPoint pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX=pd.getReadMethod();
Object retVal=methodGetX.invoke(pt1);
return retVal;
}
}
(二)Beanutils工具包
Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils,在Beanutil中可以直接进行类型的自动转换。
BeanUtil工具包下载:
1,登录 http://commons.apache.org/beanutils/
2, 点击Download
3, 点击Commons BeanUtils进行下载
使用BeanUtil
在项目中导入commons-beanutils-1.8.3.jar包即可
BeanUtil的应用
Beanutils工具包的常用类:
1,BeanUtils
2,PropertyUtils
3,ConvertUtils.regsiter(Converter convert, Class clazz)
4,自定义转换器用Beanutils工具包设置javaBean的属性和读取javaBean
导包两个方法:
1、将jar包拷贝下来,点击项目右键-〉BuildPath-〉Configure build path ,点击Libraries ,按钮“Add ExternalJARs...”是增加外面的jar包
(在本机运行没问题,工程内没有加入jar包,将工程导出时在其他机器时没有jar包)
2、在工程目录下建一个文件夹lib,把拷贝的jar包放在里面
右键jar包BuildPath-〉Add to Build Path
commons-logging-1.1.jar 是Apache下的开发包
右键工程->properties->java compiler->java Build Path
例子:IntroSpectorTest.java
package cn.itcast.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;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class IntroSpectorTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
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;
setProperties(pt1, propertyName, value);
System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());
BeanUtils.setProperty(pt1, "x", "9");//设置值 可以自动进行类型转换
System.out.println(pt1.getX());
/*
//java7的新特性
Map map={name:"zxx",age:18};
BeanUtils.setProperty(map, "name", "lhm");
*/
//BeanUtils是以字符串形式对java进行操作
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
//PropertyUtils是以属性本身的类型进行操作
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass());
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2=new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX=pd2.getWriteMethod();
methodSetX.invoke(pt1,value);
}
private static Object getProperty(ReflectPoint pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
/*PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX=pd.getReadMethod();
Object retVal=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;
}
}
ReflectPointReflectPoint.java
package cn.itcast.day1;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
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;
}
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;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
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;
}
}