微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用Spring IoC和JavaConfig配置AspectJ方面?

事实证明,有一个org.aspectj.lang.Aspects专门为此目的提供的类。看来该aspectOf()方法是由LTW添加的,这就是为什么它在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;
}

希望这对以后的人有所帮助。

解决方法

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

<bean id="profiler" class="com.xyz.profiler.Profiler"
      factory-method="aspectOf">
  <property name="profilingStrategy" ref="jamonProfilingStrategy"/>
</bean>

正如@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在以本机AspectJ
.aj语法编写方面时才起作用。如果它是用Java编写并带有注释的@Aspect,则会收到以下错误消息:

未为Profiler类型定义方法AspectOf()

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。