Java Spring AOP:使用CustomizableTraceInterceptor和JavaConfig @EnableAspectJAutoProxy,而不是XML <aop:advisor> </aop:advisor>

时间:2012-11-01 17:16:14

标签: spring aspectj spring-aop

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的{​​{1}} - 等效格式是什么?

3 个答案:

答案 0 :(得分:23)

我是这样做的:

@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());
    }

}

答案 1 :(得分:16)

只是想加入AdrienC的回复。 我将使用点表达式来引用聚合点,更清晰的分离,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());
    }
}

答案 2 :(得分:4)

不幸的是,你不能,因为Java语言不支持在Spring JavaConfig中支持它的方法文字。为此打开了一个错误,但标记为“无法修复”:https://jira.springsource.org/browse/SPR-8148

错误报告中提到的两个选项是:

  1. 使用<aop:config>
  2. 包含相关的XML代码段,继续使用@ImportResource
  3. 将任何现有的<aop:config>元素转换为使用@Aspect样式。 [CustomizableTraceInterceptor]无法使用