Spring实战 | Spring AOP核心秘笈之葵花宝典

news2024/10/7 14:22:57

Spring实战系列文章:

Spring实战 | Spring IOC不能说的秘密?

国庆中秋特辑系列文章:

国庆中秋特辑(八)Spring Boot项目如何使用JPA

国庆中秋特辑(七)Java软件工程师常见20道编程面试题

国庆中秋特辑(六)大学生常见30道宝藏编程面试题

国庆中秋特辑(五)MySQL如何性能调优?下篇

国庆中秋特辑(四)MySQL如何性能调优?上篇

国庆中秋特辑(三)使用生成对抗网络(GAN)生成具有节日氛围的画作,深度学习框架 TensorFlow 和 Keras 来实现

国庆中秋特辑(二)浪漫祝福方式 使用生成对抗网络(GAN)生成具有节日氛围的画作

国庆中秋特辑(一)浪漫祝福方式 用循环神经网络(RNN)或长短时记忆网络(LSTM)生成祝福诗词

目录

  • 一、Spring AOP 简介
  • 二、Spring AOP 原理
  • 三、Spring AOP 案例分析
  • 四、Spring AOP 提供了两种动态代理方式

Spring AOP(Aspect-Oriented Programming,面向切面编程)是 Spring 框架的一个重要模块。
在这里插入图片描述

一、Spring AOP 简介

Spring AOP(Aspect-Oriented Programming,面向切面编程)是 Spring 框架的一个重要模块,用于提供声明式的事务管理、日志记录、性能监控等功能。Spring AOP 底层依赖于 AspectJ 实现,可以与 Spring 框架无缝集成,提供一种更加简单、直观的方式来处理企业应用中的常见问题。

二、Spring AOP 原理

  1. 代理机制
    Spring AOP 采用代理机制实现,可以分为 JDK 动态代理和 CGLIB 动态代理。JDK 动态代理是通过实现目标类的接口,生成目标类的代理对象;CGLIB 动态代理是通过继承目标类,生成目标类的子类作为代理对象。
  2. 通知(Advice)
    通知是 Spring AOP 中实现切面功能的核心,可以分为五种类型:Before、After、AfterReturning、AfterThrowing 和 Around。通知的作用是在目标方法执行前、后或者抛出异常时执行特定的逻辑,实现对目标方法的增强。
  3. 切入点(Pointcut)
    切入点是 Spring AOP 中定义的一个表达式,用于指定哪些方法需要被增强。切点表达式可以使用 AspectJ 语言来编写,非常灵活。通过定义切入点,可以精确地控制哪些方法需要被增强。
  4. 切面(Aspect)
    切面是 Spring AOP 中的一种组件,包含切点和通知。切面可以将通用的逻辑(如日志、事务管理等)封装在一起,便于管理和维护。在 Spring AOP 中,可以通过 XML 配置文件或者 Java 代码来定义切面。
  5. 自动代理
    Spring AOP 框架支持自动代理,可以在运行时自动为指定类生成代理对象。自动代理的核心是 Spring AOP 容器,负责管理代理对象、切面和通知。通过自动代理,可以简化开发者的操作,提高开发效率。

三、Spring AOP 案例分析

以下通过一个简单的案例来演示 Spring AOP 的使用。

  1. 配置文件
    首先,创建一个配置文件 applicationContext.xml,用于定义目标类和切面类。
<beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns:aop="http://www.springframework.org/schema/aop"  
      xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans.xsd  
      http://www.springframework.org/schema/aop  
      http://www.springframework.org/schema/aop/spring-aop.xsd">
   <!-- 定义目标类 -->  
   <bean id="target" class="com.example.TargetClass"></bean>
   <!-- 定义切面类 -->  
   <bean id="aspect" class="com.example.AspectClass"></bean>
   <!-- 开启自动代理 -->  
   <aop:config proxy-target-class="true">  
       <!-- 指定切入点表达式 -->  
       <aop:aspect ref="aspect">  
           <aop:before pointcut="execution(* com.example.TargetClass.*(..))" method="com.example.AspectClass.beforeAdvice"></aop:before>  
           <aop:after pointcut="execution(* com.example.TargetClass.*(..))" method="com.example.AspectClass.afterAdvice"></aop:after>  
       </aop:aspect>  
   </aop:config>  
</beans>  
  1. 目标类(TargetClass)
    目标类是一个简单的计算类,包含两个方法:doAdd 和 doSubtract。
