使用cglib在記憶體中動態產生類__java

來源:互聯網
上載者:User
【博文總目錄>>>】|【項目源碼>>>】

CGLIB是一個強大的、高效能的代碼產生庫。其被廣泛應用於AOP架構(Spring、dynaop)中,用以提供方法攔截操作。本樣本展示了如何使用cglib動態產生類。

package wjc.cglib;import net.sf.cglib.beans.BeanGenerator;import net.sf.cglib.beans.BeanMap;import java.util.Map;/** * <pre> * 建立一個動態實體bean,使用cglib動態加入屬性,和相應的get,set方法 * </pre> * Author: 王俊超 * Date: 2018-02-22 09:18 * Blog: http://blog.csdn.net/derrantcm * Github: https://github.com/wang-jun-chao * All Rights Reserved !!! */public class CglibBean {    /**     * 實體Object     */    private Object object = null;    /**     * 屬性map     */    private BeanMap beanMap = null;    public CglibBean() {        super();    }    public CglibBean(Map<String, Class<?>> 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;    }    private Object generateBean(Map<String, Class<?>> propertyMap) {        if (propertyMap == null || propertyMap.isEmpty()) {            return null;        }        BeanGenerator generator = new BeanGenerator();        for (Map.Entry<String, Class<?>> entry : propertyMap.entrySet()) {            generator.addProperty(entry.getKey(), entry.getValue());        }        return generator.create();    }}
package wjc.cglib;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;/** * <pre> * CGLIB是一個強大的、高效能的代碼產生庫。其被廣泛應用於AOP架構(Spring、dynaop)中,用以提供方法攔截操作。 * </pre> * Author: 王俊超 * Date: 2018-02-22 09:26 * Blog: http://blog.csdn.net/derrantcm * Github: https://github.com/wang-jun-chao * All Rights Reserved !!! */public class ClassGeneration {    public static void main(String[] args) throws ClassNotFoundException {        // 設定類成員屬性        Map<String, Class<?>> 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", 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();        // 查看產生的類名        System.out.println(clazz.getName());        // 通過反射查看所有方法名        Method[] methods = clazz.getDeclaredMethods();        for (Method method : methods) {            System.out.println(method.getName());        }    }}

運行結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.