JavaSpringAOP:将CustomizableTraceInterceptor与JavaConfig@enableSpectJautoproxy一起使用,而不是使用XML<;aop:advisor>;
SpringAOP有一个方法级跟踪程序,名为JavaSpringAOP:将CustomizableTraceInterceptor与JavaConfig@enableSpectJautoproxy一起使用,而不是使用XML<;aop:advisor>;,spring,aspectj,spring-aop,Spring,Aspectj,Spring Aop,SpringAOP有一个方法级跟踪程序,名为CustomizelTraceInterceptor。使用Spring的XML配置方法,可以这样设置跟踪程序: <bean id="customizableTraceInterceptor" class=" org.springframework.aop.interceptor.CustomizableTraceInterceptor"> <property name="enterMessage" value="Enterin
CustomizelTraceInterceptor
。使用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>
的@EnableAspectJAutoProxy
-样式等价物是什么?不幸的是,您不能这样做,因为Java语言不支持Spring JavaConfig中支持此功能所需的方法文本。为此打开了一个bug,但标记为“无法修复”:
错误报告中提到的两个选项是:
,方法是使用@ImportResource
元素转换为使用@Aspect
样式。[这在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());
}
}
只是想增加Adrence的反应。 我将使用点表达式引用聚合点,更清晰的分隔,imho
package org.example;
@Configuration
@EnableAspectJAutoProxy
@Aspect
public class AopConfiguration {
/** Pointcut for execution of methods on {@link Service} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Service org.example..*).*(..))")
public void serviceAnnotation() { }
/** Pointcut for execution of methods on {@link Repository} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Repository org.example..*).*(..))")
public void repositoryAnnotation() {}
/** Pointcut for execution of methods on {@link JpaRepository} interfaces */
@Pointcut("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))")
public void jpaRepository() {}
@Pointcut("serviceAnnotation() || repositoryAnnotation() || jpaRepository()")
public void performanceMonitor() {}
@Bean
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
return new PerformanceMonitorInterceptor(true);
}
@Bean
public Advisor performanceMonitorAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("org.example.AopConfiguration.performanceMonitor()");
return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor());
}
}
谢谢这应该被认为是正确答案。我使用上面的代码,但是
customabletraceinterceptor
似乎没有触发。我的拙见是,这应该被标记为正确答案,只是用Spring 4验证了一下。:)谢谢你们!我从早上开始就一直在寻找这个解决方案!))
package org.example;
@Configuration
@EnableAspectJAutoProxy
@Aspect
public class AopConfiguration {
/** Pointcut for execution of methods on {@link Service} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Service org.example..*).*(..))")
public void serviceAnnotation() { }
/** Pointcut for execution of methods on {@link Repository} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Repository org.example..*).*(..))")
public void repositoryAnnotation() {}
/** Pointcut for execution of methods on {@link JpaRepository} interfaces */
@Pointcut("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))")
public void jpaRepository() {}
@Pointcut("serviceAnnotation() || repositoryAnnotation() || jpaRepository()")
public void performanceMonitor() {}
@Bean
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
return new PerformanceMonitorInterceptor(true);
}
@Bean
public Advisor performanceMonitorAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("org.example.AopConfiguration.performanceMonitor()");
return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor());
}
}