Java Spring AOP: Using CustomizableTraceInterceptor with JavaConfig @EnableAspectJAutoProxy, not XML

前端 未结 3 1720
醉话见心
醉话见心 2020-12-24 09:17

Spring AOP has a method-level tracer called CustomizableTraceInterceptor. Using Spring\'s XML configuration approach, one would set up this tracer like so:

相关标签:
3条回答
  • 2020-12-24 09:28

    Just wanted to add to AdrienC's response. I'll use the point expression to reference an aggregated point, more clearer separation, 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());
        }
    }
    
    0 讨论(0)
  • 2020-12-24 09:39

    I do it this way :

    @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());
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 09:44

    Unfortunately, you cannot because the Java language does not support method literals which would be needed to support this in Spring JavaConfig. A bug was opened for this but marked as "Won't Fix": https://jira.springsource.org/browse/SPR-8148.

    The two options mentioned in the bug report are:

    1. Continue using <aop:config> by including the relevant XML snippet using @ImportResource
    2. Convert any existing <aop:config> elemements to use @Aspect style. [which is not possible with the CustomizableTraceInterceptor]
    0 讨论(0)
提交回复
热议问题