java - Spring 无法获取 AOP 配置 bean
问题描述
错误
Bean 'aopConfiguration' of type [example.config.AopConfiguration$$EnhancerBySpringCGLIB$$5e48431d] is not eligible for getting processed by all BeanPostProcessors
以下 bean 失败:
- aop配置
- 性能监视器拦截器
- 性能监控顾问
配置
@Aspect
@EnableAspectJAutoProxy
@Configuration
public class AopConfiguration {
@Pointcut("execution(* example.service.TimestampService.findAllTimestamps(..))")
public void monitor() {
}
@Bean
public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
return new PerformanceMonitorInterceptor(false);
}
@Bean
public Advisor performanceMonitorAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("example.config.AopConfiguration.monitor()");
return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor());
}
}
应用程序.yml
logging:
file:
name: log.log
pattern:
console: "%d{HH:mm:ss.SSS} %msg%n"
level:
org.springframework.web: INFO
example.config.PerformanceMonitorInterceptor: DEBUG
example: DEBUG
基本上没有日志记录或跟踪信息被放入控制台。
pom中的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://stackoverflow.com/questions/6919498/java-throttling-->
<dependency>
<groupId>org.isomorphism</groupId>
<artifactId>token-bucket</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.maven.plugin.version}</version>
</dependency>
<!-- logging-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<!-- integration test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</dependency>
<!-- build -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
解决方案
一个方面不应该是它自己的配置类,因为配置是针对整个应用程序的。使用单独的类 for@Configuration
并让 the @Aspect
just 成为一个方面(也使方面成为 a @Component
)。在幕后,配置代理的工作方式与 Spring 中的 AOP 代理不同。如果您对技术细节感兴趣,请在此处阅读我的回答。
推荐阅读
- angular - ngx-permissions formbuilder 禁用输入
- php - Moodle 数据表
- python - 如何为每年三个班级的百分比制作堆积条形图?
- python - 如何创建一个持续接受用户输入并评估所有输入的程序
- windows - 通过管道将 CMD 输出传递给 Powershell
- user-interface - 关闭除 tkinter 中的启动窗口以外的所有窗口
- python - 如何使用if条件合并两个数据框
- excel - Active Directory 使用 powershell 获取所有用户的登录时间(当他们被允许登录时)
- javascript - 在浏览器中重新加载页面后,如何验证 Bot Framework 作曲家聊天机器人不会再次打招呼?
- javascript - Todo 不会在 React 应用程序中切换为已完成/未完成