有个技术实现需求:动态生成类,其中类中的属性来自参数对象中的全部属性以及来自参数对象propertities文件。
那么技术实现支持:使用cglib代理。
具体的实现步骤:
1.cglib的jar包(cglib-nodep):我用的是cglib-nodep-2.2.jar
------------------------------------------分割线------------------------------------------
具体下载目录在 /2014年资料/11月/3日/Java动态生成类以及动态添加属性/
------------------------------------------分割线------------------------------------------
2.封装的cglib类:
package com.citics.test.model;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBean {
private Object object = null;//动态生成的类
private BeanMap beanMap = null;//存放属性名称以及属性的类型
public DynamicBean() {
super();
}
@SuppressWarnings("rawtypes")
public DynamicBean(Map propertyMap) {
this.object = generateBean(propertyMap);
this.beanMap = BeanMap.create(this.object);
}
/**
* 给bean属性赋值
* @param property 属性名
* @param value 值
*/
public void setValue(Object property, Object value) {
beanMap.put(property, value);
}
/**
* 通过属性名得到属性值
* @param property 属性名
* @return 值
*/
public Object getValue(String property) {
return beanMap.get(property);
}
/**
* 得到该实体bean对象
* @return
*/
public Object getObject() {
return this.object;
}
/**
* @param propertyMap
* @return
*/
@SuppressWarnings("rawtypes")
private Object generateBean(Map propertyMap) {
BeanGenerator generator = new BeanGenerator();
Set keySet = propertyMap.keySet();
for (Iterator i = keySet.iterator(); i.hasNext();) {
String key = (String) i.next();
generator.addProperty(key, (Class) propertyMap.get(key));
}
return generator.create();
}
}
3.需求的实现类:
package com.citics.platformx.test.controller;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import com.citics.test.model.DynamicBean;
import com.citics.test.model.LeapRole;
public class ClassUtil {
private String filepath="/config/";//配置文件路径
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
@SuppressWarnings({ "rawtypes","unchecked"})
public Object dynamicClass(Object object) throws Exception{
HashMap returnMap = new HashMap();
HashMap typeMap=new HashMap();
//读取配置文件
Properties prop=new Properties();
String sourcepackage=object.getClass().getName();
String classname=sourcepackage.substring(sourcepackage.lastIndexOf(".")+1);
InputStream in=ClassUtil.class.getResourceAsStream(filepath+classname+".properties");
prop.load(in);
Set<String> keylist=prop.stringPropertyNames();
Class type=object.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();
for(int i=0;i<propertyDescriptors.length;i++){
PropertyDescriptor descriptor=propertyDescriptors[i];
String propertyName = descriptor.getName();
if(!propertyName.equals("class")){
Method readMethod=descriptor.getReadMethod();
Object result = readMethod.invoke(object, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
typeMap.put(propertyName, descriptor.getPropertyType());
}
}
//加载配置文件中的属性
Iterator<String> iterator=keylist.iterator();
while(iterator.hasNext()){
String key=iterator.next();
returnMap.put(key, prop.getProperty(key));
typeMap.put(key, Class.forName("java.lang.String"));
}
//map转换成实体对象
DynamicBean bean = new DynamicBean(typeMap);
//赋值
Set keys=typeMap.keySet();
for(Iterator it=keys.iterator();it.hasNext();){
String key = (String) it.next();
bean.setValue(key, returnMap.get(key));
}
Object obj=bean.getObject();
return obj;
}
public static void main(String[] args) throws Exception{
new ClassUtil().dynamicClass(new LeapRole());
}
}