0

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
    {

    }

}
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • I think if createUser() is not in RegisterServices class, it would be invoked – Ahmet Karakaya Jan 27 '15 at 08:59
  • from the code you posted it seems the call to `createUser` comes within the `RegisterServices` thus it is simple API call. However as you have already noted in comments `createUser` is invoked from another spring manged bean it will kick the aspect `@LogPerformance` – Bond - Java Bond Jan 27 '15 at 09:44

1 Answers1

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:

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

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • yes that is the one way of my code working properly. But in that case wvery similar code block have to be changed so it means additonal code writing overhead. – Ahmet Karakaya Jan 27 '15 at 10:58