首頁  >  問答  >  主體

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 fun1
before 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 fun1
fun2
可見fun2方法沒有被代理。

為什麼會有這樣的差異?

phpcn_u1582phpcn_u15822664 天前1146

全部回覆(1)我來回復

  • 阿神

    阿神2017-07-03 11:45:34

    spring的aop無法攔截內部方法調用,spring 會報存真實對象的 bean 以及 代理後的 proxyBean,proxyBean進行了切面增強處理:
    proxyBean 相當於:

    before

    invoke(bean,method)

    after
    這樣處理就導致實際上 fun2 是實際的 bean 去調用的(invoke就是使用實際物件執行你要執行的方法),所以,沒有 before 效果。
    而你實際使用 cglib 則全程都是用的是代理 bean

    回覆
    0
  • 取消回覆