@AfterThrowing Spring 不同的行为

@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.springframework.context.annotation.AnnotationConfigApplicationContext@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.springframework.context.annotation.AnnotationConfigApplicationContext@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$c62a5f.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$5c5826.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 ######

您的代码按预期工作。 @AfterThrown 通知在抛出异常后被执行。

棘手的是 System.out.println。它在后台调用 PrintStream。这是一个缓冲流。缓冲流在填满后或在您对其显式调用 flush 时写入输出(控制台)。

因此,在您的情况下,这取决于内部动态,即堆栈已填满多少以及在其他线程打印异常堆栈跟踪之前能够执行多少打印语句。

P.S。尝试 运行 你的每个解决方案几次,你会发现 ###### Some message from the exception ###### 以随机顺序在主堆栈跟踪之前和之后打印。