一、AOP概念、作用
![](https://img-blog.csdnimg.cn/img_convert/aed71a23fefc50826ced7819e61745b2.png)
AOP和OOP一样都是一种编程思想,用来指导我们做程序的。
OOP面向对象编程指导我们做类、对象、继承、封装、多态等。
AOP面向切面编程作用:在不惊动原始设计(不改变源代码)的基础上为其进行功能增强。
核心:将每一个方法(这些方法称为连接点)共同具有的功能抽取出来单独实现一个方法(这个方法叫通知)。哪些方法要通过AOP增加功能的这些方法叫切入点。那么如何把通知和切入点联系起来呢?这就需要新的一个东西叫做切面,切面就是在哪些切入点上执行哪些通知,同时面向切面编程中的切面就是指的这个切面。
![](https://img-blog.csdnimg.cn/img_convert/2c43abda78046f442b4d7f1b6d68bcc5.png)
AOP核心概念:
![](https://img-blog.csdnimg.cn/img_convert/c0b6202ce26629984f223764407deb9f.png)
二、AOP入门案列
![](https://img-blog.csdnimg.cn/img_convert/16e2f0f9d1f79d33e1bbaf670f34d25a.png)
导入两个依赖包:aop和aspectj (aop包已经包含在org.springframework包中)
![](https://img-blog.csdnimg.cn/img_convert/bddda573b7de65641a00ebc2ac41b009.png)
![](https://img-blog.csdnimg.cn/img_convert/ff1915b3bbf924bbc4bac2bddb69ff17.png)
最重要:定义切入点
![](https://img-blog.csdnimg.cn/img_convert/cb8a48b18f98d5fa7a2f7450cb30879a.png)
详细步骤:
![](https://img-blog.csdnimg.cn/img_convert/d6b4237e1ea2ebe464f0d5e226a3c760.png)
![](https://img-blog.csdnimg.cn/img_convert/e9777fa2d04394965cd968f39c3e2769.png)
![](https://img-blog.csdnimg.cn/img_convert/40c003b04b16415543f1b9729159e53e.png)
![](https://img-blog.csdnimg.cn/img_convert/49bee86e6bb315b62a2b51cfd1292746.png)
![](https://img-blog.csdnimg.cn/img_convert/9b4f381c92003e7978238f8f582281be.png)
![](https://img-blog.csdnimg.cn/img_convert/f3ad681a27fa8ff83d26e3de54735198.png)
![](https://img-blog.csdnimg.cn/img_convert/27989424a390fd31bf4630ad30adc3e0.png)
三、AOP工作流程
![](https://img-blog.csdnimg.cn/img_convert/bdab01dc7c34949ae7d4a3a99eab13db.png)
![](https://img-blog.csdnimg.cn/img_convert/28f6a0332ecb19805f0db7e720da430f.png)
SpringAOP本质:代理模式
四、AOP切入点表达式
语法格式、通配符、书写技巧
![](https://img-blog.csdnimg.cn/img_convert/037047b01ff977dc44ef3c86effd7112.png)
![](https://img-blog.csdnimg.cn/img_convert/5f4b1611f644497d1708c417fd277330.png)
通配符描述切入点:
![](https://img-blog.csdnimg.cn/img_convert/4653be36b80a4b0889df50633eb694d7.png)
![](https://img-blog.csdnimg.cn/img_convert/44a5582098aad03742c3da36e3130153.png)
五、AOP通知类型
![](https://img-blog.csdnimg.cn/img_convert/d8096af8ac61efd6db910e484af5b232.png)
![](https://img-blog.csdnimg.cn/img_convert/1d267aafe31505cc900839a03dc00124.png)
![](https://img-blog.csdnimg.cn/img_convert/4b4b41123c79e04ccfb4ec6e56d5bc42.png)
![](https://img-blog.csdnimg.cn/img_convert/425c89b0c87e8e6540828a410a4c0c50.png)
![](https://img-blog.csdnimg.cn/img_convert/042ded3fa1b0145b9eb528b92d68cf93.png)
六、AOP通知获取数据
三种数据:原始操作的参数、返回值、异常
![](https://img-blog.csdnimg.cn/img_convert/209a4cdfbea26874c8d48318d61624ca.png)
![](https://img-blog.csdnimg.cn/img_convert/87ca3e1adb34a3dff0a8dcedb85dd245.png)
![](https://img-blog.csdnimg.cn/img_convert/efc46be80ef9d804186ce8c63f77bc9c.png)
![](https://img-blog.csdnimg.cn/img_convert/990fb1e114f474311aa7299d974ce51f.png)
案例:AOP通知获取参数并修改再返回到原始代码中:
![](https://img-blog.csdnimg.cn/img_convert/89b8edccf1455d21922aa1955db2f55c.png)
![](https://img-blog.csdnimg.cn/img_convert/c4973ac854404511deb8c832d4eb39e1.png)