【问题标题】:Java Spring AOP: Using CustomizableTraceInterceptor with JavaConfig @EnableAspectJAutoProxy, not XML <aop:advisor>Java Spring AOP:将 CustomizableTraceInterceptor 与 JavaConfig @EnableAspectJAutoProxy 一起使用,而不是 XML <aop:advisor>
【发布时间】:2012-11-01 17:16:14
【问题描述】:

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 样式对应的 &lt;aop:advisor advice-ref="customizableTraceInterceptor" ...&gt; 是什么?

【问题讨论】:

    标签: spring aspectj spring-aop


    【解决方案1】:

    我是这样做的:

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

    【讨论】:

    • 谢谢!这应该被接受为正确答案。
    • 我使用上面的这段代码,但CustomizableTraceInterceptor 似乎没有触发
    【解决方案2】:

    只是想补充一下 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());
        }
    }
    

    【讨论】:

    • 依我拙见,这应该被标记为正确答案,刚刚用 Spring 4 验证。:)
    • 谢谢各位!从早上开始我就一直在寻找这个解决方案!))
    【解决方案3】:

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

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

    1. 通过使用@ImportResource 包含相关的XML sn-p 继续使用&lt;aop:config&gt;
    2. 将任何现有的&lt;aop:config&gt; 元素转换为使用@Aspect 样式。 [CustomizableTraceInterceptor 无法做到这一点]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多