Java----JavaBean

高新技术 JavaBean
一、JavaBean
JavaBean:是一种特殊的Java类,主要用于传递数据信息。这种Java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。
public class Person {

	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
}
符合这种形式的类称为JavaBean
JavaBean应用:如果两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,这种JavaBean的实例对象通常称之为值对象(Value Object,简称VO)。这些信息在类中用私有字段来存储。如果读取或设置这些字段的值,则需要通过一些相应的方法来访问。
JavaBean属性的命名:如果除去getset外,第二个字母是小写的,则把第一个字母也小写。如:
getAge——>age
getTime——>time
getCPU——>CPU
总之一个类被当作JavaBean使用时,JavaBean的属性是根据方法名推断出来的。根本看不到Java类内部的成员变量。
使用JavaBean好处
1.在JavaEE开发中,经常要使用到JavaBean。很多环节都要求按JavaBean方式进行操作。
2.JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。用内省这套API操作JavaBean比普通类的方式更方便。
package cn.itcast.day1;

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

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		Person p = new Person(20);
		PropertyDescriptor pd = new PropertyDescriptor("age", p.getClass());
		Method GetAge = pd.getReadMethod();
		Object retVal = GetAge.invoke(p, null);
		System.out.println(retVal);
		
		Method SetAge = pd.getWriteMethod();
		SetAge.invoke(p, 30);
		
		System.out.println(p.getAge());
	}

}

可以对代码进行重构,把获取动作变成方法。快捷键Shift+Alt+M

优化
package cn.itcast.day1;

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

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		Person p = new Person(20);
		String propertyName = "age";
		
		Object retVal = getProperty(p, propertyName);
		System.out.println(retVal);
		
		setProperty(p, propertyName, 30);
		System.out.println(p.getAge());
	}

	private static void setProperty(Person p, String propertyName, Object obj)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass());
		Method SetAge = pd.getWriteMethod();
		SetAge.invoke(p, obj);
	}

	private static Object getProperty(Person p, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass());
		Method GetAge = pd.getReadMethod();
		Object retVal = GetAge.invoke(p, null);
		return retVal;
	}

}
另一种方法(较繁琐):
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;

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		Person p = new Person(20);
		String propertyName = "age";
		
		Object retVal = getProperty(p, propertyName);
		System.out.println(retVal);
		
		setProperty(p, propertyName, 30);
		System.out.println(p.getAge());
	}

	private static void setProperty(Person p, String propertyName, Object obj)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		//PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass());
		//Method SetAge = pd.getWriteMethod();
		//SetAge.invoke(p, obj);
		
	}

	private static Object getProperty(Person p, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
//		PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass());
//		Method GetAge = pd.getReadMethod();
//		Object retVal = GetAge.invoke(p, null);
		
		Object retVal = null;
		BeanInfo bi = Introspector.getBeanInfo(p.getClass());
		PropertyDescriptor[] pds = bi.getPropertyDescriptors();
		for(PropertyDescriptor pd : pds){
			if(pd.getName().equals(propertyName)){
				retVal = pd.getReadMethod().invoke(p, null);
			}
		}
		return retVal;
	}
	
}
使用BeanUtils工具包操作JavaBeanBeanUtils是由apache编写的一个操作JavaBean的工具。用来操作JavaBean非常方便。
步骤:
1.首先必须要有BeanUtils工具包,在http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi可以下载。
2.需要将工具包导入到项目中。右键项目——>Build Path——>Libraries——>Add External JARs...——选择commons-beanutils-1.8.3.jar

BeanUtils类:
static String	getProperty(Object bean, String name) 
          Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.
大意:以字符串形式返回指定的bean中指定的任意属性。

package cn.itcast.day1;

import org.apache.commons.beanutils.BeanUtils;

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		
		Person p = new Person(20);
		String propertyName = "age";
		String str = BeanUtils.getProperty(p, propertyName);
		System.out.println(str);
	}
}

想要运行还缺少一个工具commons-logging-1.1.3.jar。下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi
然后导入jar包。
为了能够让其他电脑上也能编译,要把2个jar拷贝到工程中。在项目中建立一个lib文件夹。

jar包复制到里面。然后Build Path——>Libraries——>Add JARs...——>OK

运行成功。

添加设置方法。
package cn.itcast.day1;

import org.apache.commons.beanutils.BeanUtils;

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		
		Person p = new Person(20);
		String propertyName = "age";
		String str = BeanUtils.getProperty(p, propertyName);
		System.out.println(str);
		
		BeanUtils.setProperty(p, propertyName, 30);
		System.out.println(p.getAge());
	}
}

特殊之处:可以为复合属性赋值。
package cn.itcast.day1;

import org.apache.commons.beanutils.BeanUtils;

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		
		Person p = new Person(20);
		String propertyName = "age";
		String str = BeanUtils.getProperty(p, propertyName);
		System.out.println(str);
		
		BeanUtils.setProperty(p, propertyName, 30);
		System.out.println(p.getAge());
		
		BeanUtils.setProperty(p, "birthday.time", "2013");//为复合属性赋值
		System.out.println(BeanUtils.getProperty(p, "birthday.time"));
	}
}
package cn.itcast.day1;

import java.util.Date;

public class Person {

	private int age;
	private Date birthday = new Date();//添加Date对象
	
	public Person(int age) {
		super();
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

解析:因为Date有个setTime方法,所以也可以看作是一个bean对象。“birthday.time”这种形式称为属性链
补充BeanUtilsMap可以相互转换。在API文档中有很多转换方法。PropertyUtils类的setProperty()getProperty()方法可以用属性本身类型操作,而不是字符串。
package cn.itcast.day1;

import org.apache.commons.beanutils.PropertyUtils;

public class JavaBeanTest {

	public static void main(String[] args) throws Exception {
		
		Person p = new Person(20);
		String propertyName = "age";
		Object obj = PropertyUtils.getProperty(p, propertyName);
		System.out.println(obj);
		
		PropertyUtils.setProperty(p, propertyName, 30);
		System.out.println(p.getAge());
		
		PropertyUtils.setProperty(p, "birthday.time", 2013);
		System.out.println(PropertyUtils.getProperty(p, "birthday.time"));
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值