[英]Spring and AOP : @After works but not @AfterReturning
In a webapp, I use Spring AOP to check authorisations for my services on incoming calls and to manage messages (info, warn, error) on returning results. 在webapp中,我使用Spring AOP检查传入呼叫的服务授权,并在返回结果时管理消息(信息,警告,错误)。 Using an aspect to do that saves me lines of code and generalizes the behaviour of my services (and it looks sexy ^^). 使用方面来做到这一点可以节省我的代码行并概括我的服务行为(它看起来很性感^^)。
So I have this type of conf in my app context 所以我在我的应用程序上下文中有这种类型的conf
<aop:aspectj-autoproxy />
<bean id="authenticationCheckAspect" class="fr.test.server.business.aspect.AuthenticationCheckAspect" />
And my aspect looks like that : 我的方面看起来像这样:
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);
}
}
Before executing any method tagged @MyService
, the method checkAuthentication()
is supposed to be triggered and it is :) That's a relief. 在执行标记为@MyService
任何方法之前,应该触发checkAuthentication()
方法,它是:)这是一种解脱。
After executing any method tagged @MyService
, the method manageErrors is supposed to be triggered as well but it doesn't :( Note that with @After
, it works BUT I absolutely need the return value of my @MyService
annotated method and that's why I need @AfterReturning
. 在执行标记为@MyService
任何方法@MyService
,方法manageErrors也应该被触发,但它不会:(请注意,使用@After
,它可以工作但是我绝对需要我的@MyService
注释方法的返回值,这就是为什么我需要@AfterReturning
。
As my @Before
advice works (and @After
too when I tried it), I imagine I don't have a problem of proxied class or something like that, nothing would happen otherwise but I really dont understand why my @AfterReturning
advice is not called. 由于我的@Before
建议有效(当我尝试它时也是@After
),我想我没有代理类或类似的问题,否则什么都不会发生但是我真的不明白为什么我的@AfterReturning
建议不是调用。
NOTE : I don't get any error while executing a call. 注意:执行呼叫时我没有收到任何错误。 It's just that nothing is done on my @AfterReturning
advice :( 只是我的@AfterReturning
建议没有做任何事情:(
Any idea ? 任何的想法 ? Thx ! 谢谢 !
Your code looks good. 你的代码看起来不错。 I will suggest to add 我建议补充一下
@AfterThrowing(pointcut = "methodAnnotatedWithMyService()", throwing="ex")
public void doRecoveryActions( Exception e) {
// Some code may be System.out.println
// or e.printStackTrace()
}
and see if this is being executed. 并查看是否正在执行。
if there is a exception thrown within pointcut methodAnnotatedWithMyService()
then @AfterReturning
will not be called.. but a @After
will be called.. 如果在pointcut methodAnnotatedWithMyService()
抛出异常,则不会调用@AfterReturning
..但会调用@After
..
from http://static.springsource.org/spring/docs/2.0.x/reference/aop.html 来自http://static.springsource.org/spring/docs/2.0.x/reference/aop.html
@AfterReturning advice runs when a matched method execution returns normally @AfterReturning建议在匹配的方法执行正常返回时运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.