【发布时间】:2014-04-04 02:54:15
【问题描述】:
根据 Spring 的文档Configuring AspectJ aspects using Spring IoC,为了配置 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 编写此内容?
【问题讨论】:
标签: java spring aop aspectj spring-java-config