软件包 java.beans

package com.beans;

import java.beans.BeanInfo;
import java.beans.Expression;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;

import java.io.FileInputStream;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

public class BeansTest {
	public static void main(String[] args) throws Exception {
		TreeMap<String,String> meth = meth();
		System.out.println(meth);
		
		BeansTestTemp beansTemp = new BeansTestTemp();
		Map map = new HashMap();
		
		Properties pro = new Properties();
		pro.setProperty("setStr", "str");
		
		Object evaluatePo = BeansTest.evaluatePo("com.beans.BeansTestTemp", pro);
		System.out.println(evaluatePo);
		
//		Object evaluatePo2 = BeansTest.evaluatePo(beansTemp, pro);
//		System.out.println(evaluatePo2);
	}

	
	public static Object evaluatePo(String className, Properties prop)
			throws Exception {
//		Class<? extends Object> poClass = className.getClass();
		Class<?> poClass = Class.forName(className);
			Object po = poClass.newInstance();
		// Introspector相当beans这个架构的一个入口。
		// 类似于Hibernate的SessionFactory// 通过bean的类型获得bean的描述—》获得属性描述的集合
		BeanInfo bi = Introspector.getBeanInfo(poClass);
		PropertyDescriptor[] pd = bi.getPropertyDescriptors();

		for (int i = 0; i < pd.length; i++) {
			String name = pd[i].getName();
			System.out.println("name = " + name);
			
			String property = prop.getProperty(name);
			if (property != null) {
				Class<?> propertyType = pd[i].getPropertyType();
				
				Object value = getPropValue(propertyType,property);

				executeEvaluate(po, pd[i].getWriteMethod(), value);
			}
		}
		return po;
	}

	// 这里是PropertyEditor 的使用,规避了if条件的使用。让代码的扩展性得到了很好的保证。
	// 这里使用的是依赖查找(DL)的方式。// 注册PropertyEditor 的步骤放在了bean容器当中
	static Object getPropValue(Class<?> clazz, String value) throws Exception {
		PropertyEditor pe = PropertyEditorManager.findEditor(clazz);
		pe.setAsText(value);
		return pe.getValue();
	}

	// 调用赋值方法
	static void executeEvaluate(Object dest, Method m, Object value)
			throws Exception {
		Expression e = new Expression(dest, m.getName(), new Object[] { value });
		e.execute();
	}
	
	private static TreeMap<String, String> meth() throws Exception {
		Properties prop=new Properties();
		 
		FileInputStream inputStream=new FileInputStream("src/a.properties");
		  
		prop.load(inputStream);
		  
		inputStream.close();
		 
		TreeMap<String, String> treeMap=new TreeMap<String, String>((Map)prop);
		
		return treeMap;
	}
}

class BeansTestTemp{
	private String str ;
	private int i ;
	
	
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public int getI() {
		return i;
	}
	public void setI(int i) {
		this.i = i;
	}
	@Override
	public String toString() {
		return "BeansTestTemp [str=" + str + ", i=" + i + "]";
	}
	
	
}

a.properties

a=1
b=2

猜你喜欢

转载自jie130890.iteye.com/blog/2337992