JavaBean

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

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


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值