一、Spring AOP环境搭建
1.1、坐标依赖引⼊
<!--Spring AOP--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency>
1.2、添加spring.xml的配置
添加命名空间
xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
二、注解实现
2..1、定义切面
/*** 切⾯* 切⼊点和通知的抽象 (与⾯向对象中的 类 相似)* 定义 切⼊点和通知 (切⼊点定义了要拦截哪些类的哪些⽅法,通知则定义了拦截过⽅法后要做什么)*/@Component // 将对象交给IOC容器去实例化@Aspect // 声明当前类是⼀个切⾯public class LogCut {/*** 切⼊点:* 匹配规则。规定什么⽅法被拦截、需要处理什么⽅法* 定义切⼊点* @Pointcut("匹配规则")** Aop 切⼊点表达式简介* 1. 执⾏任意公共⽅法:* execution(public *(..))* 2. 执⾏任意的set⽅法* execution(* set*(..))* 3. 执⾏com.xxxx.service包下任意类的任意⽅法* execution(* com.xxxx.service.*.*(..))* 4. 执⾏com.xxxx.service 包 以及⼦包下任意类的任意⽅法* execution(* com.xxxx.service..*.*(..))** 注:表达式中的第⼀个* 代表的是⽅法的修饰范围* 可选值:private、protected、public (* 表示所有范围)*/@Pointcut("execution (* com.xxxx.service..*.*(..) )")public void cut(){}/*** 声明前置通知 并将通知应⽤到定义的切⼊点上* ⽬标类⽅法执⾏前 执⾏该通知**/@Before(value = "cut()")public void before() {System.out.println("前置通知.....");}/*** 声明返回通知 并将通知应⽤到定义的切⼊点上* ⽬标类⽅法(⽆异常)执⾏后 执⾏该通知**/@AfterReturning(value = "cut()")public void afterReturn() {System.out.println("返回通知.....");}/*** 声明最终通知 并将通知应⽤到定义的切⼊点上* ⽬标类⽅法(⽆异常或有异常)执⾏后 执⾏该通知**/@After(value = "cut()")public void after() {System.out.println("最终通知.....");}/*** 声明异常通知 并将通知应⽤到定义的切⼊点上* ⽬标类⽅法出现异常时 执⾏该通知*/@AfterThrowing(value="cut()",throwing = "e")public void afterThrow(Exception e) {System.out.println("异常通知....." + " 异常原因:" + e.getCause());}/*** 声明环绕通知 并将通知应⽤到切⼊点上* ⽅法执⾏前后 通过环绕通知定义相应处理* 需要通过显式调⽤对应的⽅法,否则⽆法访问指定⽅法 (pjp.proceed();)* @param pjp* @return*/@Around(value = "cut()")public Object around(ProceedingJoinPoint pjp) {System.out.println("前置通知...");Object object = null;try {object = pjp.proceed();System.out.println(pjp.getTarget() + "======" + pjp.getSignature());// System.out.println("返回通知...");} catch (Throwable throwable) {throwable.printStackTrace();System.out.println("异常通知...");}System.out.println("最终通知...");return object;}}
2.2配置xml文件
<!--配置AOP代理--><aop:aspectj-autoproxy/><!--aop相关配置--><aop:config><!--aop切⾯--><aop:aspect ref="logCut02"><!-- 定义aop 切⼊点 --><aop:pointcut id="cut" expression="execution(* com.xxxx.service..*.*(..))"/><!-- 配置前置通知 指定前置通知⽅法名 并引⽤切⼊点定义 --><aop:before method="before" pointcut-ref="cut"/><!-- 配置返回通知 指定返回通知⽅法名 并引⽤切⼊点定义 --><aop:after-returning method="afterReturn" pointcut-ref="cut"/><!-- 配置异常通知 指定异常通知⽅法名 并引⽤切⼊点定义 --><aop:after-throwing method="afterThrow" throwing="e" pointcut-ref="cut"/><!-- 配置最终通知 指定最终通知⽅法名 并引⽤切⼊点定义 --><aop:after method="after" pointcut-ref="cut"/><!-- 配置环绕通知 指定环绕通知⽅法名 并引⽤切⼊点定义 --><aop:around method="around" pointcut-ref="cut"/></aop:aspect></aop:config>
三、Spring AOP总结
3.1、代理模式实现三要素
1. 接⼝定义
2. ⽬标对象与代理对象必须实现统⼀接⼝
3. 代理对象持有⽬标对象的引⽤ 增强⽬标对象⾏为
3.2、代理模式实现分类以及对应区别
1. 静态代理:⼿动为⽬标对象制作代理对象,即在程序编译阶段完成代理对象的创建
2. 动态代理:在程序运⾏期动态创建⽬标对象对应代理对象。
3. jdk动态代理:被代理⽬标对象必须实现某⼀或某⼀组接⼝ 实现⽅式 通过回调创建代理对象。
4. cglib 动态代理:被代理⽬标对象可以不必实现接⼝,继承的⽅式实现。动态代理相⽐较静态代理,提⾼开发效率,可以批量化创建代理,提⾼代码复⽤率。
3.3、Aop 理解
1. ⾯向切⾯,相⽐oop 关注的是代码中的层 或⾯
2. 解耦,提⾼系统扩展性
3. 提⾼代码复⽤
3.4、Aop 关键词
1. 连接点:每⼀个⽅法
2. 切⼊点:匹配的⽅法集合
3. 切⾯:连接点与切⼊点的集合决定了切⾯,横切关注点的抽象
4. 通知:⼏种通知
5. ⽬标对象:被代理对象
6. 织⼊:程序运⾏期将切⾯应⽤到⽬标对象 并⽣成代理对象的过程
7. 引⼊:在不修改原始代码情况下,在程序运⾏期为程序动态引⼊⽅法或字段的过程