• zhs
    2021-04-20
    spring-5.2.7开始,执行顺序有了变化。https://docs.spring.io/spring-framework/docs/5.2.7.RELEASE/spring-framework-reference/core.html#aop-ataspectj-advice-ordering

    作者回复: 感谢补充~

    
    
  • sljoai
    2021-03-16
    老师,请问一下: @AfterReturing与@AfterThrowing不能同时执行嘛?

    作者回复: 对于同一个 JoinPoint 方法而言,两者不会同时出现

    
    
  • J.Smile
    2021-01-16
    马哥,为啥我这里的执行结果是@AfterReturing先于@After呢?😂 @Aspect public class AspectConfiguration { //这里只是个是否匹配的判断,不会有任何的执行动作,所以方法里的打印根本就不会被执行 @Pointcut("execution(public * *(..))") private void anypublicMethod() { System.out.println("@Pointcut at any public method"); } //Join Pint的拦截动作 @Before("anypublicMethod()") private void beforeAnyPublicMethod() { System.out.println("@Before any public method."); } //Join Pint的拦截动作 @Around("anypublicMethod()") private Object aroundAnyPublicMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("@Around any public method."); return proceedingJoinPoint.proceed(); } /** * After注解对应Finally阶段 * * @throws Throwable */ @After("anypublicMethod()") private void finalizedAnyPublicMethod() throws Throwable { System.out.println("@After any public method."); } /** * AfterReturning注解对应方法返回后阶段 * * @throws Throwable */ //Join Pint的拦截动作 @AfterReturning("anypublicMethod()") private void afterReturingAnyPublicMethod() throws Throwable { System.out.println("@AfterReturing any public method."); } /** * AfterThrowing对应异常抛出阶段 * * @throws Throwable */ @AfterThrowing("anypublicMethod()") private void afterThrowingAnyPublicMethod() throws Throwable { System.out.println("@AfterThrowing any public method."); } }
    展开

    作者回复: 这个可能是由于反射方法数据返回时,不稳定造成的,参考一下 Class#getMethods() 方法的注释

    
    
  • HashMap
    2021-01-25
    小马哥,为什么我在 @Before 和 @Around 里抛出异常都不会触发@AfterThrowing @Aspect @Order public class AspectConfiguration { @Pointcut("execution(public * *(..))") private void anyPublicMethod() { System.out.println("@Pointcut any public method."); } @Around("anyPublicMethod()") public Object aroundAnyPublicMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("@Around any public method"); return pjp.proceed(); } @Before("anyPublicMethod()") public void beforeAnyPublicMethod() throws Throwable { Random random = new Random(); if (random.nextBoolean() || random.nextBoolean()) { throw new RuntimeException("For purpose"); } System.out.println("@Before any public method"); } @After("anyPublicMethod()") public void finalizeAnyPublicMethod() { System.out.println("@After any public method"); } @AfterReturning("anyPublicMethod()") public void afterAnyPublicMethod() { System.out.println("@AfterReturning any public method"); } @AfterThrowing("anyPublicMethod()") public void afterThrowingAnyPublicMethod() { System.out.println("@AfterThrowing any public method"); } }
    展开
    
    