<返回更多

Cglib动态代理详解

2023-08-20  微信公众号  程序员长承
加入收藏

CGLIB 简介

CGLIB 原理

CGLIB 应用

图片

CGLIB API

Code Example

Basic

package com.cclu.cglibproxy;
import java.util.HashMap;import java.util.Map;
/** * @author ChangCheng Lu * @date 2023/8/20 15:59 */public class UserService {
    private static final Map<String, String> nameMap;
    private static final Map<String, String> addressMap;
    static {        nameMap = new HashMap<>(3);        nameMap.put("1001", "木子");        nameMap.put("1002", "小一");        nameMap.put("1003", "阿同");
        addressMap = new HashMap<>(3);        addressMap.put("1001", "涟水");        addressMap.put("1002", "淮安");        addressMap.put("1003", "上海");    }
    public String getUserNameById(String userId) {        return nameMap.get(userId);    }
    public String getAddressById(String userId) {        return addressMap.get(userId);    }
}
package com.cclu.cglibproxy;
import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/** * @author ChangCheng Lu * @date 2023/8/20 16:04 */public class LogInterceptor implements MethodInterceptor {    @Override    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {        System.out.println("方法调用前...");        Object res = methodProxy.invokeSuper(o, objects);        System.out.println("方法调用后:"+res);        return res;    }}
package com.cclu.cglibproxy;
import net.sf.cglib.proxy.Enhancer;
/** * @author ChangCheng Lu * @date 2023/8/20 16:10 */public class Client {    public static void mAIn(String[] args) {        Enhancer enhancer = new Enhancer();        enhancer.setSuperclass(UserService.class);        enhancer.setCallback(new LogInterceptor());        UserService userService = (UserService) enhancer.create();        userService.getUserNameById("1001");        userService.getAddressById("1003");    }}

Filter、FixedValue

package com.cclu.cglibproxy;
import java.util.HashMap;import java.util.Map;
/** * @author ChangCheng Lu * @date 2023/8/20 15:59 */public class UserService {
    private static final Map<String, String> nameMap;
    private static final Map<String, String> addressMap;
    static {        nameMap = new HashMap<>(3);        nameMap.put("1001", "木子");        nameMap.put("1002", "小一");        nameMap.put("1003", "阿同");
        addressMap = new HashMap<>(3);        addressMap.put("1001", "苏州");        addressMap.put("1002", "淮安");        addressMap.put("1003", "上海");    }
    public String getUserNameById(String userId) {        return nameMap.get(userId);    }
    public String getAddressById(String userId) {        return addressMap.get(userId);    }
}
package com.cclu.cglibproxy;
import net.sf.cglib.proxy.CallbackFilter;
import java.lang.reflect.Method;
/** * @author ChangCheng Lu * @date 2023/8/20 16:35 */public class LogInterceptorFilter implements CallbackFilter {    @Override    public int accept(Method method) {        if (method.getName().equals("getUserNameById")) {            System.out.println("是不是木子?");            return 1;        } else if (method.getName().equals("getAddressById")) {            System.out.println("是不是阿同?");            return 2;        }        return 0;    }}
package com.cclu.cglibproxy;
import net.sf.cglib.proxy.FixedValue;
/** * @author ChangCheng Lu * @date 2023/8/20 17:44 */public class TargetResultFixed implements FixedValue {    @Override    public Object loadObject() throws Exception {        System.out.println("锁定结果");        Object obj = "苏州";        return obj;    }}
package com.cclu.cglibproxy;
import net.sf.cglib.proxy.Callback;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.NoOp;import org.junit.Test;
/** * @author ChangCheng Lu * @date 2023/8/20 17:32 */public class ProxyTest {
    @Test    public void filterTest() {        Enhancer enhancer = new Enhancer();        enhancer.setSuperclass(UserService.class);        enhancer.setCallbackFilter(new LogInterceptorFilter());        enhancer.setCallbacks(new Callback[]{NoOp.INSTANCE, new LogInterceptor(), new TargetResultFixed()});
        UserService userService = (UserService) enhancer.create();        System.out.println(userService);        System.out.println(userService.getUserNameById("1001"));        System.out.println(userService.getAddressById("1003"));    }
}

参考文档

关键词:Cglib      点击(9)
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多Cglib相关>>>