文章目录

AOP(概念):

1.什么是AOP

  • 面向切面(方面)编程,不修改源代码进行功能增强。
  • 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。

举一个简单的例子:登录功能

  • 登录流程:
  • Spring AOP 底层原理、相关术语、spring中AOP的操作_xml

1.输入用户名和密码。
2.输入之后,在页面中做表单提交
3.在程序中得到用户名和密码,在数据库中查询,判断你输入的用户名和密码是否正确。

现在有个需求,在登录功能基础上添加功能:* 权限判断


实现方式:

* 原始方式:修改源代码实现

if 管理员

else if 普通用户

* AOP: 不通过修改源代码方式添加新的功能


AOP方式实现:权限判断模块独立出来成为一个新的模块,将其配置到登录的主干中去。

这样写的好处:1.登录的之前代码不需要变,假如某一天我们不需要权限判断,那么原来的代码也不需要变,我们把权限判断配置去掉就可以了。这个过程就叫AOP.

Spring AOP 底层原理、相关术语、spring中AOP的操作_动态代理_02


我们把登录主干功能和权限判断功能分离出来,两个加起来构成一个更完善的功能,这个过程它们是相互分离的,这在一定程度上降低了耦合度。

AOP底层原理

1.AOP底层使用动态代理(用动态代理的方式增强类中的某个方法的功能)
有两种情况的动态代理:

第一种:有接口情况,使用JDK动态代理

代理:现在通过这种方式创建你要被增强对象的一个代理对象,然后通过代理对象把功能实现。

  • 创建接口实现类的代理对象,增强类的方法
  • Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_03

  1. 使用JDK动态代理:使用Proxy类里的方法创建代理对象

第二种:没有接口情况,使用CGLIB动态代理

  • 创建子类的代理对象,增强类的方法
  • Spring AOP 底层原理、相关术语、spring中AOP的操作_AOP_04


  • Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_05

(1)调用newProxyInstance 方法
方法有三个参数:

  • 类加载器
  • 增强方法所在的类,这个类实现的接口,支持多个接口。
  • 实现这个接口InvocationHandler,创建代理对象,写增强的部分。
  1. JDK动态代理代码
    (1)创建接口,定义方法
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}

(2)创建接口实现类,实现方法

public class UserDaoImpl implements UserDao {

public int add(int a, int b) {
System.out.println("add方法执行了");

return a+b;
}

public String update(String id) {
System.out.println("update方法执行了");
return id;
}
}

(3)使用Proxy类创建接口代理对象

package com.atguigu.spring5;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
* Create By LiFang on 2020/11/8.
*/
public class JDKProxy {
public static void main(String[] args) {
//创建接口实现类的代理对象
Class[] interfaces = {UserDao.class};

//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
//1.创建的是谁的代理对象,把谁传递过来
//通过有参构造把对象传过来

private Object obj; //更通用就写object(也可写UserDaoImpl userDaoImpl)

public UserDaoProxy(Object obj) {
this.obj = obj;
}

//增强的逻辑
//只要对象一创建,类里面的方法就会被调用。方法中就写增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行...." + method.getName() + "传递的参数..." + Arrays.toString(args));
//被增强的方法执行
Object res = method.invoke(obj, args); //执行当前的方法
//方法之后
System.out.println("方法之后执行..." + obj);
return res;
}
}
UserDaoImpl userDao = new UserDaoImpl();

//三个参数:1.类加载器 2.接口 3.实现类 (接口等于实现类的代理对象==> UserDao userDao = new UserDaoImpl();)
UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));//new UserDaoProxy():匿名对象
int result = dao.add(1, 2);
System.out.println("result" + result);

//因为现在想要把接口实现类的方法做增强,因此现在要把它的对象传到 JDK代理对象中,因为传过去才能
// 把之前的逻辑执行,再加上新的逻辑
}
}

AOP操作术语

  • 连接点
  • 切入点
  • 通知(增强)
  • 切面

Spring AOP 底层原理、相关术语、spring中AOP的操作_xml_06

spring中AOP操作

