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建议没有做任何事情:(

任何想法 ? 谢谢 !

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. 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

<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.

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.

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.

NOTE : I don't get any error while executing a call. It's just that nothing is done on my @AfterReturning advice :(

Any idea ? Thx !

最满意答案

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

@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建议在匹配的方法执行正常返回时运行

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..

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

@AfterReturning advice runs when a matched method execution returns normally

更多推荐

@AfterReturning,@MyService,@After,class,电脑培训,计算机培训,IT培训"/> <meta