当前位置: 首页 > news >正文

springboot实现aop

目录

  • AOP(术语)
  • 引入依赖
  • 实现步骤
  • 测试验证
  • 感谢阅读

AOP(术语)

  1. 连接点
    类里面哪些方法可以增强,这些点被称为连接点

  2. 切入点
    实际被真正增强的方法

  3. 通知(增强)
    实际增强的逻辑部分称为通知(增强)
    通知(增强)有多种类型

  • 前置通知–@Before
  • 后置通知–@After
  • 环绕通知–@Around
  • 异常通知–@AfterThrowing
  • 最终通知–@AfterReturning
    切面(是动作)–
    把通知(增强)应用到切入点过程

引入依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency></dependencies>

实现步骤

  1. 新建一个注解类,用于后面做切入点
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Authorization {}
  1. 建一个切面类
@Aspect
@Component
public class AuthAspect {/*** 定义了一个切点* 这里的路径填自定义注解的全路径*/@Pointcut("@annotation(com.zhuyh.studytest.spring5.aop.Authorization)")public void authornizeCut() {}@Before("authornizeCut()")public void cutProcess(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("AOP开始拦截, 当前拦截的方法名: " + method.getName());}@After("authornizeCut()")public void after(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("AOP执行的方法 :" + method.getName() + " 执行完了");}@Around("authornizeCut()")public Object testCutAround(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("AOP拦截开始进入环绕通知.......");Object proceed = joinPoint.proceed();System.out.println("准备退出环绕......");return proceed;}/*** returning属性指定连接点方法返回的结果放置在result变量中** @param joinPoint 连接点* @param result    返回结果*/@AfterReturning(value = "authornizeCut()", returning = "result")public void afterReturn(JoinPoint joinPoint, Object result) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: " + method.getName() + ", 返回结果为: " + result.toString());}@AfterThrowing(value = "authornizeCut()", throwing = "e")public void afterThrow(JoinPoint joinPoint, Exception e) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("AOP进入方法异常拦截, 方法名为: " + method.getName() + ", 异常信息为: " + e.getMessage());}
}
  1. 来一个Controller作为请求被代理的对象
@RestController
@RequestMapping("/company")
public class CompanyController {@GetMapping("/aopTest")@Authorizationpublic Object aopTest(@RequestParam String name){//远程调用System.out.println("执行接口name:" + name);// int n = 1/0;return "成功了-----" + name;}
}
  1. 启动类启动项目
@SpringBootApplication
public class StudyTestApplication {public static void main(String[] args) {SpringApplication.run(StudyTestApplication.class, args);}
}

测试验证

  1. 浏览器或者api调用工具调接口
    在这里插入图片描述
  2. 控制台打印
AOP拦截开始进入环绕通知.......
AOP开始拦截, 当前拦截的方法名: aopTest
执行接口name:zhuyh
AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: aopTest, 返回结果为: 成功了-----zhuyh
AOP执行的方法 :aopTest 执行完了
准备退出环绕......
  1. Controller加入一个异常 int i=1/0; 重启项目再次调用
    异常通知打印了 / by zero
AOP拦截开始进入环绕通知.......
AOP开始拦截, 当前拦截的方法名: aopTest
执行接口name:zhuyh
AOP进入方法异常拦截, 方法名为: aopTest, 异常信息为: / by zero
AOP执行的方法 :aopTest 执行完了
2024-01-24 16:50:10.477 ERROR 4888 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ArithmeticException: / by zero] with root causejava.lang.ArithmeticException: / by zeroat com.zhuyh.studytest.spring5.aop.CompanyController.aopTest(CompanyController.java:16) ~[classes/:na]

感谢阅读

谢谢您的陪伴! 如果您有任何问题、建议或想要了解的特定主题,请随时在评论中告诉我们。期待与您共同探索java,共同提升我们的Java开发技能!

http://www.lryc.cn/news/287699.html

相关文章:

  • Golang 中高级工程师学习笔记
  • USB-C接口给显示器带来怎样的变化?
  • 写一份简单的产品说明书:格式和排版建议
  • 【Python学习】Python学习21- 正则表达式(1)
  • Docker 和 Kubernetes:容器化时代的崛起与演变
  • 美易官方京东养车回应索赔事件:推动行业健康发展并携手品牌商家加码补贴
  • 深度学习与图像描述生成——看图说话(3)
  • [SAP ABAP] ABAP编程中SY-SUBRC值的含义
  • 测试模型分类
  • mavros和PX4中的海拔高与椭球高转换
  • 洛谷刷题-【入门2】分支结构
  • 文件包含技术总结
  • Docker搭建私有仓库
  • 【计算机网络】【练习题】【新加坡南洋理工大学】【Computer Control Network】
  • 【学习笔记】CF1349F2 Slime and Sequences (Hard Version)
  • HarmonyOS 鸿蒙应用开发( 六、实现自定义弹窗CustomDialog)
  • # Java NIO(一)FileChannel
  • [嵌入式软件][启蒙篇][仿真平台] STM32F103实现串口输出输入、ADC采集
  • Deepin基本环境查看(四)【硬盘/分区、文件系统、硬连接/软连接】
  • JS之打地鼠案例
  • Kubernetes入门
  • EtherNet/IP开发:C++搭建基础模块,EtherNet/IP源代码
  • Django(九)
  • 解决Android Studio Unexpected tokens (use ; to separate expressions on the same line)
  • 【云原生】Docker网络模式和Cgroup资源限制
  • 实战:加密传输数据解密
  • 前端开发提高效率的两大工具
  • 探索设计模式的魅力:深入理解面向对象设计的深层原则与思维
  • 【Py/Java/C++三种语言详解】LeetCode每日一题240122【贪心】LeetCode670、最大交换
  • Linux/Doctor