准备

  1. Spring框架一般都是基于AspectJ实现AOP操作
  • AspectJ不是spring的组成部分,独立的AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作
  1. 基于AspectJ实现AOP操作
  • 基于xml配置文件
  • 基于注解方式实现(实际开发中使用较多)
  1. 在项目工程中引入AOP相关依赖
  2. Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_07

  3. 切入点表达式
    (1)切入点表达式的作用:知道对哪个类里面的哪个方法进行增强
    (2)语法结构:
  4. Spring AOP 底层原理、相关术语、spring中AOP的操作_动态代理_08


  5. Spring AOP 底层原理、相关术语、spring中AOP的操作_xml_09


  6. Spring AOP 底层原理、相关术语、spring中AOP的操作_xml_10

基于AspectJ注解方式实现五种通知类型配置(AOP操作)

  1. 创建类,在类里面定义方法。
public class User {
public void add() {
System.out.println("add....");
}
}
  1. 创建增强类(编写增强逻辑)
    (1)在增强类里面,创建方法,让不同的方法代表不同通知类型
public class UserProxy {
//前置通知
public void before() {
System.out.println("before....");
}
}
  1. 进行通知的配置
    (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.atguigu.spring5.aopanno"></context:component-scan>
</beans>

(2)使用注解创建User和UserProxy对象

Spring AOP 底层原理、相关术语、spring中AOP的操作_xml_11


Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_12

(3)在增强类上面添加注解@Aspect(表示让这个类生成代理对象)

Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_13


(4)在spring配置文件中开启生成代理对象

<!--    开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  1. 配置不同类型的通知
    (1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
  2. Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_14

package com.atguigu.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
* Create By LiFang on 2020/11/10.
*/
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知
@Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void before() {
System.out.println("before....");
}

//后置通知(返回通知)
@AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add())")
public void afterReturning() {
System.out.println("afterReturning....方法返回结果之后执行;有异常时,不执行");
}

//最终通知(有异常也执行)
@After(value = "execution(* com.atguigu.spring5.aopanno.User.add())")
public void after() {
System.out.println("after....在方法执行之后执行");
}

//异常通知(方法出现异常时执行)
@AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add())")
public void afterThrowing() {
System.out.println("afterThrowing...异常通知");
}

//环绕通知(在方法之后和之前都执行)
@Around(value = "execution(* com.atguigu.spring5.aopanno.User.add())")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前....");
//被增强的add方法执行
proceedingJoinPoint.proceed();

System.out.println("环绕之后。。。");

}
}

测试:

public class TestAop {

@Test
public void testAopAnno() {
//获取Spring 的上下文对象,拿到Spring 的容器
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//我们的对象现在都在Spring中的管理了,我们要使用,直接去里面取出来就可以
User user = context.getBean("user", User.class);
user.add();
}
}

测试结果:

Spring AOP 底层原理、相关术语、spring中AOP的操作_spring_15


5. 细节问题:相同切入点抽取

//相同切入点抽取
@Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add())")
public void pointdemo() {

}
//前置通知
@Before(value = "pointdemo()")
public void before() {
System.out.println("before....");
}

6. 有多个增强类对同一个方法进行增强,设置多个增强类优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小,优先级越高。

package com.atguigu.spring5.aopanno;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* Create By LiFang on 2020/11/14.
*/
@Component
@Aspect //生成代理对象
@Order(1)
public class PersonProxy {
//前置通知
@Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void before() {
System.out.println("Person before。。。");
}
}
package com.atguigu.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* Create By LiFang on 2020/11/10.
*/
//增强的类
@Component
@Aspect //生成代理对象
@Order(2)
public class UserProxy {
//相同切入点抽取
@Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void pointdemo() {

}

//前置通知
@Before(value = "pointdemo()")
public void before() {
System.out.println("before....");
}

//后置通知(返回通知)
@AfterReturning(value = "pointdemo()")
public void afterReturning() {
System.out.println("afterReturning....方法返回结果之后执行;有异常时,不执行");
}

//最终通知(有异常也执行)
@After(value = "pointdemo()")
public void after() {
System.out.println("after....在方法执行之后执行");
}

//异常通知(方法出现异常时执行)
@AfterThrowing(value = "pointdemo()")
public void afterThrowing() {
System.out.println("afterThrowing...异常通知");
}

//环绕通知(在方法之后和之前都执行)
@Around(value = "pointdemo()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前....");
//被增强的add方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后。。。");
}
}

测试结果:

Spring AOP 底层原理、相关术语、spring中AOP的操作_xml_16