package com.example;
public class TargetClass {  
   public int doAdd(int a, int b) {  
       System.out.println("TargetClass doAdd method called");  
       return a + b;  
   }
   public int doSubtract(int a, int b) {  
       System.out.println("TargetClass doSubtract method called");  
       return a - b;  
   }  
}
  1. 切面类(AspectClass)
    切面类包含两个通知方法:beforeAdvice 和 afterAdvice,分别用于在目标方法执行前和执行后执行特定逻辑。
package com.example;
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.After;
public class AspectClass {  
   @Before("execution(* com.example.TargetClass.*(..))")  
   public void beforeAdvice(JoinPoint joinPoint) {  
       System.out.println("Before advice: " + joinPoint.getSignature().getName());  
   }
   @After("execution(* com.example.TargetClass.*(..))")  
   public void afterAdvice(JoinPoint joinPoint) {  
       System.out.println("After advice: " + joinPoint.getSignature().getName());  
   }  
}
  1. 测试类(TestClass)
    测试类用于测试 Spring AOP 的效果。
package com.example;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClass {  
   public static void main(String[] args) {  
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
       TargetClass target = (TargetClass) context.getBean("target");
       int result1 = target.doAdd(2, 3);  
       int result2 = target.doSubtract(5, 2);
       System.out.println("Result 1: " + result1);  
       System.out.println("Result 2: " + result2);  
   }  
}

运行测试类,输出结果如下:

TargetClass doAdd method called  
Before advice: doAdd  
After advice: doAdd  
Result 1: 5  
TargetClass doSubtract method called  
Before advice: doSubtract  
After advice: doSubtract  
Result 2: 3  

从输出结果可以看出,在目标方法执行前和执行后分别执行了 beforeAdvice 和 afterAdvice 方法,说明 Spring AOP 已经成功实现了对目标方法的增强。

四、Spring AOP 提供了两种动态代理方式

JDK 动态代理和 CGLIB 动态代理。JDK 动态代理是基于接口实现的,而 CGLIB 动态代理是基于类实现的。这两种代理方式在性能上有一定的差别,JDK 动态代理更适合用于接口较多的场景,而 CGLIB 动态代理则更适合用于类较多的场景。
4.1 下面是一个简单的 Spring AOP JDK 动态代理示例,演示了如何使用 Spring AOP 实现日志切面:

  1. 首先,创建一个切面类(Aspect),包含一个通知(Advice):
package com.example.aspect;
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;
@Aspect  
@Component  
public class LoggingAspect {
   @Pointcut("execution(* com.example.service.*.*(..))")  
   public void serviceMethods() {  
   }
   @Before("serviceMethods()")  
   public void logBefore(JoinPoint joinPoint) {  
       System.out.println("Before method: " + joinPoint.getSignature().getName());  
   }
   @AfterReturning(pointcut = "serviceMethods()", returning = "result")  
   public void logAfterReturning(JoinPoint joinPoint, Object result) {  
       System.out.println("After returning method: " + joinPoint.getSignature().getName());  
       System.out.println("Result: " + result);  
   }  
}
  1. 接下来,创建一个目标类(Target Class),包含一个需要增强的方法:
package com.example.service;
import org.springframework.stereotype.Service;
@Service  
public class TargetService {
   public String sayHello(String name) {  
       System.out.println("Hello, " + name);  
       return "Hello, " + name;  
   }  
}
  1. 然后,创建一个 Spring 配置类,启用 AOP 支持,并扫描包含切面和目标类的包:
package com.example;
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration  
@EnableAspectJAutoProxy  
@ComponentScan(basePackages = {"com.example.aspect", "com.example.service"})  
public class AppConfig {  
}
  1. 最后,创建一个测试类,使用 Spring AOP 提供的 API 调用目标类的方法:
package com.example;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
import org.springframework.stereotype.Component;
@Component  
public class Test {
   public static void main(String[] args) {  
       ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);  
       TargetService targetService = context.getBean(TargetService.class);  
       String result = targetService.sayHello("World");  
       System.out.println("Result: " + result);  
   }  
}

运行测试类,你将看到目标方法被切面增强的日志输出。这个示例展示了如何使用 Spring AOP JDK 动态代理实现简单的日志切面,以记录目标方法执行的前后状态。这有助于实现代码的重用和提高可维护性。

4.2 下面是一个简单的 Spring AOP CGLIB 动态代理示例,演示了如何使用 Spring AOP 实现日志切面:

  1. 首先,创建一个切面类(Aspect),包含一个通知(Advice):
