# SpringBoot整合AOP-Demo **Repository Path**: xsyzer/springbootAOP ## Basic Information - **Project Name**: SpringBoot整合AOP-Demo - **Description**: SpringBoot整合AOP的Demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-12-28 - **Last Updated**: 2020-12-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AOP 1. **依赖** ```xml org.springframework.boot spring-boot-starter-aop ``` 2. **底层原理** 两种动态代理: 1. **有接口** 使用JDK动态代理 - 创建接口实现类代理对象,增强类的方法 ![image-20201227111601714](https://gitee.com/xsyzer/image_01/raw/master/img/20201227111609.png) 2. **无接口**,使用CGlib ![image-20201227111957342](https://gitee.com/xsyzer/image_01/raw/master/img/20201227111959.png) 3. **术语** 连接点:一个类中哪些方法可以被增强,这些方法成为连接点 切入点:实际上被真正增强的方法,成为切入点 通知: - 实际增强的逻辑部分称为通知(增强) - 通知类型 (前置,后置,环绕,异常,最终) 切面:是动作,把通知应用到切入点的过程 4. **切入点表达式** 1. 作用:知道对哪个类的哪个方法进行增强 ]]语法结构(基于`AspectJ`): > execution(【权限修饰符】,【返回类型】,【类全路径】,(【参数列表】)) 2. 举例: ![image-20201227161706947](https://gitee.com/xsyzer/image_01/raw/master/img/20201227161708.png) ![image-20201227161735168](https://gitee.com/xsyzer/image_01/raw/master/img/20201227161737.png) 5. **通知的配置** (1)在spring配置文件中,开启注解扫描 (2)使用注解创建User和UserProxy对象 (3)在增强类上面添加注解@Aspect (4)在spring配置文件中开启生成代理对象 6. **通知的种类** ```java ***/相同切入点抽取*** @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void pointdemo() { } **//前置通知** //@Before注解表示作为前置通知 @Before(value = "pointdemo()")//相同切入点抽取使用! **//后置通知(返回通知)** @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") **//最终通知** @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") **//异常通知** @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") **//环绕通知** @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕之前........."); *//被增强的方法执行* proceedingJoinPoint.proceed(); System.out.println("环绕之后........."); } ``` 7. **通知类的优先级** 在类的前面加上`@order(0)`,括号里的数字越小,表示优先级越高,可以填写0,1,2,3,4,5...