簡體   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