package com.example.aspect;
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;
@Aspect  
@Component  
public class LoggingAspect {
   @Pointcut("execution(* com.example.service.*.*(..))")  
   public void serviceMethods() {  
   }
   @Before("serviceMethods()")  
   public void logBefore(JoinPoint joinPoint) {  
       System.out.println("Before method: " + joinPoint.getSignature().getName());  
   }
   @AfterReturning(pointcut = "serviceMethods()", returning = "result")  
   public void logAfterReturning(JoinPoint joinPoint, Object result) {  
       System.out.println("After returning method: " + joinPoint.getSignature().getName());  
       System.out.println("Result: " + result);  
   }  
}
  1. 接下来,创建一个目标类(Target Class),包含一个需要增强的方法:
package com.example.service;
import org.springframework.stereotype.Service;
@Service  
public class TargetService {
   public String sayHello(String name) {  
       System.out.println("Hello, " + name);  
       return "Hello, " + name;  
   }  
}
  1. 然后,创建一个 Spring 配置类,启用 AOP 支持,并扫描包含切面和目标类的包:
package com.example;
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration  
@EnableAspectJAutoProxy  
@ComponentScan(basePackages = {"com.example.aspect", "com.example.service"})  
public class AppConfig {  
}
  1. 最后,创建一个测试类,使用 Spring AOP 提供的 API 调用目标类的方法:
package com.example;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
import org.springframework.stereotype.Component;
@Component  
public class Test {
   public static void main(String[] args) {  
       ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);  
       TargetService targetService = context.getBean(TargetService.class);  
       String result = targetService.sayHello("World");  
       System.out.println("Result: " + result);  
   }  
}

运行测试类,你将看到目标方法被切面增强的日志输出。这有助于实现代码的重用和提高可维护性。
需要注意的是,CGLIB 动态代理需要 TargetService 类实现 equals() 和 hashCode() 方法,否则会报错。这是因为 CGLIB 需要生成目标类的代理类,而如果 TargetService 类没有实现 equals() 和 hashCode() 方法,那么生成的代理类将无法正确处理目标类的对象。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1090738.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

IDEA的常用设置

【1】进入设置&#xff1a; 【2】设置主题&#xff1a; 【3】编辑区的字体变大或者变小&#xff1a; 【4】鼠标悬浮在代码上有提示&#xff1a; 【5】自动导包和优化多余的包&#xff1a; 手动导包&#xff1a;快捷键&#xff1a;altenter 自动导包和优化多余的包&#xf…

计算机网络第2章-HTTP和Web协议(2)

Web和HTTP 一个新型应用即万维网&#xff08;World Wide Web&#xff09;Web。 HTTP概况 Web的应用层协议是超文本传输协议&#xff08;HTPP&#xff09;&#xff0c;它是Web的核心。 HTTP由两个程序实现&#xff1a;一个用户程序和一个服务器程序。 Web页面&#xff08;W…

leetcode-518. 零钱兑换 II

1. 题目 链接: 零钱兑换II 2. 解决方案1 #include <stdio.h> #include <stdlib.h>int change(int amount, int* coins, int coinsSize){int dp[amount1];//确定dp大小memset(dp, 0, sizeof(int) * (amount1));dp[0] 1;//初始化为0for(int i 0 ; i < coins…

LED电子屏幕可以通过什么方式进行人屏互动

传统的LED大屏幕以单向传播的形式面向观众&#xff0c;不仅被动&#xff0c;而且逐渐缺乏动感和创新。随着LED显示技术的蓬勃发展&#xff0c;现在观众与LED电子大屏幕的方式越来越多。那么现阶段实现LED显示屏人屏互动的主要方式都有哪些呢&#xff1f;带你8分钟了解LED互动地…

KASan介绍

目录 概括介绍 配置说明 单独关闭读或写检查 操作使用 影响及注意事项 结果解读 使用注意 实现原理简介 KASAN原理 malloc原理 内容参考 概括介绍 KernelAddressSANitizer &#xff08;KASAN&#xff09; 是一个动态内存错误检测器。它提供了一个快速而全面的解决方…

D课堂 | 如何设置域名解析?解析记录类型选哪个?

上回&#xff0c;D妹和各位小伙伴们介绍了DNS的作用和原理——《什么是DNS&#xff1f;DNS是怎么运作的&#xff1f;》&#xff0c;相信大家对DNS已经有了一定的认识。 DNS是互联网不可或缺的基础服务&#xff0c;核心作用是将域名翻译成计算机可读取的IP地址&#xff0c;也就是…

VMware搭载linux出现的bugs

---------后续在实际Linux项目复盘过程中有遇到问题(解决办法)会不定时更新.......----------- ques: Linux自带的media目录用于挂载或可移动存储设备已满&#xff08;造成这一原因是由于我多次创建新的虚拟机并在同一虚拟目录下挂载同一镜象导致有些残存文件没有删除干净&…

