Spring Aspect记录器 [英] Spring Aspect Logger
问题描述
我一直在创建基于注释的方面定义,因此创建@LogPerformance
并将其放在createuser()
方法上.在这种情况下,它不会调用Aspect方法.但是,当我将@LogPerformance从createuser()
移到create()方法时,将调用Aspect方法.
为什么@LogPerformance
不会影响createuser方法.
I have been creating a Annotation based aspect definition thus create @LogPerformance
and put it on createuser()
method. In that case it does not call the aspect method.But when I have moved @LogPerformance from createuser()
to create() method aspect method is invoked.
Why @LogPerformance
does not effect on createuser method.
@Component
@Path(SystemConstants.REST_REGISTER)
public class RegisterServices {
@PUT
@Path(SystemConstants.REST_REGISTER_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
public Response create(@Context HttpServletRequest requestContex) String requestIp, String param) {
createUser(...);
}
@LogPerformance
public ClientRespWsBean createUser(ClientReqWsBean request) throws XMPPException
{
}
}
推荐答案
我猜我们使用基于Springs Proxy的AOP(您没有发布配置,所以我不得不猜测).
I guess us use Springs Proxy Based AOP (you did not post your configuration so I have to guess).
仅当直接从另一个bean调用建议方法时,此基于代理的AOP才起作用(因为随后也调用了代理).但是,当您从同一个bean中(通过this
)调用建议方法时,则不会调用代理,因此不会执行方面. (@see 春季参考,第9.6.1章了解AOP代理)
This Proxy Based AOP works only when the advised method is invoked directly from an other bean (because then the proxy is invoked too). But when you invoke the advised method from within the same bean (via this
) then the proxy is not invoked and therefore the aspect is not executed. (@see Spring Reference, Chapter 9.6.1 Understanding AOP proxies)
有两种解决方案:
- use real AspectJ (@see: Spring Reference, Chapter 9.8 Using AspectJ with Spring applications)
- inject an instance of
RegisterServices
to the service itself and then use it instead of this:
示例:
public class RegisterServices {
/*
* You must use @Resource instead of @Autowire
* https://jira.spring.io/browse/SPR-8450
* (and of course you need to enable @Resourse support first)
*/
@Resource private RegisterServices self; //self reference with proxy
...
public Response create(...) {
this.self.createUser(...);
}
@LogPerformance
public ClientRespWsBean createUser(...){...}
}
我更喜欢AspectJ方式,因为使用自引用方式时,人们可能会忘记使用它.
这篇关于Spring Aspect记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!