动态代理
静态代理
public interface IService {
void method1();
void method2();
void method3();
}
public class ServiceA implements IService {
Logger logger = LoggerFactory.getLogger(ServiceA.class);
@Override
public void method1() {
logger.info("我是ServiceA中的method1方法");
}
@Override
public void method2() {
logger.info("我是ServiceA中的method2方法");
}
@Override
public void method3() {
logger.info("我是ServiceA中的method3方法");
}
}
public class ServiceB implements IService {
@Override
public void method1() {
System.out.println("我是ServiceB中的method1方法");
}
@Override
public void method2() {
System.out.println("我是ServiceB中的method2方法");
}
@Override
public void method3() {
System.out.println("我是ServiceB中的method3方法");
}
}
静态代理
public class Staticproxy implements IService {
Logger logger = LoggerFactory.getLogger(Staticproxy.class);
private IService target;
public Staticproxy(IService target) {
this.target = target;
}
@Override
public void method1() {
long startTime = System.nanoTime();
target.method1();
long endTime = System.nanoTime();
logger.info("耗时时间为:{}", endTime - startTime);
}
@Override
public void method2() {
long startTime = System.nanoTime();
target.method2();
long endTime = System.nanoTime();
logger.info("耗时时间为:{}", endTime - startTime);
}
@Override
public void method3() {
long startTime = System.nanoTime();
target.method2();
long endTime = System.nanoTime();
logger.info("耗时时间为:{}", endTime - startTime);
}
}
JDK动态代理
public interface IService {
void method1();
void method2();
void method3();
}
public class ServiceA implements IService {
Logger logger = LoggerFactory.getLogger(ServiceA.class);
@Override
public void method1() {
logger.info("我是ServiceA中的method1方法");
}
@Override
public void method2() {
logger.info("我是ServiceA中的method2方法");
}
@Override
public void method3() {
logger.info("我是ServiceA中的method3方法");
}
}
定义动态代理:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyInvocationHandler implements InvocationHandler {
Logger logger = LoggerFactory.getLogger(MyInvocationHandler.class);
private Object tarjet;
public MyInvocationHandler(Object tarjet) {
this.tarjet = tarjet;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName() + " ->> 方法被执行");
long startTime = System.nanoTime();
Object invoke = method.invoke(tarjet, args);
long endTime = System.nanoTime();
logger.info("耗时时间为:{}", endTime - startTime);
return invoke;
}
/**
* 获取被代理接口实例对象
* @param <T>
* @return
*/
public <T> T getProxy() {
return (T) Proxy.newProxyInstance(tarjet.getClass().getClassLoader(), tarjet.getClass().getInterfaces(), this);
}
}
测试:
public class JDKProxyTest {
public static void main(String[] args) {
IService serviceProxy = new MyInvocationHandler(new ServiceA()).getProxy();
serviceProxy.method1();
serviceProxy.method2();
serviceProxy.method3();
}
}
输出结果:
method1 ->> 方法被执行
17:04:33.338 [main] INFO com.zy.proxy.service.ServiceA - 我是ServiceA中的method1方法
17:04:33.339 [main] INFO com.zy.proxy.jdkproxy.MyInvocationHandler - 耗时时间为:3024300
method2 ->> 方法被执行
17:04:33.340 [main] INFO com.zy.proxy.service.ServiceA - 我是ServiceA中的method2方法
17:04:33.340 [main] INFO com.zy.proxy.jdkproxy.MyInvocationHandler - 耗时时间为:45900
method3 ->> 方法被执行
17:04:33.340 [main] INFO com.zy.proxy.service.ServiceA - 我是ServiceA中的method3方法
17:04:33.340 [main] INFO com.zy.proxy.jdkproxy.MyInvocationHandler - 耗时时间为:20200
CGLIB动态代理
public class ServiceC{
public void method1() {
System.out.println("我是ServiceB中的method1方法");
}
public void method2() {
System.out.println("我是ServiceB中的method2方法");
}
public void method3() {
System.out.println("我是ServiceB中的method3方法");
}
}
定义CGLIB动态代理:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CGLibFactory implements MethodInterceptor {
Logger logger = LoggerFactory.getLogger(MethodInterceptor.class);
private Object target;
public CGLibFactory(Object target) {
this.target = target;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println(method.getName() + " ->> 方法被执行");
long startTime = System.nanoTime();
Object invoke = method.invoke(target, objects);
long endTime = System.nanoTime();
logger.info("耗时时间为:{}", endTime - startTime);
return invoke;
}
public <T> T getProxy() {
//增强器
Enhancer enhancer = new Enhancer();
// 创建子类,作为代理类
enhancer.setSuperclass(target.getClass());
// 设置回调类
enhancer.setCallback(this);
return (T) enhancer.create();
}
}
测试:
public class CGLibTest {
public static void main(String[] args) {
ServiceC serviceC = new ServiceC();
ServiceC serviceProxy = new CGLibFactory(serviceC).getProxy();
serviceProxy.method1();
serviceProxy.method2();
serviceProxy.method3();
}
}
输出结果:
method1 ->> 方法被执行
我是ServiceB中的method1方法
18:29:03.935 [main] INFO org.springframework.cglib.proxy.MethodInterceptor - 耗时时间为:24500
method2 ->> 方法被执行
我是ServiceB中的method2方法
18:29:03.938 [main] INFO org.springframework.cglib.proxy.MethodInterceptor - 耗时时间为:23300
method3 ->> 方法被执行
我是ServiceB中的method3方法
18:29:03.938 [main] INFO org.springframework.cglib.proxy.MethodInterceptor - 耗时时间为:19400

浙公网安备 33010602011771号