Spring Aspect Logger

被刻印的时光 ゝ 提交于 2019-12-13 07:40:42

问题


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
    {

    }

}

回答1:


I guess us use Springs Proxy Based AOP (you did not post your configuration so I have to guess).

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)

There are two solutions:

  • 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:

example:

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(...){...}
}

I prefer the AspectJ way, because when using the self reference way, one could forget to use it



来源:https://stackoverflow.com/questions/28165943/spring-aspect-logger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!