转至:
cglib
是一个开源项目!
是一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java类与实现Java接口。许多开源项目都用到了它,如Hibernate,Spring之类用它来实现动态代理。
简单样例—动态生成bean
样例说明:建立一个动态实体bean,使用cglib动态加入属性,和相应的get,set方法。
需要导入cglib-nodep-2.2.jar
been类
- <strong>public class CglibBean {
- /**
- * 实体Object
- */
- public Object object = null;
- /**
- * 属性map
- */
- public BeanMap beanMap = null;
- public CglibBean() {
- super();
- }
- @SuppressWarnings("unchecked")
- public CglibBean(Map propertyMap) {
- this.object = generateBean(propertyMap);
- this.beanMap = BeanMap.create(this.object);
- }
- /**
- * 给bean属性赋值
- * @param property 属性名
- * @param value 值
- */
- public void setValue(String 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;
- }
- @SuppressWarnings("unchecked")
- 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();
- }
- }
- </strong>
再写一个测试类
- package com.infotech.cglibTest;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- /**
- * Cglib测试类
- * @author cuiran
- * @version 1.0
- */
- public class CglibTest {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws ClassNotFoundException {
- // 设置类成员属性
- HashMap propertyMap = new HashMap();
- propertyMap.put("id", Class.forName("java.lang.Integer"));
- propertyMap.put("name", Class.forName("java.lang.String"));
- propertyMap.put("address", Class.forName("java.lang.String"));
- // 生成动态 Bean
- CglibBean bean = new CglibBean(propertyMap);
- // 给 Bean 设置值
- bean.setValue("id", new Integer(123));
- bean.setValue("name", "454");
- bean.setValue("address", "789");
- // 从 Bean 中获取值,当然了获得值的类型是 Object
- System.out.println(" >> id = " + bean.getValue("id"));
- System.out.println(" >> name = " + bean.getValue("name"));
- System.out.println(" >> address = " + bean.getValue("address"));
- // 获得bean的实体
- Object object = bean.getObject();
- // 通过反射查看所有方法名
- Class clazz = object.getClass();
- Method[] methods = clazz.getDeclaredMethods();
- for (int i = 0; i < methods.length; i++) {
- System.out.println(methods[i].getName());
- }