20

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-style 等价物<aop:advisor advice-ref="customizableTraceInterceptor" ...>

4

3 回答 3

29

我这样做:

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

}
于 2014-02-21T07:44:06.603 回答
17

只是想补充一下 AdrienC 的回应。我将使用点表达式来引用聚合点,更清晰的分离,恕我直言

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());
    }
}
于 2015-03-12T11:47:33.560 回答
4

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

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

  1. <aop:config>通过包含相关的 XML 片段继续使用@ImportResource
  2. 将任何现有<aop:config>元素转换为使用@Aspect样式。[这是不可能的CustomizableTraceInterceptor]
于 2012-11-10T21:12:44.450 回答