Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Spring Aspect Logger

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
    {

    }

}

Upvotes: 0

Views: 756

Answers (1)

Ralph
Ralph

Reputation: 120771

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:

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

Upvotes: 1

Related Questions