CgLib介绍

2016/12/31 20:49
阅读数 23
以下一段代码是生成动态Bean的一个Example: [codesyntax lang="java" lines="normal"]
import java.util.Map;
import java.util.Set;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

/**
 * @author suren
 *
 */
public class AbstractViewGenerator {
	private Object object = null;
	private BeanMap beanMap = null;

	public AbstractViewGenerator(Map<String, Class<?>> propertyMap)
	{
		this.object = generateBean(propertyMap);
		this.beanMap = BeanMap.create(this.object);
	}

	public void setValue(String property, Object value)
	{
		this.beanMap.put(property, value);
	}

	public Object getValue(String property)
	{
		return beanMap.get(property);
	}

	public Object getObject()
	{
		return this.object;
	}

	private Object generateBean(Map<String, Class<?>> propertyMap)
	{
		BeanGenerator generator = new BeanGenerator();

		Set<String> keySet = propertyMap.keySet();
		for(String key : keySet)
		{
			generator.addProperty(key, propertyMap.get(key));
		}

		return generator.create();
	}
}
[/codesyntax]
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部