java - 原生CGLib内部方法互相调用时可以代理,但基于CGLib的Spring AOP却代理失效,为什么?
问题描述
下面是CGLib的原生写法(使用net.sf.cglib.proxy.*包内的类实现)
class Foo { public void fun1(){System.out.println('fun1');fun2(); } public void fun2() {System.out.println('fun2'); }}class CGlibProxyEnhancer implements MethodInterceptor{ public Object getProxy(Class clazz) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(clazz);enhancer.setCallback(this);return enhancer.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.print('before ');Object result = proxy.invokeSuper(obj,args);return result; }}public class Test { public static void main(String[] args) {CGlibProxyEnhancer pf = new CGlibProxyEnhancer();Foo foo = (Foo) pf.getProxy(Foo.class);foo.fun1(); }}
打印结果是:before fun1before fun2可以看到,虽然fun2()是通过foo.fun1()调用的,但fun()2依然能被代理。
但如果用Spring AOP那套基本写法的话:
class Foo { public void fun1() {System.out.println('fun1');fun2(); } public void fun2() {System.out.println('fun2'); }}class Before implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable {System.out.print('before '); }}public class TestCGLib { public static void main(String[] args) {Foo foo = new Foo();BeforeAdvice advice = new Before();ProxyFactory pf = new ProxyFactory();pf.setOptimize(true);//启用Cglib2AopProxy创建代理pf.setProxyTargetClass(true);pf.setTarget(foo);pf.addAdvice(advice);Foo proxy = (Foo) pf.getProxy();proxy.fun1(); }}
输出结果是:before fun1fun2可见fun2方法没有被代理。
为什么会有这样的差异?
问题解答
回答1:spring的aop无法拦截内部方法调用,spring 会报存真实对象的 bean 以及 代理后的 proxyBean,proxyBean进行了切面增强处理:proxyBean 相当于:
before
invoke(bean,method)
after这样处理就导致实际上 fun2 是实际的 bean 去调用的(invoke就是使用实际对象执行你要执行的方法),所以,没有 before 效果。而你实际使用 cglib 则全程都是用的是代理 bean
