2017-06-14 74 views
1

我有以下奇怪的行爲。@AfterThrowing in Spring different behavior

當我使用了一個名爲切入點的建議方法@AfterThrowing註解的方法的身體之前運行。但是如果我使用內聯切入點,@AfterThrowing註釋一個先運行。

爲什麼會這樣?

下面是代碼:

@Component 
@Aspect 
public class CustomAspect { 

    @AfterThrowing(pointcut = "execution(* throwAnException(..))", throwing = "exception") 
    public void adviceForExceptionThrowing(Exception exception) { 
     System.out.println("###### " + exception.getMessage() + " ######"); 
    } 

} 

結果:

INFO: Refreshing org.spring[email protected]5a10411: startup date [Wed Jun 14 15:51:26 EEST 2017]; root of context hierarchy 
###### Some message from the exception ###### 
Exception in thread "main" java.lang.Exception: Some message from the exception 

第二個結果是:

@Component 
@Aspect 
public class CustomAspect { 

    @Pointcut("execution(* throwAnException(..))") 
    private void pointcutForException() { 
    } 

    @AfterThrowing(pointcut = "pointcutForException()", throwing = "exception") 
    public void adviceForExceptionThrowing(Exception exception) { 
     System.out.println("###### " + exception.getMessage() + " ######"); 
    } 

} 

而我們得到:

INFO: Refreshing org.spring[email protected]5a10411: startup date [Wed Jun 14 15:54:38 EEST 2017]; root of context hierarchy 
Exception in thread "main" java.lang.Exception: Some message from the exception 
    at blog.codingideas.aspects.SomeBean.throwAnException(SomeBean.java:13) 
    at blog.codingideas.aspects.SomeBean$$FastClassBySpringCGLIB$$97c62a5f.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) 
    at blog.codingideas.aspects.SomeBean$$EnhancerBySpringCGLIB$$985c5826.throwAnException(<generated>) 
    at blog.codingideas.ApplicationMain.main(ApplicationMain.java:13) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
###### Some message from the exception ###### 

回答

1

您的代碼按預期工作。拋出異常後@AfterThrown通知已被執行。

棘手的是System.out.println。它在引擎蓋下調用PrintStream。這是一個緩衝流。緩衝的流在填滿之後被寫入輸出(控制檯),或者當您明確地調用flush時。

所以你的情況,這取決於內在動力堆多少填補了多少報表打印都能夠得到前其他線程打印異常堆棧跟蹤執行。

P.S.嘗試運行您的每個解決方案几次,並且您會發現 ###### Some message from the exception ######正在以主要堆棧跟蹤的前後順序進行打印。