2017-06-14 72 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 ######正在以主要堆栈跟踪的前后顺序进行打印。