3

Web アプリケーションでは、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、メソッド manageErrors もトリガーされるはずですが、トリガーされないことに注意してください:(で動作しますが、注釈付きメソッド@Afterの戻り値が絶対に必要なため、@MyService@AfterReturning

私の@Beforeアドバイスが機能するので(そして@After私が試したときも)、プロキシされたクラスなどの問題はないと思います。それ以外の場合は何も起こりませんが、なぜ私の@AfterReturningアドバイスが呼び出されないのか本当にわかりません。

注:呼び出しの実行中にエラーは発生しません。私の@AfterReturningアドバイスでは何も行われていないだけです:(

何か案が ?どうも !

4

1 に答える 1

4

あなたのコードはよさそうです。追加することをお勧めします

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

これが実行されているかどうかを確認します。

ポイントカット内で例外がスローされた場合、 はmethodAnnotatedWithMyService()呼び出さ@AfterReturningれませんが、 a@Afterが呼び出されます。

http://static.springsource.org/spring/docs/2.0.x/reference/aop.htmlから

@AfterReturning アドバイスは、一致したメソッドの実行が正常に戻ったときに実行されます

于 2013-05-13T13:08:38.663 に答える