Spring AOP 有一个名为 CustomizableTraceInterceptor
的方法级跟踪器。 .使用 Spring 的 XML 配置方法,可以像这样设置这个跟踪器:
<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)。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
什么是@EnableAspectJAutoProxy
- 等效于 <aop:advisor advice-ref="customizableTraceInterceptor" ...>
的样式?
最佳答案
我是这样做的:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class TraceLoggerConfig {
@Bean
public CustomizableTraceInterceptor customizableTraceInterceptor() {
CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor();
customizableTraceInterceptor.setUseDynamicLogger(true);
customizableTraceInterceptor.setEnterMessage("Entering $[methodName]($[arguments])");
customizableTraceInterceptor.setExitMessage("Leaving $[methodName](), returned $[returnValue]");
return customizableTraceInterceptor;
}
@Bean
public Advisor jpaRepositoryAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))");
return new DefaultPointcutAdvisor(pointcut, customizableTraceInterceptor());
}
}
关于spring - Java Spring AOP : Using CustomizableTraceInterceptor with JavaConfig @EnableAspectJAutoProxy, 不是 XML <aop:advisor>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182205/