iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >如何使用Spring AOP进行测试
  • 671
分享到

如何使用Spring AOP进行测试

2024-04-02 19:04:59 671人浏览 泡泡鱼
摘要

本篇内容主要讲解“如何使用spring aop进行测试”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Spring AOP进行测试”吧!AOP解析今天来介

本篇内容主要讲解“如何使用spring aop进行测试”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Spring AOP进行测试”吧!

AOP解析

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

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

如何使用Spring AOP进行测试

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

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

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

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

如何使用Spring AOP进行测试

AOP 领域中的特性术语:

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 ,  事务等等....

  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。

  • 通知(Advice):AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 连接点(JointPoint):表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP  中,连接点总是方法的调用。

  • 切入点(PointCut):可以插入增强处理的连接点。

  • 引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。

  • 织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。

Advice通知

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

如何使用Spring AOP进行测试

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

如何使用Spring AOP进行测试

实例编码

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

如何使用Spring AOP进行测试

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

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

首先,mvn中添加配置


如何使用Spring AOP进行测试

实例如下:

创建接口

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>

测试类如下:

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

执行测试代码,结果如下

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

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

执行执行了

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

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

到此,相信大家对“如何使用Spring AOP进行测试”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: 如何使用Spring AOP进行测试

本文链接: https://www.lsjlt.com/news/83214.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作