Spring AOP功能最基本的技术要点为动态代理。
当下Java主要有两种动态代理方式
- 基于接口的JDK动态代理
- 基于类CGLib动态代理
JDK动态代理
JDK动态代理是在运行时根据类的接口生成新的实现类,让新的实现类对已有对象进行代理。
首先声明两个接口
public interface Machine {
public void start();
}
public interface Car extends Machine {
public void drive();
}
实现类
public class Bus implements Car {
@Override
public void drive() {
System.out.println("老司机开车啦!");
}
@Override
public void start() {
System.out.println("开始!!");
}
}
代理处理类:
public class JdkProxy implements InvocationHandler {
private Object obj;
@SuppressWarnings( "unchecked" )
public <T> T getProxy( T obj ) {
this.obj = obj;
return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
}
@Override
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable {
System.out.println("动态代理开始!");
Object reVal = method.invoke(obj, args);
System.out.println("动态代理结束!");
return reVal;
}
}
Proxy.newProxyInstance是使用当前类的接口生成代理对象。
InvocationHandler 为代理的处理接口。
Main函数
public static void main( String[] args ) {
Car car = new Bus();
JdkProxy jdkProxy = new JdkProxy();
Car proxyCar = jdkProxy.getProxy(car);
proxyCar.start();
proxyCar.drive();
}
CGLib动态代理
CGLib是生成当前类的子类,让子类对父类进行代理。
引入Jar包
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
被代理的类
public class Student {
public void sayHello() {
System.out.println("hello");
}
}
代理处理及代理对象生成
public class StudentProxy implements MethodInterceptor {
private Enhancer enhancer;
public <T> T getProxy( Class<T> clazz ) {
enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return (T) enhancer.create();
}
@Override
public Object intercept( Object object, Method method, Object[] args, MethodProxy proxy ) throws Throwable {
System.out.println("before");
proxy.invokeSuper(object, args);
System.out.println("after");
return null;
}
}
Main函数:
public static void main( String[] args ) {
StudentProxy proxy = new StudentProxy();
Student student = proxy.getProxy(Student.class);
student.sayHello();
}
结果:
简单测试两种代理方式的性能:
/**
* @author : Zak
* @date : 2017年1月17日 上午11:52:23
* @version : 2017年1月17日 Zak 首次创建
*/
public class Main {
public static void main( String[] args ) {
Bus bus = new Bus();
JdkProxy jdkProxy = new JdkProxy();
CglibProxy cglibProxy = new CglibProxy();
long jdkproxyInitTime = 0, cglibproxyinitTime = 0, jdkInvokeTime = 0, cglibInvokeTime = 0, startTime = 0;
for( int i = 0; i < 1000001; i++ ) {
System.out.println(i);
switch(i % 2) {
case 0:// JDKProxy
startTime = System.currentTimeMillis();
Car car = jdkProxy.getProxy(bus);
jdkproxyInitTime += System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
car.drive();
jdkInvokeTime += System.currentTimeMillis() - startTime;
break;
case 1:// CGLib
startTime = System.currentTimeMillis();
Car car1 = cglibProxy.getProxy(Bus.class);
cglibproxyinitTime += System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
car1.drive();
cglibInvokeTime += System.currentTimeMillis() - startTime;
}
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~Result~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("JDKInitTtime:" + jdkproxyInitTime);
System.out.println("jdkInvokeTime:" + jdkInvokeTime);
System.out.println("jdkInvokeTime:" + cglibproxyinitTime);
System.out.println("cglibInvokeTime:" + cglibInvokeTime);
}
}