如何使用Spring AOP进行测试

发布时间:2021-10-15 09:53:04 作者:iii
来源:亿速云 阅读:166

本篇内容主要讲解“如何使用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 领域中的特性术语:

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进行测试”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Spring AOP
  2. 如何在spring中使用hibernate

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring aop

上一篇:为什么要学Shell

下一篇:C++编译期多态与运行期多态是什么样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》