一、Spring AOP失效原因
(1)内部调用不会触发AOP;
(2)方法是private修饰的,AOP会失效; 解决方法:改成public
(3)目标类没有配置为bean,AOP会失效; 解决方法:将目标类配置为bean
(4)切入点表达式配置不正确,AOP也会失效; 解决方法:正确配置切入点
二、Spring AOP正常代理案例
2.1、pom
<dependencies><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.1</version></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.19</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version></dependency><!-- 普通maven项目中使用Sl4j注解 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.32</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.10</version></dependency></dependencies>
2.2、MySpringConfig
/*** @Author : 一叶浮萍归大海* @Date: 2023/10/29 16:30* @Description: 演示Spring AOP(正常情况)* @EnableAspectJAutoProxy的作用:开启Spring对AOP的支持* 属性:boolean proxyTargetClass() default false;* 作用:是否启用CGLIB动态代理:* true:启用* false:不启用* 说明:如果不设置的话,Spring会自行选择,当被代理对象实现了接口将会使用JDK动态代理,否则使用CHLIB动态代理* 属性:boolean exposeProxy() default false;* 作用:是否暴露当前代理对象到本地线程* true:暴露* false:不暴露*/
@Configuration
@ComponentScan(basePackages = "org.star")
@EnableAspectJAutoProxy
public class MySpringConfig {}
2.3、Calculator
/*** @Author : 一叶浮萍归大海* @Date: 2023/10/29 15:18* @Description: 计算器类*/
public interface Calculator {/*** 加* @param num1* @param num2* @return*/int add(Integer num1,Integer num2);/*** 减* @param num1* @param num2* @return*/int subtract(Integer num1,Integer num2);/*** 乘* @param num1* @param num2* @return*/int multiply(Integer num1,Integer num2);/*** 除* @param num1* @param num2* @return*/int divide(Integer num1,Integer num2);}
2.4、CalculatorImpl
/*** @Author : 一叶浮萍归大海* @Date: 2023/10/29 15:20* @Description: 目标类*/
@Component
@Slf4j
public class CalculatorImpl implements Calculator {@Overridepublic int add(Integer num1, Integer num2) {log.info("执行目标方法add,参数:{},{}", num1, num2);return num1 + num2;}@Overridepublic int subtract(Integer num1, Integer num2) {log.info("执行目标方法subtract,参数:{},{}", num1, num2);return num1 - num2;}@Overridepublic int multiply(Integer num1, Integer num2) {log.info("执行目标方法multiply,参数:{},{}", num1, num2);return num1 * num2;}@Overridepublic int divide(Integer num1, Integer num2) {log.info("执行目标方法divide,参数:{},{}", num1, num2);return num1 / num2;}
}
2.5、测试
@Slf4j
public class App {public static void main(String[] args) {m1();}/*** jdk动态代理案例一*/private static void m1() {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);Calculator calculator = context.getBean(Calculator.class);int result = calculator.add(3, 2);System.out.println(result);}}
三、Spring AOP内部调用导致AOP失效&解决方案(一)
3.1、失效
失效代码基本同上,修改的地方为 CalculatorImpl#add()
@Override public int add(Integer num1, Integer num2) {log.info("执行目标方法add,参数:{},{}", num1, num2);/*** 演示Spring AOP内部调用导致AOP失效* int subtractResult = subtract(num1, num2);* System.out.println("subtractResult = " + subtractResult);*/int subtractResult = subtract(num1, num2);System.out.println("subtractResult = " + subtractResult);return num1 + num2; }
3.1.1、测试
3.2、解决
解决也很简单,代码基本同上,修改内容如下
3.2.1、测试
四、Spring AOP内部调用导致AOP失效&解决方案(二)
4.1、解决
代码基本同2.xxx、修改内容如下:
4.1.1、MySpringConfig