【OpenCv光流法进行运动目标检测】

opencv系列文章目录 文章目录 opencv系列文章目录前言一、光流法是什么&#xff1f;二、光流法实例1.C的2.C版本3.python版本 总结 前言 随着计算机视觉技术的迅猛发展&#xff0c;运动目标检测在图像处理领域中扮演着至关重要的角色。在现实世界中&#xff0c;我们常常需要追…

JDK21要来了,协程对Java带来什么

目录 前言 协程是什么 多线程有什么问题&#xff1f; 协程的线程模型 Reactor模型 使用协程后 RPC并发 IO阻塞 网络IO 磁盘IO epoll为什么不支持磁盘io&#xff1f; Kotlin与Go的协程 Go 使用 Go的协程调度(GPM模型) Kotlin 使用 Kotlin协程调度 阿里Wisp协程…

Linux程序调试工具使用整理

Linux程序调试工具使用整理 GDB调试入门 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许&#xff0c;各位比较喜欢那种图形界面方式的&#xff0c;像VC、BCB等IDE的调试&#xff0c;但如果你是在 UNIX平台下做软件&#xff0c;你会发现GDB这个调试工具有比VC、…

万界星空科技可视化数字大屏应用场景及作用

一、MES系统大屏显示&#xff1a;实时监控生产数据的关键 随着制造业的发展&#xff0c;现代企业越来越依赖于高效的生产管理系统来保证生产效率和质量。其中&#xff0c;MES系统数据大屏显示成为了监控生产数据的关键工具。通过实时监控和显示生产数据&#xff0c;企业能够及…

智能网关在校园能耗监测系统中的应用介绍

安科瑞 崔丽洁 摘要&#xff1a;国家提出了全社会节能减排的战略举措&#xff0c;节约型校园的建设是实现这一举措的重要内容。为了对校园能耗实行量化管理、实时监测&#xff0c;需要建立一个完善的监管体系校园节能监管体系。而节能监管体系的核心是能耗监测平台&#xff0c;…

解决react集成typescript报错:找不到名称“div“之类的错误

现象&#xff1a; 原因&#xff1a;Typescript 不希望在 Typescript 文件中看到 JSX元素。 解决此问题的最简单方法是将文件后缀从 .ts 重命名为 .tsx 。

【学习笔记】DTM分布式事务

分布式事务是什么 本文的分布式事务指的是DTM下的分布式事务。 分布式事务有两类&#xff0c;这里指的是跨数据库、跨服务的分布式事务。 分布式事务指事务的发起者、资源及资源管理器和事务协调者分别位于分布式系统的不同节点之上。 CAP理论 C&#xff08;一致性&#x…

【UVM 验证平台打印时间单位控制】

UVM 验证平台打印时间单位控制 UVM 具有丰富的打印功能&#xff0c;打印信息会包含时间/打印位置等信息&#xff0c;根据打印时间可以方便的在波形上找到错误点。默认打印时间单位时fs&#xff0c;由于单位太小会导致打印信息上的时间信息比较长&#xff0c;不方便查看与查找。…

实现Element Select选择器滚动加载

<template><el-selectpopper-class"more-tag-data"v-model"tagId"filterableplaceholder"请选择"focus"focusTag"><el-optionv-for"(item, index) in taskTagLists":key"index":label"item.n…

软件开发无人天车智能控制系统智能库存管理单元解决方案

天车&#xff08;行吊 起重机&#xff09;智控系统在自动控制的基础上&#xff0c;添加了基于智能控制、数据分析存储等尖端技术研发出的各类算法&#xff0c;赋予天车更“聪明”的任务执行及决策制定能力。智控系统能够根据获取的数据和预设的任务需求&#xff0c;通过智能决策…

ftp发布服务器

ftp工具 发布测试 第一步&#xff1a;下载FileZilla 第二部建立站点 连接成功之后可以看到文件了 项目打包后上传 远程站点里的文件删除 左边本地站点上传。over

淘天集团大模型应用十大挑战命题发布

以AI人工智能为代表的新技术正在成为全球商业发展的新动能。淘天集团从去年开始&#xff0c;就已经在AI重点领域&#xff0c;展开和高校的一系列深入合作。 近期&#xff0c;淘天集团集合基础模型和电商应用场景的具体问题&#xff0c;面向高校师生和全社会发布大模型应用十大挑…

【算法-贪心】分数背包问题

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kuan 的首页,持续学…