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注解,特别是@EnableAspectJAutoProxy
来激活JavaConfig中的AspectJ )。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
<aop:advisor advice-ref="customizableTraceInterceptor" ...>
的@EnableAspectJAutoProxy
-style等价物是什么
发布于 2014-02-21 15:44:07
我这样做:
@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());
}
}
发布于 2015-03-12 19:47:34
只是想补充一下AdrienC的回应。我将使用point表达式来引用聚合点,更清晰的分离,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());
}
}
https://stackoverflow.com/questions/13182205
复制相似问题