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
アドバイスでは何も行われていないだけです:(
何か案が ?どうも !