AOP(面向切面编程)的核心概念是"切面"。
切面是一个跨越多个对象的类,它封装了横切关注点的具体实现。通过定义切面,开发人员可以将通用功能从业务逻辑中分离出来,形成独立的模块。在Spring AOP中,切面由切点(Pointcut)、通知(Advice)和连接点(Joinpoint)组成。切点用于定义哪些连接点上应该应用通知,而通知则是在连接点处执行的代码。
AOP通过将横切关注点与主业务逻辑进行解耦,实现了关注点的模块化和可重用性。这种编程范式提高了代码的组织性、可维护性和可扩展性,使得开发人员可以更容易地管理和维护复杂的软件系统。
试用场景:
有100个类,希望给这个100个类执行前,执行后都加入日志,记录执行的开始时间和结束时间,使用切面写一个类就可以搞定,不用把写日志的操作在100个方法里都写一遍。如下看如何实现:
一、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二、工程结构

三、启动类

四、切面类

五、方法类

六、测试类

七、测试运行
1、指定切面类应用的对象所在的包
(1)execution(* com.example.service.*.*(..))
(2)完整的切面类 如下:

(3)执行的具体方法

(4)执行

(5)输出

2、修改前置方法指定的类
(1)切面类修改

(2)另一个类中的方法

(3)分别运行如下两个测试方法

(4)输出

3、修改前置方法指定的方法
(1)指定到方法

(2)分别运行如下两个测试方法

(3)结果:第二个方法缺少前置输出,说明切面的前置输出只对第一个方法起作用

源码获取方式(免费):
(1)登录-注册:http://resources.kittytiger.cn/
(2)搜索:springboot-AOP切面编程的demo