Spring 方面记录器

Spring Aspect Logger

我一直在创建基于注释的方面定义,因此创建 @LogPerformance 并将其放在 createuser() 方法中。在那种情况下,当我将 @LogPerformance 从 createuser() 移动到调用 create() 方法方面方法时,它不会调用方面 method.But。 为什么 @LogPerformance 不影响 createuser 方法。

@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(您没有 post 您的配置,所以我不得不猜测)。

此基于代理的 AOP 仅在直接从其他 bean 调用建议的方法时起作用(因为代理也会被调用)。但是,当您从同一个 bean 中调用建议的方法时(通过 this),则不会调用代理,因此不会执行方面。 (@see Spring Reference, Chapter 9.6.1 Understanding AOP proxies)

有两种解决方案:

示例:

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的方式,因为使用self reference方式的时候,容易忘记使用