1. 概述
JDK动态代理是利用java反射机制 生成一个实现接口的匿名类, 在调用具体方法前调用InvocationHandler来处理
Cglib动态代理是 利用asm开源包 把被代理类的class文件加载进来 通过修改其字节码生成子类来处理
如果目标对象实现了接口 那么默认使用jdk代理(可以强制使用cglib代理)
如果没有实现接口 必须使用cglib代理
强制使用cglib代理需要
*引入cglibjar包
*配置spring <aop:aspectj-autoproxy proxy-target-class="true"/>
cglib因为是动态生成被代理类的子类 并覆盖被代理类的方法 来实现的 所以 被代理方法不要使用final修饰
2. 代码示例
2.1 cglib代理类
package com.rocky.spring; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; public class CglibProxy { public static void main(String[] args) {
final UserService service = new UserService();
Enhancer hancer = new Enhancer();
hancer.setSuperclass(service.getClass());
hancer.setCallback(new MethodInterceptor(){ @Override
public Object intercept(Object proxy, Method method, Object[] arg2,
MethodProxy arg3) throws Throwable {
System.out.println("增强前 ... Cglib");
Object invoke = method.invoke(service, arg2);
System.out.println("增强后 ... Cglib");
return invoke;
}});
UserService userService = (UserService) hancer.create();
userService.sayHello(); }
}
//需要引入cglib-2.2.jar 和org.objectweb.asm-3.3.1.jar
//输出
//增强前 ... Cglib
//this userService works....
//增强后 ... Cglib
被代理类UserService
package com.rocky.spring; public class UserService { public void sayHello(){
System.out.println("this userService works....");
} }
2.2 jdk代理接口
package com.rocky.spring; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class JdkProxy { public static void main(String[] args) {
final ActorService service = new ActorServiceImpl();
ActorService actorService = (ActorService) Proxy.newProxyInstance(
service.getClass().getClassLoader(), service.getClass()
.getInterfaces(), new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
System.out.println("增强前...jdk");
Object invoke = method.invoke(service, args);
System.out.println("增强后...jdk");
return invoke;
}
});
actorService.sayHi();
}
}
//增强前...jdk
//Honestly, I do the work.
//增强后...jdk
被代理接口及实现类
package com.rocky.spring; public interface ActorService { public void sayHi();
} ----------------- package com.rocky.spring; public class ActorServiceImpl implements ActorService { @Override
public void sayHi() {
doSomething();
} private void doSomething() {
System.out.println("Honestly, I do the work.");
} }