繁体   English   中英

Spring和AOP:@After工作但不是@AfterReturning

[英]Spring and AOP : @After works but not @AfterReturning

在webapp中,我使用Spring AOP检查传入呼叫的​​服务授权,并在返回结果时管理消息(信息,警告,错误)。 使用方面来做到这一点可以节省我的代码行并概括我的服务行为(它看起来很性感^^)。

所以我在我的应用程序上下文中有这种类型的conf

    <aop:aspectj-autoproxy />
    <bean id="authenticationCheckAspect" class="fr.test.server.business.aspect.AuthenticationCheckAspect" />

我的方面看起来像这样:

package fr.test.server.business.aspect;

@Aspect
public class AuthenticationCheckAspect {

    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationCheckAspect.class);

    @Autowired
    private AuthenticationBiz authBiz;

    /**
     * methodAnnotatedWithMyService Pointcut
     */
    @Pointcut("execution(@fr.test.server.business.aspect.MyService * *(..))")
    public void methodAnnotatedWithMyService() {
        // Méthode vide servant de Pointcut
    }

    @Before("methodAnnotatedWithMyService()")
    public void checkAuthentication(final JoinPoint joinPoint) throws FunctionalException {
        LOG.debug("checkAuthentication {}", joinPoint);

        {process...}
    }

    @AfterReturning(pointcut = "methodAnnotatedWithMyService()", returning = "result")
    public void manageErrors(final JoinPoint joinPoint, final Object result) {
        LOG.debug("Returning {}", joinPoint);
    }
}

在执行标记为@MyService任何方法之前,应该触发checkAuthentication()方法,它是:)这是一种解脱。

在执行标记为@MyService任何方法@MyService ,方法manageErrors也应该被触发,但它不会:(请注意,使用@After ,它可以工作但是我绝对需要我的@MyService注释方法的返回值,这就是为什么我需要@AfterReturning

由于我的@Before建议有效(当我尝试它时也是@After ),我想我没有代理类或类似的问题,否则什么都不会发生但是我真的不明白为什么我的@AfterReturning建议不是调用。

注意:执行呼叫时我没有收到任何错误。 只是我的@AfterReturning建议没有做任何事情:(

任何的想法 ? 谢谢 !

你的代码看起来不错。 我建议补充一下

@AfterThrowing(pointcut = "methodAnnotatedWithMyService()",  throwing="ex")
  public void doRecoveryActions( Exception e) {
    // Some code may be System.out.println 
    // or e.printStackTrace()
  }

并查看是否正在执行。

如果在pointcut methodAnnotatedWithMyService()抛出异常,则不会调用@AfterReturning ..但会调用@After ..

来自http://static.springsource.org/spring/docs/2.0.x/reference/aop.html

@AfterReturning建议在匹配的方法执行正常返回时运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM