Java Spring AOP:将CustomizableTraceInterceptor与JavaConfig @EnableAspectJAutoProxy一起使用,而不是XML< aop:advisor> [英] Java Spring AOP: Using CustomizableTraceInterceptor with JavaConfig @EnableAspectJAutoProxy, not XML <aop:advisor>
问题描述
Spring AOP有一个称为CustomizableTraceInterceptor
的方法级跟踪器.使用Spring的XML配置方法,可以像下面这样设置此跟踪器:
Spring AOP has a method-level tracer called CustomizableTraceInterceptor
. Using Spring's XML configuration approach, one would set up this tracer like so:
<bean id="customizableTraceInterceptor" class="
org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/>
</aop:config>
我想使用Spring的JavaConfig样式设置上述配置(即利用Java批注,尤其是@EnableAspectJAutoProxy
来激活JavaConfig中的AspectJ).
I would like to set up above configuration using Spring's JavaConfig style (i.e. taking advantage of Java annotations, especially @EnableAspectJAutoProxy
for activating AspectJ in JavaConfig).
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
<aop:advisor advice-ref="customizableTraceInterceptor" ...>
的@EnableAspectJAutoProxy
样式等效于什么?
推荐答案
不幸的是,您不能这样做,因为Java语言不支持在Spring JavaConfig中支持该方法的文字.为此打开了一个错误,但将其标记为无法修复": https://jira.springsource .org/browse/SPR-8148 .
Unfortunately, you cannot because the Java language does not support method literals which would be needed to support this in Spring JavaConfig. A bug was opened for this but marked as "Won't Fix": https://jira.springsource.org/browse/SPR-8148.
错误报告中提到的两个选项是:
The two options mentioned in the bug report are:
- 通过使用
@ImportResource
包含相关的XML代码段,继续使用 - 将任何现有的
<aop:config>
元素转换为使用@Aspect
样式. [这是CustomizableTraceInterceptor
不可能的
<aop:config>
- Continue using
<aop:config>
by including the relevant XML snippet using@ImportResource
- Convert any existing
<aop:config>
elemements to use@Aspect
style. [which is not possible with theCustomizableTraceInterceptor
]
这篇关于Java Spring AOP:将CustomizableTraceInterceptor与JavaConfig @EnableAspectJAutoProxy一起使用,而不是XML< aop:advisor>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!