JDK动态代理与CGLIB 动态代理

一、JDK动态代理

实现方式:通过反射类获取目标类的接口实现,进行拦截和扩展

优点:通过字节通过反射获取目标对象的方法进行拦截

缺点:目标对象类一定要实现接口

示例代码:
public interface UserService {

    String getName();
}

public class UserServiceImpl implements UserService {

    @Override
    public String getName() {
        return "hello world";

    }
}

public class JDKProxyTest {

    @Test
    public void userServiceTest() {
        UserService userService = JDKProxyFactory.getProxy(UserServiceImpl.class, new UserInvocationHandler(new UserServiceImpl()));
        Assert.assertEquals("hello world", userService.getName());
    }
}

二、CGLIB 动态代理

实现方式:通过字节码创建子类的方式,采取子类方法拦截父类的方法,实现切入。

优点:可以代理不需要实现接口的类,执行效率高

缺点:使用字节码创建对象,耗时比JDK久,对于final修饰的方法无法进行代理。

示例代码:
    public class CglibProxyFactory {

    /**
     * @param tClass            目标代理对象类
     * @param methodInterceptor 方法拦截对象
     * @param <T>
     * @return
     */
    public static  <T> T getProxy(Class<T> tClass, MethodInterceptor methodInterceptor) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(tClass);
        enhancer.setCallback(methodInterceptor);
        return (T) enhancer.create();
    }
}

public class CglibProxyTest {

    @Test
    public void userServiceTest() {
        UserService userService = CglibProxyFactory.getProxy(UserServiceImpl.class, new UserServiceInterceptor());
        Assert.assertEquals("hello world", userService.getName());
    }
}

git地址:https://gitlab.com/xiaoc/proxy-Impl.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDK动态代理CGLib动态代理都是实现AOP编程的方式,它们的使用上有以下异同点: 1. JDK动态代理只能代理实现了接口的类,而CGLib动态代理可以代理没有实现接口的类。 2. JDK动态代理是通过反射来实现的,而CGLib动态代理使用的是继承。 3. JDK动态代理在生成代理对象时,需要传入一个InvocationHandler对象,而CGLib动态代理在生成代理对象时,需要继承MethodInterceptor类,并重写intercept()方法。 4. JDK动态代理生成的代理对象性能比CGLib动态代理生成的代理对象性能要低。 下面是一个JDK动态代理的例子: ```python import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface Subject { void request(); } class RealSubject implements Subject { public void request() { System.out.println("RealSubject request"); } } class DynamicProxy implements InvocationHandler { private Object subject; public DynamicProxy(Object subject) { this.subject = subject; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before calling " + method); method.invoke(subject, args); System.out.println("after calling " + method); return null; } } public class Main { public static void main(String[] args) { RealSubject realSubject = new RealSubject(); InvocationHandler handler = new DynamicProxy(realSubject); Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), handler); subject.request(); } } ``` 下面是一个CGLib动态代理的例子: ```python import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; class RealSubject { public void request() { System.out.println("RealSubject request"); } } class DynamicProxy implements MethodInterceptor { private Object subject; public DynamicProxy(Object subject) { this.subject = subject; } public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("before calling " + method); proxy.invoke(subject, args); System.out.println("after calling " + method); return null; } } public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(RealSubject.class); enhancer.setCallback(new DynamicProxy(new RealSubject())); RealSubject subject = (RealSubject) enhancer.create(); subject.request(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值