有個技術實現需求:動態生成類,其中類中的屬性來自參數對象中的全部屬性以及來自參數對象properties文件。
那么技術實現支持:使用CGLib代理。
具體的實現步驟:
1.配置Maven文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journey</groupId>
<artifactId>journey</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
2.封裝的cglib類
package com.journey;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Created by little_eleventh_wolf on 2017/11/18.
*/
public class DynamicBean {
private Object object = null; //動態生成的類
private BeanMap beanMap = null; //存放屬性名稱以及屬性的類型
public DynamicBean() {
super();
}
public DynamicBean(Map propertyMap) {
this.object = generateBean(propertyMap);
this.beanMap = BeanMap.create(this.object);
}
/**
* @param propertyMap
* @return
*/
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();
}
/**
* 給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;
}
}
3.需求的實現類:
package com.journey;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
/**
* Created by little_eleventh_wolf on 2017/11/18.
*/
public class ClassUtil {
private String filePath = "/config/"; //配置文件路徑
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
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()/*LeapRole是個普通類,未貼源碼*/);
}
}
4.技術實現目的:前台框架表格數據源實際上就是帶有數據的實體,但是grid中數據的類型、以及是否可見、toolbar工具欄上的按鈕、是否分頁,是針對實體而言,所以目前把這些信息作為實體的配置文件。在展示頁面之前,讀取全部信息,轉為參數對象的完整對象。
