CgLib介绍

以下一段代码是生成动态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]

你可能感兴趣的:(bean,proxy,method,cglib)