JavaBean操作详解

一、bean简介

在java的反射中,所有的类被抽象出一个类,即Class类,这样我们就可以在程序运行的过程中通过配置文件,动态的加载类。但是在用反射的时候有些前提,就是:当我们调用有参的constructor的时候必须先知道构造函数传入的参数是什么类型;调用Method的时候必须先知道成员函数传入的参数是什么类型;调用field的时候必须先知道成员变量的类型。但是在某些情况下,一个类中的成员属性的名字对外是不可见的,这时候我们只可以得出他的成员类型的数组field[],在不知道确切变量名的情况下不可以定位到确切的变量上。而他提供了对这个属性公开的读(get)、写(set)方法, 虽然方法名和方法操作的对象有时候有很大的关系,但是我们不能保证这个关系一定成立。而且这种只提供get、set方法,不提供变量名的情况还很常见,因此java就将这种抽象为javabean类,对javabean类的操作,通过get、set函数后面的名字就“拟”得出变量的名字。通过这个名字,我们可以确切的求出某个变量的值。而对javabean的操作是通过内省(introspector)来完成的。

二、内省访问JavaBean属性的两种方式:

通过PropertyDescriptor类操作Bean的属性
通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。

下面是实例:

package reflect;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class TestBean {

    public static void main(String[] args) throws Exception {
        testProDes();
        testBeanInfo();
        testBeanInfo1();
        
    }
    
    static void testBeanInfo() throws Exception{
        Dog d = (Dog) Class.forName("reflect.Dog").newInstance();
        S使用beanuitls框架操作bean的属性tring myname = "name";    
        BeanInfo beanInfo = Introspector.getBeanInfo(Dog.class);//beaninfo中存放bean的所有信息
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for(PropertyDescriptor pd : propertyDescriptors){
            if(pd.getName().equals(myname)){
                Method m1 = pd.getWriteMethod();
                m1.invoke(d,"zhangsan");
                Method m2 = pd.getReadMethod();
                m2.invoke(d);
                System.out.println(d);
                break;
            }
        }
    }
    
    static void testProDes() throws Exception{
        Dog d = (Dog) Class.forName("reflect.Dog").newInstance();
        String myname = "name" ;//这里是根据函数名确定的string的变量名,而不是内部定义的私有属性名。
        PropertyDescriptor pd = new PropertyDescriptor(myname, Dog.class);//propertyDescriptor直接得到属性描述符
        
        Method m1 = pd.getWriteMethod();
        m1.invoke(d,"zhangsan");
        Method m2 = pd.getReadMethod();
        m2.invoke(d);
        System.out.println(d);
    }
    static void testBeanInfo1() throws Exception{
        Dog d = (Dog) Class.forName("reflect.Dog").newInstance();
        String myname = "name";    
        BeanInfo beanInfo = Introspector.getBeanInfo(Dog.class);//beaninfo中存放bean的所有信息
        MethodDescriptor[] mds = beanInfo.getMethodDescriptors();
        for(MethodDescriptor md : mds){
            String str = md.getName();
            if (str.matches("[gs]et.*")) {
                System.out.println(str);//打印set/get开头的函数
            }使用beanuitls框架操作bean的属性
        }
    }

}

class Dog {
    private String n ;
    private int a ;
    
    public Dog(){
    }
    public Dog(String name,int age){
        this.n = name;
        this.a = age;
    }
    
    public String getName() {
        return n;
    }
    public void setName(String name) {
        this.n = name;
    }
    public int getAge() {
        return a;
    }
    public void setAge(int age) {
        this.a = age;
    }
    @Override
    public String toString(){
        return n + " : "+a;
    }
    
}

在这里我们故意将Dog类中的get/set方法和属性名定义的不一样。但是通过内省,我们并不需要知道确切的内部属性的名字,通过get/set的名字,就可以实现对属性的设置和读取。

