SpringBoot之集成Spring aop实例介绍
本篇文章主要介绍了详解SpringBoot之集成Spring AOP,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在开始之前,我们先把需要的jar包添加到工程里。新增Maven依赖如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
接下来,我们进入正题。这里的涉及的通知类型有:前置通知、后置最终通知、后置返回通知、后置异常通知、环绕通知,下面我们就具体的来看一下怎么在SpringBoot中添加这些通知。
首先我们先创建一个Aspect切面类:
@Component @Aspect public class WebControllerAop { }
指定切点:
//匹配com.zkn.learnspringboot.web.controller包及其子包下的所有类的所有方法 @Pointcut("execution(* com.zkn.learnspringboot.web.controller..*.*(..))") public void executeService(){ }
接着我们再创建一个Controller请求处理类:
package com.zkn.learnspringboot.web.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by zkn on 2016/11/19. */ @RestController @RequestMapping("/aop") public class AopTestController { }
前置通知
配置前置通知:
/** * 前置通知,方法调用前被调用 * @param joinPoint */ @Before("executeService()") public void doBeforeAdvice(JoinPoint joinPoint){ System.out.println("我是前置通知!!!"); //获取目标方法的参数信息 Object[] obj = joinPoint.getArgs(); //AOP代理类的信息 joinPoint.getThis(); //代理的目标对象 joinPoint.getTarget(); //用的最多 通知的签名 Signature signature = joinPoint.getSignature(); //代理的是哪一个方法 System.out.println(signature.getName()); //AOP代理类的名字 System.out.println(signature.getDeclaringTypeName()); //AOP代理类的类(class)信息 signature.getDeclaringType(); //获取RequestAttributes RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //从获取RequestAttributes中获取HttpServletRequest的信息 HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); //如果要获取Session信息的话,可以这样写: //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION); Enumeration<String> enumeration = request.getParameterNames(); Map<String,String> parameterMap = Maps.newHashMap(); while (enumeration.hasMoreElements()){ String parameter = enumeration.nextElement(); parameterMap.put(parameter,request.getParameter(parameter)); } String str = JSON.toJSONString(parameterMap); if(obj.length > 0) { System.out.println("请求的参数信息为:"+str); } }
注意:这里用到了JoinPoint和RequestContextHolder。通过JoinPoint可以获得通知的签名信息,如目标方法名、目标方法参数信息等。通过RequestContextHolder来获取请求信息,Session信息。
接下来我们在Controller类里添加一个请求处理方法来测试一下前置通知:
@RequestMapping("/testBeforeService.do") public String testBeforeService(String key,String value){ return "key="+key+" value="+value; }
前置通知拦截结果如下所示:
后置返回通知
配置后置返回通知的代码如下:
/** * 后置返回通知 * 这里需要注意的是: * 如果参数中的第一个参数为JoinPoint,则第二个参数为返回值的信息 * 如果参数中的第一个参数不为JoinPoint,则第一个参数为returning中对应的参数 * returning 限定了只有目标方法返回值与通知方法相应参数类型时才能执行后置返回通知,否则不执行,对于returning对应的通知方法参数为Object类型将匹配任何目标返回值 * @param joinPoint * @param keys */ @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys") public void doAfterReturningAdvice1(JoinPoint joinPoint,Object keys){ System.out.println("第一个后置返回通知的返回值:"+keys); } @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys",argNames = "keys") public void doAfterReturningAdvice2(String keys){ System.out.println("第二个后置返回通知的返回值:"+keys); }
Controller里添加响应的请求处理信息来测试后置返回通知:
@RequestMapping("/testAfterReturning.do") public String testAfterReturning(String key){ return "key=: "+key; } @RequestMapping("/testAfterReturning01.do") public Integer testAfterReturning01(Integer key){ return key; }
当发送请求为:http://localhost:8001/aop/testAfterReturning.do?key=testsss&value=855sss时,处理结果如图所示:
当发送请求为:http://localhost:8001/aop/testAfterReturning01.do?key=55553&value=855sss时,处理结果如图所示:
后置异常通知
后置异常通知的配置方式如下:
/** * 后置异常通知 * 定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法; * throwing 限定了只有目标方法抛出的异常与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行, * 对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。 * @param joinPoint * @param exception */ @AfterThrowing(value = "executeService()",throwing = "exception") public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){ //目标方法名: System.out.println(joinPoint.getSignature().getName()); if(exception instanceof NullPointerException){ System.out.println("发生了空指针异常!!!!!"); } }
Controller里配置响应的请求处理类:
@RequestMapping("/testAfterThrowing.do") public String testAfterThrowing(String key){ throw new NullPointerException(); }
后置异常通知方法的处理结果如下所示:
后置最终通知
后置最终通知的配置方式如下:
/** * 后置最终通知(目标方法只要执行完了就会执行后置通知方法) * @param joinPoint */ @After("executeService()") public void doAfterAdvice(JoinPoint joinPoint){ System.out.println("后置通知执行了!!!!"); }
Controller类配置相应的请求处理类:
@RequestMapping("/testAfter.do") public String testAfter(String key){ throw new NullPointerException(); } @RequestMapping("/testAfter02.do") public String testAfter02(String key){ return key; }
当发送请求为:http://localhost:8001/aop/testAfter.do?key=55553&value=855sss
当发送请求为:http://localhost:8001/aop/testAfter02.do?key=55553&value=855sss
环绕通知
环绕通知的配置方式如下:
/** * 环绕通知: * 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。 * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型 */ @Around("execution(* com.zkn.learnspringboot.web.controller..*.testAround*(..))") public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ System.out.println("环绕通知的目标方法名:"+proceedingJoinPoint.getSignature().getName()); try { Object obj = proceedingJoinPoint.proceed(); return obj; } catch (Throwable throwable) { throwable.printStackTrace(); } return null; }
Controller对应的请求处理类如下:
@RequestMapping("/testAroundService.do") public String testAroundService(String key){ return "环绕通知:"+key; }
当发送请求为:http://localhost:8001/aop/testAroundService.do?key=55553
当发送请求为:http://localhost:8001/aop/testAfter02.do?key=55553&value=855sss时,不符合环绕通知的切入规则,所以环绕通知不会 执行。
完整的AOP配置代码如下:
package com.zkn.learnspringboot.aop; import com.alibaba.fastjson.JSON; import com.google.common.collect.Maps; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.Enumeration; import java.util.Map; /** * Created by zkn on 2016/11/18. */ @Component @Aspect public class WebControllerAop { //匹配com.zkn.learnspringboot.web.controller包及其子包下的所有类的所有方法 @Pointcut("execution(* com.zkn.learnspringboot.web.controller..*.*(..))") public void executeService(){ } /** * 前置通知,方法调用前被调用 * @param joinPoint */ @Before("executeService()") public void doBeforeAdvice(JoinPoint joinPoint){ System.out.println("我是前置通知!!!"); //获取目标方法的参数信息 Object[] obj = joinPoint.getArgs(); //AOP代理类的信息 joinPoint.getThis(); //代理的目标对象 joinPoint.getTarget(); //用的最多 通知的签名 Signature signature = joinPoint.getSignature(); //代理的是哪一个方法 System.out.println(signature.getName()); //AOP代理类的名字 System.out.println(signature.getDeclaringTypeName()); //AOP代理类的类(class)信息 signature.getDeclaringType(); //获取RequestAttributes RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //从获取RequestAttributes中获取HttpServletRequest的信息 HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); //如果要获取Session信息的话,可以这样写: //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION); Enumeration<String> enumeration = request.getParameterNames(); Map<String,String> parameterMap = Maps.newHashMap(); while (enumeration.hasMoreElements()){ String parameter = enumeration.nextElement(); parameterMap.put(parameter,request.getParameter(parameter)); } String str = JSON.toJSONString(parameterMap); if(obj.length > 0) { System.out.println("请求的参数信息为:"+str); } } /** * 后置返回通知 * 这里需要注意的是: * 如果参数中的第一个参数为JoinPoint,则第二个参数为返回值的信息 * 如果参数中的第一个参数不为JoinPoint,则第一个参数为returning中对应的参数 * returning 限定了只有目标方法返回值与通知方法相应参数类型时才能执行后置返回通知,否则不执行,对于returning对应的通知方法参数为Object类型将匹配任何目标返回值 * @param joinPoint * @param keys */ @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys") public void doAfterReturningAdvice1(JoinPoint joinPoint,Object keys){ System.out.println("第一个后置返回通知的返回值:"+keys); } @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys",argNames = "keys") public void doAfterReturningAdvice2(String keys){ System.out.println("第二个后置返回通知的返回值:"+keys); } /** * 后置异常通知 * 定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法; * throwing 限定了只有目标方法抛出的异常与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行, * 对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。 * @param joinPoint * @param exception */ @AfterThrowing(value = "executeService()",throwing = "exception") public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){ //目标方法名: System.out.println(joinPoint.getSignature().getName()); if(exception instanceof NullPointerException){ System.out.println("发生了空指针异常!!!!!"); } } /** * 后置最终通知(目标方法只要执行完了就会执行后置通知方法) * @param joinPoint */ @After("executeService()") public void doAfterAdvice(JoinPoint joinPoint){ System.out.println("后置通知执行了!!!!"); } /** * 环绕通知: * 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。 * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型 */ @Around("execution(* com.zkn.learnspringboot.web.controller..*.testAround*(..))") public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ System.out.println("环绕通知的目标方法名:"+proceedingJoinPoint.getSignature().getName()); try {//obj之前可以写目标方法执行前的逻辑 Object obj = proceedingJoinPoint.proceed();//调用执行目标方法 return obj; } catch (Throwable throwable) { throwable.printStackTrace(); } return null; } }
完整的Controller类代码如下:
package com.zkn.learnspringboot.web.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by zkn on 2016/11/19. */ @RestController @RequestMapping("/aop") public class AopTestController { @RequestMapping("/testBeforeService.do") public String testBeforeService(String key,String value){ return "key="+key+" value="+value; } @RequestMapping("/testAfterReturning.do") public String testAfterReturning(String key){ return "key=: "+key; } @RequestMapping("/testAfterReturning01.do") public Integer testAfterReturning01(Integer key){ return key; } @RequestMapping("/testAfterThrowing.do") public String testAfterThrowing(String key){ throw new NullPointerException(); } @RequestMapping("/testAfter.do") public String testAfter(String key){ throw new NullPointerException(); } @RequestMapping("/testAfter02.do") public String testAfter02(String key){ return key; } @RequestMapping("/testAroundService.do") public String testAroundService(String key){ return "环绕通知:"+key; } }
以上是SpringBoot之集成Spring aop实例介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

wapi这个名词用户们可能在使用网络得时候见到过,但是对于一部分人来说肯定都不知道wapi是什么,下面就带来了详细介绍,帮助不知道小伙伴去了解。wapi是什么东西:答:wapi是无线局域网鉴别和保密的基础结构。这就像红外线和蓝牙等功能一样,一般都覆盖在办公楼等地方的附近。基本都是为一个小部门所有的,所以这个功能涉及的范围只有几公里。wapi相关介绍:1、wapi是无线局域网里面的一种传输协议。2、这款技术是可以去避免窄频带通信的问题,可以更好的去进行传播。3、仅仅只需要一个代码就可以去传送信号了

2023年,AI技术已经成为热点话题,对各行业产生了巨大影响,编程领域尤其如此。人们越来越认识到AI技术的重要性,Spring社区也不例外。随着GenAI(GeneralArtificialIntelligence)技术的不断进步,简化具备AI功能的应用程序的创建变得至关重要和迫切。在这个背景下,"SpringAI"应运而生,旨在简化开发AI功能应用程序的过程,使其变得简单直观,避免不必要的复杂性。通过"SpringAI",开发者可以更轻松地构建具备AI功能的应用程序,将其变得更加易于使用和操作

Spring+AI作为行业领导者,通过其强大、灵活的API和先进的功能,为各种行业提供了领先性的解决方案。在本专题中,我们将深入探讨Spring+AI在各领域的应用示例,每个案例都将展示Spring+AI如何满足特定需求,实现目标,并将这些LESSONSLEARNED扩展到更广泛的应用。希望这个专题能对你有所启发,更深入地理解和利用Spring+AI的无限可能。Spring框架在软件开发领域已经有超过20年的历史,自SpringBoot1.0版本发布以来已有10年。现在,无人会质疑,Spring

pubg又称绝地求生,是一款非常经典的射击大逃杀类型游戏,从2016年火爆以来一直拥有非常多的玩家。在最近的win11系统推出后,就有不少玩家想要在win11上游玩它,下面就跟着小编来看看win11是否可以玩pubg吧。win11能玩pubg吗:答:win11可以玩pubg。1、在win11推出之初,因为win11需要开启tpm的缘故,所以导致很多玩家被pubg封号处理了。2、不过后来根据玩家的反馈,蓝洞方面已经解决了这个问题,目前已经可以在win11中正常玩pubg了。3、如果大家遇到了pub

spring编程式事务的实现方式:1、使用TransactionTemplate;2、使用TransactionCallback和TransactionCallbackWithoutResult;3、使用Transactional注解;4、使用TransactionTemplate和@Transactional结合使用;5、自定义事务管理器。

很多用户更新了最新的win11之后发现自己系统的声音有了些许的变化,但是又不知道该怎么去进行调整,所以今天本站就给你们带来了电脑最新win11声音调法介绍,操作不难而且选择多样,快来一起下载试试吧。电脑最新系统windows11声音如何调1、首先右击桌面右下角的声音图标,并选择“播放设置”。2、然后进入设置中点击播放栏中的“扬声器”。3、随后点击右下方的“属性”。4、点击属性中的“增强”选项栏。5、此时如果“禁用所有声音效果”前的√勾上了就把他取消。6、之后就可以选择下面的声音效果来进行设置并点

很多用户在电脑上安装了打印机驱动程序,但却不知道如何找到它们。因此,今天我为大家带来了详细介绍打印机驱动程序在电脑中的位置,对于还不了解的用户,快来看看吧打印机驱动在电脑哪里找重新撰写内容而不改变原义时,需要将语言改写为中文,不需要出现原句首先,建议使用第三方软件进行搜索2、在右上角找到"工具箱"3、在下方找到并点击“设备管理器”。改写后的句子:3、在底部找到并点击“设备管理器”4、然后打开“打印队列”,然后找到你的打印机设备。此次是你的打印机名称型号。5、右键打印机设备,就能够去更新或者卸载我

PyCharm是一款功能强大的Python集成开发环境,具有丰富的功能和工具,能够极大地提高开发效率。其中,替换功能是开发过程中经常用到的功能之一,能够帮助开发者快速修改代码并提高代码质量。本文将详细介绍PyCharm的替换功能,并结合具体的代码示例,帮助新手更好地掌握和使用该功能。替换功能简介PyCharm的替换功能可以帮助开发者在代码中快速替换指定的文本
