读书人

普普通通的使用java反射内省以及使用B

发布时间: 2013-03-12 11:19:35 作者: rapoo

普通的使用java反射内省以及使用BeanUtils工具包对javaBean进行操作


/**
?* 使用beanUtils工具包? 需要导入第三方jar
?* commons-beanutils-1.8.3.jar
?* commons-logging-1.1.1.jar
?*/


package com.sg.reflex.test;

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.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import com.sg.bean.TestBeanReflex;

public class TestNeiXing {

??? public static void main(String[] args)throws Exception {
??? ??? TestBeanReflex br = new TestBeanReflex("2", "5");
??? ???
??? ??? String propertyName = "x";
??? ??? String returnValue = getProperty(br, propertyName);
??? ??? System.out.println(returnValue);
??? ??? Object value = "3";
??? ??? setProperty(br, propertyName, value);
??? ??? System.out.println(br.getX());
??? ???
??? ??? /**
??? ??? ?* 通过BeanUtils操作获取bean属性的值? 可以自动将参数类型转换成bean类对应参数的类型
??? ??? ?*/
??? ??? System.out.println(BeanUtils.getProperty(br, propertyName));
??? ??? System.out.println(BeanUtils.getProperty(br, propertyName).getClass().getName());//java.lang.String
??? ??? /**
??? ??? ?* 通过BeanUtils这是bean属性的值
??? ??? ?*/
??? ??? BeanUtils.setProperty(br, propertyName, "5");
??? ??? System.out.println(br.getX());
??? ??? /**
??? ??? ?* 通过BeanUtils操作bean属性的级联操作
??? ??? ?*/
??? ??? BeanUtils.setProperty(br, "brithDay.time", "111");
??? ??? System.out.println(BeanUtils.getProperty(br,"brithDay.time"));//结果111
??? ??? /**
??? ??? ?* beanUtils将一个bean转换成一个map对象
??? ??? ?*/
??? ??? Map map = BeanUtils.describe(br);
??? ??? /*Iterator iterator = map.entrySet().iterator();
??? ??? while(iterator.hasNext()){
??? ??? ??? String key = (String)iterator.next();
??? ??? ??? map.get(key);
??? ??? }
??? ???
??? ??? Set set =? map.keySet();
??? ??? for(Object object : set){
??? ??? ??? System.out.println("key : "+object + "value : " + map.get(object));
??? ??? }*/
??? ??? /**
??? ??? ?* PropertyUtils也具有同样的功能? 但是参数值? 要与bean的属性的类型一致
??? ??? ?*/
??? ??? PropertyUtils.setProperty(br, "z", 9);
??? ??? System.out.println(BeanUtils.getProperty(br, "z"));//
??? ??? System.out.println(BeanUtils.getProperty(br, "z").getClass().getName());//
??? }
???
???

??? public static void setProperty(TestBeanReflex br, String propertyName,
??? ??? ??? Object value) throws IntrospectionException,
??? ??? ??? IllegalAccessException, InvocationTargetException {
??? ??? PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, br.getClass());
??? ??? Method method = descriptor.getWriteMethod();
??? ??? method.invoke(br, value);
??? }

??? public static String getProperty(TestBeanReflex br, String propertyName)
??? ??? ??? throws IntrospectionException, IllegalAccessException,
??? ??? ??? InvocationTargetException {
??? ??? /*PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, br.getClass());
??? ??? Method method = descriptor.getReadMethod();
??? ??? String returnValue = (String)method.invoke(br);
??? ??? return returnValue;*/
??? ??? //第二种操作
??? ??? //获取那个javaBean的所有信息
??? ??? BeanInfo beanInfo =? Introspector.getBeanInfo(br.getClass());
??? ??? //获取那个javaBean的所有属性的信息
??? ??? PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
??? ??? String returnValue = null;
??? ??? //遍历所有的属性
??? ??? for(PropertyDescriptor pd : pds){
??? ??? ??? //获取属性的名字? 判断是否和传入的相等
??? ??? ??? if (pd.getName().equals(propertyName)) {
??? ??? ??? ??? //如果相等就获取它的get方法
??? ??? ??? ??? Method method = pd.getReadMethod();
??? ??? ??? ???
??? ??? ??? ??? returnValue = (String)method.invoke(br);
??? ??? ??? ??? break;
??? ??? ??? }
??? ??? }
??? ??? return returnValue;
??? }
???
???
???
}

读书人网 >编程

热点推荐