Spring AOP
2-AOP详细介绍
2021-08-20 129 0
简介 AOP的详细用法
1. AOP中的术语介绍
1、连接点
类里面哪些方法可以被增强,这些方法称为连接点
2、切入点
实际被真正增强的方法,称为切入点
3、通知(增强)
(1)实际增强的逻辑部分称为通知(增强)
(2)通知的类型
*前置通知
*后置通知
*环绕通知
*异常通知
*最终通知 不管有没有异常,都会执行,类似finally
4、切面
就是动作
(1)把通知应用到切入点过程
2. AOP操作
1、 Spring 框架一般都是基于 AspectJ 实现 AOP 操作
(1) AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作
2、基于 AspectJ 实现 AOP 操作
(1)基于 xml 配置文件实现
(2)基于注解方式实现(使用)
3、在项目工程里面引入 AOP 相关依赖
4. 切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
例 1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强
execution(* com.atguigu.dao.BookDao.add(..)) * 表示所有的权限修饰符
例 2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强
execution(* com.atguigu.dao.BookDao.* (..))
例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强
execution(* com.atguigu.dao.*.* (..))
3. AOP的具体实现
1、创建类,在类里面定义方法
被增强的方法,也就是被代理类中的方法
2. 创建增强类(编写增强逻辑)
(1)在增强类里面,创建方法,让不同方法代表不同通知类型
(2)增强类,也就是代理类,需要代理被增强的类
public class Emp {
public void method(){
System.out.println("emp method ... ");
}
}
public class EmpProxy {
public void before() {
System.out.println("before ...");
}
}
3. 进行通知的配置
(1)在 spring 配置文件中,开启注解扫描
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.ylaihui"></context:component-scan>
</beans>
(2)使用注解创建 Emp 和 EmpProxy 对象
@Component
public class Emp {
public void method(){
System.out.println("emp method ... ");
}
}
@Component
public class EmpProxy {
public void before() {
System.out.println("before ...");
}
}
(3)在增强类(代理类)上面添加注解@Aspect
@Component
@Aspect
public class EmpProxy {
public void before() {
System.out.println("before ...");
}
}
(4)在 spring 配置文件中开启生成代理对象
<!-- 配置自动生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4、配置不同类型的通知
(1)在增强类的里面,在不同的方法添加通知类型注解,使用切入点表达式配置,使得每个方法有不同的功能
也就是配置方法的不同通知类型
@Component
@Aspect
public class EmpProxy {
@Before(value = "execution(* com.ylaihui.aopAspectJ.Emp.method(..))")
public void before() {
System.out.println("before ...");
}
}
测试类
public class TestAop {
@Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aopAspectJ.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.method();
}
}
测试结果
before ...
emp method ...
4. 抽取公共的切入点
//抽取切入点
@Pointcut(value = "execution(* com.ylaihui.aopAspectJ.Emp.method(..))")
public void pointCut(){
}
@Before(value = "pointCut()")
public void before() {
System.out.println("before ...");
}
5. 增强类的优先级问题
假如现在有多个增强类(代理类), 对同一个方法进行增强(被代理类的方法),设置增强类优先级
在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高
@Order(1)
代理类...
@Order(2)
代理类...
6. AOP的完全注解开发
不需要配置XML配置文件
1. 配置类
// 配置、组件扫描、 自动生成代理对象
@Configuration
@ComponentScan(basePackages = {"com.ylaihui"})
@EnableAspectJAutoProxy(exposeProxy = true)
public class SpringAopConfig {
}
2. 代理类和被代理类
@Component
public class Student {
public void study(){;
System.out.println("# study method called ...");
}
}
@Component
@Aspect
public class StudentProxy {
@Before(value = "execution(* com.ylaihui.aopAspectJAnnocation.Student.study(..))")
public void before(){
System.out.println("# before method called ...");
}
}
3. 测试类和测试结果
public class TestAop {
@Test
public void test1(){
System.out.println("# test1 method called ...");
AnnotationConfigApplicationContext con = new AnnotationConfigApplicationContext(SpringAopConfig.class);
Student student = con.getBean("student", Student.class);
student.study();
}
}
//# test1 method called ...
//# before method called ...
//# study method called ...