测试同学上手Spring 之AOP最易懂的解析

开发 前端
今天来介绍Spring的另一个核心技术点AOP,AOP的概念不好理解,希望大家仔细阅读文章并按照文章中的代码进行练习,届时一定会有很大的收获!

[[390401]]

前面连续介绍了几篇上手Spring的基础文章

  • 测试同学从0到1上手Spring
  • 测试同学上手Spring 之IoC深入解析
  • 测试同学上手Spring 之DI深入解析

AOP解析

今天来介绍Spring的另一个核心技术点AOP,AOP的概念不好理解,希望大家仔细阅读文章并按照文章中的代码进行练习,届时一定会有很大的收获!

AOP (Aspect OrientProgramming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。从《Spring实战(第4版)》图书中扒了一张图:

从该图可以很形象地看出,所谓切面,相当于应用对象间的横切点,我们可以将其单独抽象为单独的模块。

Spring提供了面向切面编程的丰富支持,是通过动态代理实现的。允许通过分离应用的业务逻辑与系统级服务(例如:审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识到)其它系统级别的关注点,例如:日志或事务支持。

AOP 要达到的效果是,保证开发者在不修改源代码的前提下,去为系统中的业务组件添加某种通用功能。

AOP基本运行流程如下图所示:

AOP 领域中的特性术语:

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等....
  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
  • 通知(Advice):AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 连接点(JointPoint):表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP 中,连接点总是方法的调用。
  • 切入点(PointCut):可以插入增强处理的连接点。
  • 引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。
  • 织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。

Advice通知

通知(Advice)是切面的一种实现,可以完成简单织入功能(织入功能就是在这里完成的)。Spring AOP 中有 5 中通知类型,分别如下:

各个通知的执行顺序如下图所示:

实例编码

需求:在类中添加日志功能,如下图:

实现方法1:在各个类中添加方法logMsg()。如果类数量少,问题不大,如果有几百个类需要处理,那么工作量就很大了。

实现方法2:通过aop来实现

首先,mvn中添加配置


实例如下:

创建接口

  1. public interface UserService { 
  2.        public void add(); 
  3.        public void delete(); 
  4.        public void update(); 
  5.        public void search(); 
  6.     } 

 创建切入点类

  1. public class UserServiceImpl implements UserService { 
  2.       public void add() { 
  3.           System.out.println("增加用户"); 
  4.       } 
  5.        public void delete() { 
  6.           System.out.println("删除用户"); 
  7.       } 
  8.        public void update() { 
  9.           System.out.println("更新用户"); 
  10.       } 
  11.        public void search() { 
  12.           System.out.println("查询用户"); 
  13.       } 

 创建类,实现@Before通知

  1. import java.lang.reflect.Method; 
  2. import org.springframework.aop.MethodBeforeAdvice; 
  3. public class BeforeLog implements MethodBeforeAdvice { 
  4.        //method : 要执行的目标对象的方法 
  5.        //objects : 被调用的方法的参数 
  6.        //Object : 目标对象 
  7.        public void before(Method method, Object[] objects, Object o) throws Throwable { 
  8.           System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被执行了"); 
  9.       } 
  10.     } 

 创建类,实现@After通知

  1. import java.lang.reflect.Method; 
  2. import org.springframework.aop.AfterReturningAdvice; 
  3. public class AfterLog implements AfterReturningAdvice { 
  4.        //returnValue 返回值 
  5.        //method被调用的方法 
  6.        //args被调用的方法的对象的参数 
  7.        //target 被调用的目标对象 
  8.        public void afterReturning(Object returnValue,Method method, Object[] args, Object target) throws Throwable { 
  9.           System.out.println("执行了" + target.getClass().getName() 
  10.           +"的"+method.getName()+"方法," 
  11.           +"返回值:"+returnValue); 
  12.       } 
  13.     } 

 编辑xml文件

  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <beansxmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xmlns:p="http://www.springframework.org/schema/p" 
  7.     xmlns:c="http://www.springframework.org/schema/c" 
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  9.        https://www.springframework.org/schema/beans/spring-beans.xsd 
  10.        http://www.springframework.org/schema/context 
  11.        https://www.springframework.org/schema/context/spring-context.xsd 
  12.        http://www.springframework.org/schema/aop 
  13.         http://www.springframework.org/schema/aop/spring-aop.xsd"> 
  14.     <context:annotation-config/> 
  15.  <!--注册bean--> 
  16.    <beanid="userService"class="com.my.demo.aop.UserServiceImpl"/> 
  17.    <beanid="beforelog"class="com.my.demo.aop.BeforeLog"/> 
  18.    <beanid="afterLog"class="com.my.demo.aop.AfterLog"/> 
  19.    <aop:config> 
  20.        <!--切入点 expression:表达式匹配要执行的方法--> 
  21.        <aop:pointcutid="pointcut"expression="execution(* 
  22. com.my.demo.aop.UserServiceImpl.*(..))"/> 
  23.        <!--执行环绕; advice-ref执行方法.pointcut-ref切入点--> 
  24.        <aop:advisoradvice-ref="beforelog"pointcut-ref="pointcut"/> 
  25.        <aop:advisoradvice-ref="afterLog"pointcut-ref="pointcut"/> 
  26.    </aop:config> 
  27. </beans> 

 测试类如下:

  1.  public static void main(String[] args) { 
  2.      ApplicationContextcontext = new ClassPathXmlApplicationContext("bean3.xml"); 
  3.       UserServiceuserService = (UserService) context.getBean("userService"); 
  4.       userService.search(); 

 执行测试代码,结果如下

com.my.demo.aop.UserServiceImpl的search方法被执行了 //before方法执行

查询用户 //UserServiceImpl中的search方法

执行执行了

com.my.demo.aop.UserServiceImpl的search方法,返回值:null//after方法执行

可以发现由于实现了@Before通知@After通知,我们在调用方法前后,就分别自动对before 和afterReturning完成了调用。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-03-23 08:12:13

SpringDIIoC

2021-03-16 08:22:49

SpringIoCAOP

2021-03-10 09:21:00

Spring开源框架Spring基础知识

2022-06-07 07:58:45

SpringSpring AOP

2024-04-10 08:59:39

SpringAOP业务

2019-04-28 11:06:01

Hbase架构程序员

2022-06-08 08:04:28

Springservicerepository

2021-05-06 18:17:52

SpringAOP理解

2023-02-04 18:19:39

2009-06-19 13:28:30

Spring AOPSpring 2.0

2022-02-17 13:39:09

AOP接口方式

2009-06-22 10:41:34

Spring.AOP

2023-02-01 09:15:41

2022-12-07 08:02:43

Spring流程IOC

2009-06-18 14:54:52

Spring AOP

2019-11-29 16:21:22

Spring框架集成

2024-03-04 08:47:17

Spring框架AOP

2009-09-29 10:00:40

Spring AOP框

2021-05-27 08:47:16

C语言C语言程序开发

2009-06-19 11:09:27

Spring AOP
点赞
收藏

51CTO技术栈公众号