Spring AOP没有调用Interceptor

时间:2017-12-01 01:03:10

标签: java spring aspectj

我是AspectJ的新手,在阅读了相关问题之后,我仍然坚持。

我想使用AspectJ来记录方法调用在指定包中运行所花费的时间。

我正在使用:<dependency <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.2.5</version> </dependency>

我已经包含以下其他Maven依赖项:

@Configuration
@EnableAspectJAutoProxy
@Aspect
public class PerformanceConfiguration {

    @Bean
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
        return new PerformanceMonitorInterceptor(true);
    }

    @Bean
    public Advisor QuoteServiceImplMonitorAdvisor(PerformanceMonitorInterceptor performanceMonitorInterceptor) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* com.mycompany.*.*(..))");
        return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor);
    }
}

我已经定义了一个配置类:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<property name="DEV_HOME" value="MyApplicationHome/logs" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/debug.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <logger name="log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor" level="TRACE">
        <appender-ref ref="STDOUT"/>
    </logger>

    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

这是我的logback.xml文件:

.push

我没有在控制台中看到任何跟踪条目,并且从不调用performanceMonitorInterceptor。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试将useDynamicLogger设置为false,新的PerformanceMonitorInterceptor(false)

答案 1 :(得分:-1)

为什么您的配置类使用@Aspect进行注释?

@Aspect应该在bean类本身上。

请参阅https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/aop.html#aop-at-aspectj