Java和CGLIB的动态代理

静态代理和动态代理的区别是: 代理类和委托类的关系是在运行时还是运行前确定.

动态代理分两种:

  • Java反射机制实现的动态代理
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    
    public class MyInvocationHandler implements InvocationHandler {
        private Object target;
    
        MyInvocationHandler() {
            super();
        }
    
        MyInvocationHandler(Object target) {
            super();
            this.target = target;
        }
    
        @Override
        public Object invoke(Object o, Method method, Object[] args) throws Throwable {
            if("getName".equals(method.getName())){
                System.out.println("++++++before " + method.getName() + "++++++");
                Object result = method.invoke(target, args);
                System.out.println("++++++after " + method.getName() + "++++++");
                return result;
            }else{
                Object result = method.invoke(target, args);
                return result;
            }
    
        }
    }
    
    
    import com.meituan.hyt.test3.service.UserService;
    import com.meituan.hyt.test3.service.impl.UserServiceImpl;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Proxy;
    
    public class Main1 {
        public static void main(String[] args) {
            UserService userService = new UserServiceImpl();
            InvocationHandler invocationHandler = new MyInvocationHandler(userService);
            UserService userServiceProxy = (UserService)Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                    userService.getClass().getInterfaces(), invocationHandler);
            System.out.println(userServiceProxy.getName(1));
            System.out.println(userServiceProxy.getAge(1));
        }
    }
    
  • Cglib是一个优秀的动态代理框架,它的底层使用ASM在内存中动态的生成被代理类的子类,使用CGLIB即使代理类没有实现任何接口也可以实现动态代理功能。CGLIB具有简单易用,它的运行速度要远远快于JDK的Proxy动态代理.
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    import java.lang.reflect.Method;
    
    public class CglibProxy implements MethodInterceptor {
        @Override
        public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
            System.out.println(method.getName());
            Object o1 = methodProxy.invokeSuper(o, args);
            System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
            return o1;
        }
    }
    
    
    import com.meituan.hyt.test3.service.UserService;
    import com.meituan.hyt.test3.service.impl.UserServiceImpl;
    import net.sf.cglib.proxy.Enhancer;
    
    public class Main2 {
        public static void main(String[] args) {
            CglibProxy cglibProxy = new CglibProxy();
    
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(UserServiceImpl.class);
            enhancer.setCallback(cglibProxy);
    
            UserService o = (UserService)enhancer.create();
            o.getName(1);
            o.getAge(1);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值