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

[系统运维] 时间:2025-11-05 01:53:46 来源:益强IT技术网 作者:系统运维 点击:135次

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

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

AOP解析

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

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

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

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

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

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

AOP 领域中的特性术语:

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

Advice通知

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

各个通知的云服务器执行顺序如下图所示:

实例编码

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

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

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

首先,mvn中添加配置

实例如下:

创建接口

public interface UserService {        public void add();        public void delete();        public void update();        public void search();     } 

 创建切入点类

public class UserServiceImpl implements UserService {       public void add() {           System.out.println("增加用户");       }        public void delete() {           System.out.println("删除用户");       }        public void update() {           System.out.println("更新用户");       }        public void search() {           System.out.println("查询用户");       } 

 创建类,实现@Before通知

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

 创建类,实现@After通知

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

 编辑xml文件

<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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"     xmlns:p="http://www.springframework.org/schema/p"     xmlns:c="http://www.springframework.org/schema/c"     xsi:schemaLocation="http://www.springframework.org/schema/beans        https://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd">     <context:annotation-config/>  <!--注册bean-->    <beanid="userService"class="com.my.demo.aop.UserServiceImpl"/>    <beanid="beforelog"class="com.my.demo.aop.BeforeLog"/>    <beanid="afterLog"class="com.my.demo.aop.AfterLog"/>    <aop:config>        <!--切入点 expression:表达式匹配要执行的方法-->        <aop:pointcutid="pointcut"expression="execution(* com.my.demo.aop.UserServiceImpl.*(..))"/>        <!--执行环绕; advice-ref执行方法.pointcut-ref切入点-->        <aop:advisoradvice-ref="beforelog"pointcut-ref="pointcut"/>        <aop:advisoradvice-ref="afterLog"pointcut-ref="pointcut"/>    </aop:config> </beans> 

(责任编辑:数据库)

    相关内容
    精彩推荐
    热门点击
    友情链接