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 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
cglib
cglib
3.2.9
需要实现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 newProxyInstance(T targerClass, MethodInterceptor methodInterceptor){
return (T) Enhancer.create(targerClass.getClass(),methodInterceptor);
}
}