Configuring AspectJ aspects using Spring IoC with JavaConfig?

前端 未结 2 1186
無奈伤痛
無奈伤痛 2020-12-04 01:59

According to Spring\'s Documentation Configuring AspectJ aspects using Spring IoC in order to configure an aspect for Spring IOC, the following has to be added to the xml co

相关标签:
2条回答
  • 2020-12-04 02:33

    Turns out that there is an org.aspectj.lang.Aspects class to provide for specifically this purpose. It appears that the aspectOf() method is added by the LTW which is why it works fine in XML configuration, but not at compile time.

    To get around this limitation, org.aspectj.lang.Aspects provides a aspectOf() method:

    @Bean
    public com.xyz.profiler.Profiler profiler() {
        com.xyz.profiler.Profiler profiler = Aspects.aspectOf(com.xyz.profiler.Profiler.class);
        profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean
        return profiler;
    }
    

    Hope this helps someone else in the future.

    0 讨论(0)
  • 2020-12-04 02:36

    Is there an equivalent way of writing this using JavaConfig?

    Almost always.

    @Bean
    public com.xyz.profiler.Profiler profiler() {
        com.xyz.profiler.Profiler profiler = com.xyz.profiler.Profiler.aspectOf();
        profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean
        return profiler;
    }
    

    The factory-method is explained in the documentation in Instantiation with a static factory method.

    0 讨论(0)
提交回复
热议问题