在Spring Boot中引入AOP就跟引入其他模块一样,非常简单,只需要在pom.xml
中加入如下依赖:
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
在完成了引入AOP依赖包后,一般来说并不需要去做其他配置
而当我们需要使用CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true
,不然默认使用的是标准Java的实现,就会报错。错误信息如下:
***************************
APPLICATION FAILED TO START
***************************
Description::
The bean 'jdbcTemplate' could not be injected as a 'org.springframework.jdbc.core.JdbcTemplate' because it is a JDK dynamic proxy that implements:
org.springframework.jdbc.core.JdbcOperations
Action::
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
解决方法:在yml文件中加入
spring:
aop:
proxy-target-class: true