使用Spring IoC和JavaConfig配置AspectJ方面?

根据Spring的文档使用Spring IoC配置AspectJ方面以配置Spring IOC的方面,必须在xml配置中添加以下内容:

   

正如@SotiriosDelimanolis所建议的那样,在JavaConfig中将以下内容重写为:

 @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; } 

但是,如果Profiler方面是用native aspectj .aj语法编写的,这似乎只能起作用。 如果它是用Java编写的并使用@Aspect注释,则会收到以下错误消息:

对于类型Profiler,方法aspectOf()未定义

对于使用@AspectJ语法编写的方面,是否有使用JavaConfig编写此方法的等效方法?

事实certificate,有一个org.aspectj.lang.Aspects类专门用于此目的。 看来,LTW添加了aspectOf()方法,这就是为什么它在XML配置中工作正常,但在编译时却没有。

为了解决这个限制, org.aspectj.lang.Aspects提供了aspectOf()方法:

 @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; } 

希望这有助于将来的其他人。

是否有使用JavaConfig编写此方法的等效方法?

几乎总是。

 @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; } 

factory-method实例化中的文档中用静态工厂方法进行了解释