如何在免费网站上做推扩,自然志 wordpress,怎么建设一个论坛网站,有模板了怎么建设网站文章目录 一、JDK动态代理1、关键类和接口2、实现步骤 二、CGLIB动态代理1、关键类2、实现步骤 三、总结 Java中的动态代理是一种设计模式#xff0c;它允许在运行时创建代理对象#xff0c;而不是在编译时。代理对象可以用来代理真实对象的方法调用。
Java中的动态代理主要… 文章目录 一、JDK动态代理1、关键类和接口2、实现步骤 二、CGLIB动态代理1、关键类2、实现步骤 三、总结 Java中的动态代理是一种设计模式它允许在运行时创建代理对象而不是在编译时。代理对象可以用来代理真实对象的方法调用。
Java中的动态代理主要有两种实现方式JDK动态代理和CGLIB动态代理。
一、JDK动态代理
JDK动态代理使用Java标准库中的java.lang.reflect包来创建代理对象。它适用于实现了接口的类通过接口类型来代理对象。
1、关键类和接口
InvocationHandler接口 这是一个接口必须实现其invoke方法代理对象的所有方法调用都会转发到这个方法。Proxy类 这是一个用来创建代理对象的类提供了静态方法newProxyInstance来生成代理对象。
2、实现步骤
1定义接口和实现类
public interface MyService {void perform();
}public class MyServiceImpl implements MyService {Overridepublic void perform() {System.out.println(Executing perform method);}
}2实现InvocationHandler接口编写代理逻辑
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;public class MyInvocationHandler implements InvocationHandler {private Object target;public MyInvocationHandler(Object target) {this.target target;}Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(Before method invoke);Object result method.invoke(target, args);System.out.println(After method invoke);return result;}
}3使用Proxy.newProxyInstance方法创建代理对象
import java.lang.reflect.Proxy;public class Main {public static void main(String[] args) {MyService myService new MyServiceImpl();MyInvocationHandler handler new MyInvocationHandler(myService);MyService proxyInstance (MyService) Proxy.newProxyInstance(myService.getClass().getClassLoader(),myService.getClass().getInterfaces(),handler);proxyInstance.perform();}
}二、CGLIB动态代理
CGLIBCode Generation Library是一种强大的字节码生成库它允许在运行时创建子类适用于没有实现接口的类。CGLIB通过继承被代理类来创建代理对象因此无法代理final类。
1、关键类
Enhancer类 这是CGLIB用来生成代理类的主要类通过设置超类和回调来创建代理对象。MethodInterceptor接口 这是一个接口类似于InvocationHandler需要实现intercept方法。
2、实现步骤
1添加CGLIB依赖
在项目中添加CGLIB库的依赖。对于Maven项目
dependencygroupIdcglib/groupIdartifactIdcglib/artifactIdversion3.3.0/version
/dependency2定义实现类定义一个没有实现接口的类
public class MyService {public void perform() {System.out.println(Executing perform method);}
}3实现MethodInterceptor编写代理逻辑。
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;public class MyMethodInterceptor implements MethodInterceptor {Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.println(Before method invoke);Object result proxy.invokeSuper(obj, args);System.out.println(After method invoke);return result;}
}4使用Enhancer类创建代理对象。
import net.sf.cglib.proxy.Enhancer;public class Main {public static void main(String[] args) {Enhancer enhancer new Enhancer();enhancer.setSuperclass(MyService.class);enhancer.setCallback(new MyMethodInterceptor());MyService proxyInstance (MyService) enhancer.create();proxyInstance.perform();}
}三、总结
JDK动态代理适用于实现了接口的类通过InvocationHandler处理代理逻辑。CGLIB动态代理适用于没有实现接口的类通过继承实现代理使用MethodInterceptor处理代理逻辑。性能更优。
动态代理在AOP面向切面编程、拦截器、权限控制、事务管理等领域有广泛应用提供了灵活的方式来在运行时增强对象行为。