第一个函数直接用propertyDescriptor得出属性描述符,然后通过read方法求出get对应的method,write方法求出set对应的method,通过method的反射invoke函数实现对属性的操作。

第二种函数和第一个函数实现一样的功能,但是路径不一样,先用introspector静态方法得出beaninfo,beaninfo中包含了所有的关于类的信息,再通过beaninfo的方法得出propertyDescriptor,在按照第一种的实现方法实现对属性的操作。

第三个函数顺便测试了methoddescripor,将这个javabean类中的所有get/set的方法打印出来。

三、beanutils工具包操作bean

Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils
Beanutils工具包的常用类:
BeanUtils
PropertyUtils
ConvertUtils.regsiter(Converter convert, Class clazz)自定义转换器

四、使用beanuitls框架操作bean的属性

1、创建Student类:

package cn.itcast.beanutils;
import java.util.Date;
//javabean (属性)
public class Student {
<span style="white-space:pre">	</span> private String name; //只要有get和set中的任意一个方法,那么就是属性,否则为字段
<span style="white-space:pre">	</span> private int age;
<span style="white-space:pre">	</span> private Date birthday;
<span style="white-space:pre">	</span> 
<span style="white-space:pre">	</span> public String getName() {
<span style="white-space:pre">		</span> return name;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public void setName(String name) {
<span style="white-space:pre">		</span> this.name = name;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public int getAge() {
<span style="white-space:pre">		</span> return age;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public void setAge(int age) {
<span style="white-space:pre">		</span> this.age = age;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public Date getBirthday() {
<span style="white-space:pre">		</span> return birthday;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public void setBirthday(Date birthday) {
<span style="white-space:pre">		</span> this.birthday = birthday;
<span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span> public String getX(){        //X也是属性,只要有get或者set方法就算
<span style="white-space:pre">		</span> return "X";
<span style="white-space:pre">	</span> }
 //其实之中还有一个默认的getClass,所以一共有5个字段
}
2、使用beanuitls框架操作bean的属性

Person类:

package cn.itcast.beanutils;

import java.util.Date;

public class Person {
	private String name;
	private String password;
	private int age;
	private Date birthday;
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public String getPassword() {
		return password;
	}
	public int getAge() {
		return age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

操作Person代码:

package cn.itcast.beanutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

//使用beanUtils操纵bean的属性 ( 第三方)
public class Demo1 {
	@Test
	public void test1() throws Exception{
		Person p=new Person();
		BeanUtils.setProperty(p, "age", 456);
		System.out.println(p.getAge());//456
	}
	@Test
	public void test2() throws Exception{
		String name="aaaa";
		String age="123";
		String password="pw";
				
		Person p=new Person();
		//支持8种基本类型自动转换
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "password", password);
		
		System.out.println(p.getName());//aaaa
		System.out.println(p.getAge());//123
		System.out.println(p.getPassword());//pw

	}
	@Test
	public void test3() throws Exception{

		String birthday="1983-12-1";
		
		//为了让日期赋值到bean的birthday属性上,给beanUtils注册一个日期转换器
		//ConvertUtils.register(converter, clazz);
		ConvertUtils.register(new Converter(){
			
			public Object convert(Class type, Object value) {
				if(value==null) return null;
				if(!(value instanceof String)){
					throw new ConversionException("只支持String类型的转换");
				}
				String str=(String)value;
				if(str.trim().equals("")) return null;
				SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd",Locale.US);
				try {
					return df.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e);					
				}
			}
		}, Date.class);
		Person p=new Person();
		BeanUtils.setProperty(p, "birthday", birthday);
		System.out.println(p.getBirthday());//pw
		System.out.println("___"+BeanUtils.getProperty(p, "birthday"));
	}
	public void test5() throws Exception {
		Map map=new HashMap();
		map.put("name", "aaa");
		map.put("password", "123");
		map.put("brithday", "1980-09-09");
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		Person p=new Person();
		//用map集合填充bean属性,map关键字和bean属性要一致
		BeanUtils.populate(p, map);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值