2013-08-02 43 views
24

比方說,你有三個建議:各地之前和後。AspectJ的「左右」和「前進」與「前/後」

1)前/後調用時進行周圍建議被調用, 或者是他們叫前/後周圍建議作爲一個整體?

2)如果我各地意見不叫進行, 會把前/後建議無論如何運行?

+0

我會想象這是容易測試的;有沒有一個特定的問題阻止你這樣做? –

+0

@DaveNewton嗯,我不是Java開發人員,也沒有可用的環境。我用另一種語言移植AOP範例,並查看現有工具的文檔。我認爲最好問問那裏的專家,但如果沒有人能夠幫助我,這就是我最後要做的。 –

回答

35

與這個測試

@Aspect 
public class TestAspect { 
    private static boolean runAround = true; 

    public static void main(String[] args) { 
     new TestAspect().hello(); 
     runAround = false; 
     new TestAspect().hello(); 
    } 

    public void hello() { 
     System.err.println("in hello"); 
    } 

    @After("execution(void aspects.TestAspect.hello())") 
    public void afterHello(JoinPoint joinPoint) { 
     System.err.println("after " + joinPoint); 
    } 

    @Around("execution(void aspects.TestAspect.hello())") 
    public void aroundHello(ProceedingJoinPoint joinPoint) throws Throwable { 
     System.err.println("in around before " + joinPoint); 
     if (runAround) { 
      joinPoint.proceed(); 
     } 
     System.err.println("in around after " + joinPoint); 
    } 

    @Before("execution(void aspects.TestAspect.hello())") 
    public void beforeHello(JoinPoint joinPoint) { 
     System.err.println("before " + joinPoint); 
    } 
} 

我具有以下執行之前在圍繞輸出

  1. (無效aspects.TestAspect.hello())
  2. 執行前(無效aspects.TestAspect.hello ())
  3. in hello
  4. 執行後(void aspects.TestAspect.hello())
  5. 在周圍執行後(無效aspects.TestAspect.hello())
  6. 在周圍執行前(無效aspects.TestAspect.hello())
  7. 在周圍執行後(無效aspects.TestAspect.hello() )

所以你可以看到前/後不叫

+0

Thx爲您的答案。它真的幫了大忙! –

+0

非常感謝!!!!! – IloveIniesta