2014-07-09 31 views
0

uppose療法是A類:SpringAOP複雜的切入點

public class A { 
    public B b; 

    public void justDoIt1(){ 
     b.getB(); 
    } 

    @SomeAnnotation 
    public void justDoIt2(){ 
     b.getB(); 
    } 
} 

和B類:

public class B{ 
    public void getB(){ 
     System.out.println("get"); 
    } 
} 

我們如何創建切入點B.getB(執行),其是從內部被稱爲方法用@SomeAnnotation註釋?

這裏是我試過

@Aspect 
public class LocalizationAspect { 
    @Before(value = "@within(Localize) && execution(* B.getB())") 
    public void aspectStuff() { 
     System.out.println("aspect"); 
    } 
} 

只是爲了讓我的觀點明確:調用justDoIt2()時,預期產出將是;

方面 得到

但調用justDoIt1時();

得到

注:我使用SpringAOP(也許它有一些這方面的限制) 任何幫助?

+0

您的類是否實現了包含建議方法的接口? (默認情況下,Spring AOP使用JDK代理) –

回答

1

如果我使用普通的AspectJ我這樣做:

execution(* B.getB()) && cflow(@withincode(SomeAnnotation))

「getB()與SomeAnnotation註釋的方法的控制流程的執行但這並不意味着它會被逮住,如果。例如,如果使用SomeAnnotation註釋的方法調用某些調用getB()的某個東西 - 會被這個建議捕獲 - 這會被這個建議所捕獲。

我不知道它如何在Spring AOP下運行。

編輯:在進一步思考時,上面的切入點可能不是最優的,因爲@withincode()可能會創建比絕對必要的更多的字節碼。更優化的版本可能是:

execution(* B.getB()) && cflow(execution(@SomeAnnotation * *(..)))

@withincode(SomeAnnotation)將通知所有參加的方法中的點標記@SomeAnnotation,但你可能只是在執行連接點感興趣。