什么是代理模式?
代理模式的主要作用是为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
代理模式的思想是为了提供额外的处理或者不同的操作而在实际对象与调用者之间插入一个代理对象。这些额外的操作通常需要与实际对象进行通信。
需求 使用代理在不改变原先代码的情况执行支付方法前后分别执行 打印个 支付开始执行 和 支付执行结束
以下根据三种代理模式进行实现
公用代码:
PayService 支付接口
public interface PayService {
/**
* 支付方法
*/
public void pay();
}
支付接口实现类
/**
* @Auther: heng
* @Date: 2021/4/25 10:58
* @Description: PayServiceImpl
* @Version 1.0.0
*/
public class PayServiceImpl implements PayService {
/**
* 支付方法实现类
*/
@Override
public void pay() {
System.err.println("这是一个支付接口.......");
}
}
静态代理实现代码
静态代理类StaticProxyService
/**
* @Auther: heng
* @Date: 2021/4/25 11:03
* @Description: StaticProxyService 静态代理类
* @Version 1.0.0
*/
public class StaticProxyService implements PayService {
private PayService payService;
public StaticProxyService(PayService payService) {
this.payService = payService;
}
@Override
public void pay() {
before();
payService.pay();
after();
}
public void before(){
System.err.println("before执行方法前执行");
}
public void after(){
System.err.println("after执行方法后执行");
}
}
Main方法测试
/**
* @Auther: heng
* @Date: 2021/4/25 11:14
* @Description: ProxyMain
* @Version 1.0.0
*/
public class ProxyMain {
public static void main(String[] args) {
PayService payService = new StaticProxyService(new PayServiceImpl());
payService.pay();
}
}
JDK动态代理方法实现需求(被代理类必须实现接口 否则会报未实现接口错误)
需要实现InvocationHandler
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* @Auther: heng
* @Date: 2021/4/25 13:11
* @Description: JdkInvocationHandler jdk动态代理
* @Version 1.0.0
*/
public class PayInvocationHandler implements InvocationHandler {
public Object targerClass;
public PayInvocationHandler(Object targerClass) {
this.targerClass = targerClass;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object result = method.invoke(targerClass, args);
after();
return result;
}
public void before(){
System.err.println("before执行方法前执行");
}
public void after(){
System.err.println("after执行方法后执行");
}
}
创建JdkProxy方法
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* @Auther: heng
* @Date: 2021/4/25 13:20
* @Description: JdkProxy jdk动态代理类 执行
* @Version 1.0.0
*/
public class JdkProxy {
public static <T> T newProxyInstance(T targerClass, InvocationHandler invocationHandler){
ClassLoader classLoader = targerClass.getClass().getClassLoader();
Class<?>[] interfaces = targerClass.getClass().getInterfaces();
return (T) Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
}
}
Main测试代码
//jdk动态代理
PayService payService = new PayServiceImpl();
PayInvocationHandler payInvocationHandler = new PayInvocationHandler(payService);
PayService proxyPayService = JdkProxy.newProxyInstance(payService, payInvocationHandler);
proxyPayService.pay();
Cglib动态代理实现需求方法(可以代理实现接口的类和不实现接口的类)
需要引入cglib jar
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.9</version>
</dependency>
需要实现MethodInterceptor
创建CglibInvocationHandler
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* @Auther: heng
* @Date: 2021/4/25 13:37
* @Description: CglibInvocationHandler cglib动态代理
* @Version 1.0.0
*/
public class CglibInvocationHandler implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
before();
Object result = methodProxy.invokeSuper(o, args);
after();
return result;
}
public void before(){
System.err.println("before执行方法前执行");
}
public void after(){
System.err.println("after执行方法后执行");
}
}
创建CglibProxy动态代理执行类
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
/**
* @Auther: heng
* @Date: 2021/4/25 13:42
* @Description: CglibProxy cglib动态代理执行类
* @Version 1.0.0
*/
public class CglibProxy {
public static <T> T newProxyInstance(T targerClass, MethodInterceptor methodInterceptor){
return (T) Enhancer.create(targerClass.getClass(),methodInterceptor);
}
}
Main测试
//cglib动态代理
CglibInvocationHandler cglibInvocationHandler = new CglibInvocationHandler();
PayServiceImpl payService = CglibProxy.newProxyInstance(new PayServiceImpl(), cglibInvocationHandler);
payService.pay();
PS:
以上代码下载地址:https://download.csdn.net/download/qq_39313596/17830207