Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Java动态生成类以及动态添加属性

有个技术实现需求:动态生成类,其中类中的属性来自参数对象中的全部属性以及来自参数对象propertities文件。那么技术实现支持:使用cglib代理。具体的实现步骤:1.cglib的jar包(cglib-nodep):我用的是cglib-nodep-2.2.jar------------------------------------------分割线------------------------------------------免费下载地址在 http://linux.linuxidc.com/用户名与密码都是www.linuxidc.com具体下载目录在 /2014年资料/11月/3日/Java动态生成类以及动态添加属性/下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm------------------------------------------分割线------------------------------------------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());
 }
}4.技术实现目的:前台框架表格数据源实际上就是带有数据的实体,但是grid中数据的类型、已经是否可见、toolbar工具栏上的按钮、是否分页,是针对实体而言,所以目前把这些信息作为实体的配置文件。在展示页面之前,读取全部信息,转为参数对象的完整对象。Java中介者设计模式 http://www.linuxidc.com/Linux/2014-07/104319.htmJava 设计模式之模板方法开发中应用 http://www.linuxidc.com/Linux/2014-07/104318.htm设计模式之 Java 中的单例模式(Singleton) http://www.linuxidc.com/Linux/2014-06/103542.htmJava对象序列化 http://www.linuxidc.com/Linux/2014-10/107584.htm大话设计模式(带目录完整版) PDF+源代码 http://www.linuxidc.com/Linux/2014-08/105152.htm本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/108908.htm