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

Abd*_*ull 17 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>
Run Code Online (Sandbox Code Playgroud)

我想使用Spring的JavaConfig样式设置上述配置(即利用Java注释,尤其是@EnableAspectJAutoProxy在JavaConfig中激活AspectJ).

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {

    @Bean someBean() {
    ...
    }
...
}
Run Code Online (Sandbox Code Playgroud)

什么是@EnableAspectJAutoProxy风格的等价物<aop:advisor advice-ref="customizableTraceInterceptor" ...>

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

}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这应该被接受为正确的答案. (3认同)
  • 我使用上面的代码,但“CustomizedTraceInterceptor”似乎没有触发 (2认同)

小智 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());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在我的拙见中,这应该被标记为正确答案,只需在Spring 4中验证.:) (2认同)

pra*_*dym 4

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

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

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