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

这篇具有很好参考价值的文章主要介绍了Spring实战 | Spring AOP核心秘笈之葵花宝典。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spring实战系列文章:

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

国庆中秋特辑系列文章:

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

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

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

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

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

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

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

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

Spring AOP(Aspect-Oriented Programming,面向切面编程)是 Spring 框架的一个重要模块。
Spring实战 | Spring AOP核心秘笈之葵花宝典,spring,java,后端,开发语言

一、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() 方法,那么生成的代理类将无法正确处理目标类的对象。文章来源地址https://www.toymoban.com/news/detail-713742.html

到了这里,关于Spring实战 | Spring AOP核心秘笈之葵花宝典的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【运维安全】运维界葵花宝典:Nginx配置与优化秘籍

    必要的原理介绍 ● Nginx 里有一个master进程和多个worker进程.master进程并不处理网络请求,主要负责调度工作进程: 加载配置,启动工作进程及非停升级.worker进程负责处理网络请求与响应. ● master进程主要用来管理worker进程,具体包括如下4个主要功能: 接收来自外界的信号 向各wo

    2024年02月21日
    浏览(40)
  • 【送书福利-第十六期】Spring Cloud Alibaba 核心技术宝典

    大家好,我是洲洲,欢迎关注,一个爱听周杰伦的程序员。关注公众号【程序员洲洲】即可获得10G学习资料、面试笔记、大厂独家学习体系路线等…还可以加入技术交流群欢迎大家在CSDN后台私信我! 今天洲洲给大家推荐一本开发实战好书:《Spring Cloud Alibaba核心技术宝典》,

    2024年02月12日
    浏览(34)
  • Spring核心思想之IOC和AOP

    IOC和AOP不是Spring提出的,在spring之前就已经存在,只不过更偏向于理论化,Spring在技术层次把这两个思想做了⾮常好的实现(Java)。 什么是IoC? IoC Inversion of Control (控制反转/反转控制),注意它是⼀个 技术思想 ,不是⼀个技术实现。 描述的事情 :Java开发领域对象的创建,

    2024年02月09日
    浏览(45)
  • 【Java 初级】Spring核心之面向切面编程(AOP)

    tip:作为程序员一定学习编程之道,一定要对代码的编写有追求,不能实现就完事了。我们应该让自己写的代码更加优雅,即使这会费时费力。 💕💕 推荐: 体系化学习Java(Java面试专题) AOP(面向切面编程)是一种编程范式,用于将横切关注点(如日志记录、性能统计等

    2024年02月04日
    浏览(44)
  • Java spring Aop实战

    Spring AOP 1. 实战 1.实战 创建工程和依赖 数据库建表 实体类 Mapper 接口 方法一 方法二 Service 包 接口1: 实现接口 Mapper Mapper 1 Mapper 2 配置xml文件 Xml 1 Xml 2 Spring 配置文件 Mybatis配置文件 测试类 数据库结果 测试2 测试3 不符合业务逻辑,加入事物 头文件 只能在service实现类加 重

    2024年02月15日
    浏览(33)
  • 【Spring教程16】Spring框架实战:详解解读AOP配置管理中AOP切入点表达式和通知类型

    欢迎大家回到《 Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《 如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《 AOP的工作流程和AOP的核心概念》 前面的案例中,有涉及到如下内容: 对于

    2024年02月04日
    浏览(33)
  • 【Spring教程21】Spring框架实战:Spring事务简介、AOP事务管理、代码示例全面详解

    欢迎大家回到《Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《AOP(面对切面编程)知识总结》 事务作用:在数据层保障一系列的数据库

    2024年02月04日
    浏览(39)
  • 【Spring教程18】Spring框架实战:利用Aop测定业务层接口执行效率代码实例详解

    欢迎大家回到《Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《详解解读AOP通知类型的使用》 这个需求也比较简单,前面我们在介绍AOP的

    2024年02月05日
    浏览(36)
  • Java实战:Spring Boot实现AOP记录操作日志

    本文将详细介绍如何在Spring Boot应用程序中使用Aspect Oriented Programming(AOP)来实现记录操作日志的功能。我们将探讨Spring Boot集成AOP的基本概念,以及如何使用Spring Boot实现AOP记录操作日志。最后,我们将通过一个具体示例来演示整个实现过程。本文适合已经具备Spring Boot基础

    2024年02月22日
    浏览(47)
  • 【Spring AOP + 自定义注解 + 动态数据源 实现主从库切换&读写分离】—— 案例实战

                                                 💧 S p r i n g A O P + 主从数据源切换 + 读写分离 + 自定义注解案例实战! color{#FF1493}{Spring AOP + 主从数据源切换 + 读写分离 + 自定义注解 案例实战!} Sp r in g A OP + 主从数据源切换 + 读写分离 + 自定义注解案例

    2024年02月15日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包