03-Spring-AOP
1. SpringAOP
-
什么是 AOP
面向切面编程
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高代码的可重用性,同时提高开发的效率。
通俗表达:在不改变源代码的情况下,在原功能上添加新的功能
主要的实现:日志记录、安全控制、事务处理、异常处理等等
2. 底层原理
-
AOP底层使用动态代理 实现
-
第一种 有接口:使用JDK动态代理
1.创建当前接口实现类的代理对象
2.通过代理对象来增强实现类的方法
-
第二种 无接口:使用CGLIB动态代理
1.创建当前类子类的代理对象
2.通过代理对象来增强 类的方法
-
3. 操作术语
1、连接点 (可以被增强的方法,都可称为连接点)
2、切入点 (实际真正被增强的方法)
3、通知(增强) (实际增加的逻辑部分称为通知)
-
前置通知:在方法执行前 执行 @Before
-
后置通知:在方法执行后 执行 @AfterReturning
-
环绕通知:在方法执行前后 都执行 @Around
-
异常通知:在方法发生异常时 执行 @AfterThrowing
-
最终通知:最后面执行 @After
4、切面 (把通知应用到切入点的过程)
4. 准备工作
-
AspectJ :独立的AOP框架。不是Spring组成部分,一般 AspectJ 和 Spring 一起使用
-
导入AspectJ 的jar包 配置Spring一起使用
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> <scope>runtime</scope> </dependency> spring-core、spring-beans、spring-content、spring-aop -
切入点表达式:知道对哪个类的哪个方法进行增强
//语法: execution([权限修饰符][返回类型][类全路径][方法名称][参数列表]) //例1: //对com.potato.dao.BookDao 类的add()进行增强 execution(* com.potato.dao.BookDao.add(..)) //此处省略了返回类型 //例2: //对com.potato.dao.BookDao 类的所有方法进行增强 execution(* com.potato.dao.BookDao.*(..)) //此处省略了返回类型 //例3: //对com.potato.dao 包下的所有类进行增强 execution(* com.potato.dao.*.*(..)) //此处省略了返回类型
5. AspectJ注解使用
-
在Spring配置中开启组件扫描
-
开启AspectJ 生成代理对象
-
创建增强类,创建不同的方法 代表不同的通知
配置文件
<!-- 1.组件扫描--> <context:component-scan base-package="com.potato"/> <!-- 2.开启AspectJ 生成代理对象--> <aop:aspectj-autoproxy/>配置类
@Configuration @ComponentScan("com.potato") @EnableAspectJAutoProxy(proxyTargetClass = true) public class SpringConfig { }配置类和配置文件 二选一
增强类
/** * 增强类 */ @Component @Aspect public class UserProxy { // 前置通知 @Before("execution(* com.potato.dao.*.*(..))") void before() { System.out.println("前置通知..............."); } // 后置通知 / 返回通知 @AfterReturning("execution(* com.potato.dao.*.*(..))") void afterReturning() { System.out.println("后置通知..............."); } // 最终通知 @After("execution(* com.potato.dao.*.*(..))") void after() { System.out.println("最终通知..............."); } // 异常通知 @AfterThrowing("execution(* com.potato.dao.*.*(..))") void afterThrowing() { System.out.println("异常通知..............."); } // 环绕通知 @Around("execution(* com.potato.dao.*.*(..))") void around(ProceedingJoinPoint point) throws Throwable { System.out.println("环绕之前..............."); // 执行被增强的方法 point.proceed(); System.out.println("环绕之后..............."); } }
@Pointcut
切入点提取
@Pointcut(value = "execution(* com.potato.dao.*.*(..))")
public void pointDemo() {
}
// 前置通知
@Before("pointDemo()")
void before() {
System.out.println("前置通知...............");
}
// 后置通知 / 返回通知
@AfterReturning("pointDemo()")
void afterReturning() {
System.out.println("后置通知...............");
}
@Order
设置多个增强类的优先执行顺序
用于类上,值越小优先级越高
@Component
@Aspect
@Order(1)
public class UserProxy {
}
6. AspectJ配置使用
- 创建增强类和被增强类
- 在配置文件中创建对象
- 在配置文件中配置切入点
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1.创建增强类和被增强类-->
<!-- 2.增强类和被增强类 交由Spring管理-->
<bean id="userDao" class="com.potato.dao.UserDaoImpl"/>
<bean id="userProxy" class="com.potato.aop.UserProxy"/>
<!-- 3.配置aop增强-->
<aop:config>
<!-- 3.1 切入点-->
<aop:pointcut id="pointcut01" expression="execution(* com.potato.dao.*.*(..))"/>
<!-- 3.2 配置切面-->
<aop:aspect ref="userProxy">
<aop:before method="before" pointcut-ref="pointcut01"/>
<aop:after method="after" pointcut-ref="pointcut01"/>
</aop:aspect>
</aop:config>
</beans>

浙公网安备 33010602011771号