04、SpringAOP详解

article/2025/9/14 9:34:49

1、Spring AOP简介

1、什么是AOP

1、定义阐述

AOP的全称是 Aspect Oriented Programming,是面向切面编程的技术,把一个个的横切关注点放到某个模块中去,称之为切面。那么每一个的切面都能影响业务的某一种功能,切面的目的就是功能增强,如日志切面就是一个横切关注点,应用中许多方法需要做日志记录的只需要插入日志的切面即可。(动态代理就可以实现 AOP),这种面向切面编程的思想就是 AOP 思想了。

2、图示

在这里插入图片描述

3、好处

  • AOP 能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来。
  • 减少代码重复。
  • 降低模块之间的耦合度,有利于维护和拓展。

2、AOP术语

  • Aspect:切面,在实际应用中,通常指的是封装的用于横向插入系统功能的类。该类要被Spring容器识别为切面,需要在配置文件中进行指定。
  • Joinpoint:连接点,一般指的是要被增强的方法。
  • Pointcut:切入点,哪些包中的哪些类中的哪些方法想加增强方法。
  • Advice:(增强或通知处理):AOP框架在特定的切入点执行的增强处理。也就是在什么时候做什么增强处理。
  • Target Object(目标对象):是指所有被通知的对象,也称为被增强的对象,如果使用的动态的AOP实现,该对象是一个代理对象。
  • Proxy:代理:将通知应用到目标对象之后,被动创建代理对象。
  • Weaving:织入:将切面代码插入到目标对象之上,从而产生代理对象的过程。

3、AspectJ开发

1、什么是AspecJ

AspectJ 是一个面向切面的框架,它扩展了Java 语言(即使用 Java 对 AOP 进行了实现)。

2、AspectJ 切入点语法

在这里插入图片描述

3、切入点语法通配符

  • *:匹配任何部分,只能表示一个单词。
  • ..: 可用于全限定名中和方法参数中,分别表示子包和 0 到 N 个参数。

4、举例

// 注意第一个星符号后面有空格
execution(* cn.wolfcode.ssm.service.impl.*ServiceImpl.*(..))

4、基于XML配置的声明式AspectJ

1、<aop:config>元素及其子元素

在这里插入图片描述

2、创建一个Maven项目导入如下依赖

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.8.RELEASE</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency></dependencies>

4、提供一个service接口

package cn.simplelife.service;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/23 10:50* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}

5、书写接口实现类

package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/23 10:51* @Version 1.0*/public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存:" + name + " " + password);}
}

6、书写增强方法

package cn.simplelife.utils;/*** @ClassName MyTransactionManger* @Description* @Author simplelife* @Date 2022/11/23 10:52* @Version 1.0*/public class MyTransactionManger {public void begin() {System.out.println("开启事务");}public void commit() {System.out.println("提交事务");}public void rollback() {System.out.println("回滚事务");}
}

7、书写配置

<?xml version="1.0" encoding="UTF-8"?>
<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="myTransactionManger" class="cn.simplelife.utils.MyTransactionManger"/><!--配置业务对象:此时的对象不再是真实对象,而是springAOP创建的代理对象--><bean id="employeeService" class="cn.simplelife.service.impl.IEmployeeServiceImpl"/><!--AOP配置 WHERE WHEN WHAT proxy-target-class="true" 改用底层使用CJLB动态代理--><aop:config><!--切面配置ref:将事务管理与切面关联--><aop:aspect ref="myTransactionManger"><!--切入点配置--><aop:pointcut id="txPointcut" expression="execution(* cn.simplelife.service.impl.*ServiceImpl.*(..))"/><!-- 关联三者:在业务方法执行之前,在容器中找到myTransactionManger对象,调用其begin方法--><aop:before pointcut-ref="txPointcut" method="begin"/><!-- 关联三者:在业务方法执行正常执行,在容器中找到myTransactionManger对象,调用其commit方法--><aop:after-returning pointcut-ref="txPointcut" method="commit"/><!-- 关联三者:在业务方法抛出异常之后,在容器中找到myTransactionManger对象,调用其rollback方法--><aop:after-throwing pointcut-ref="txPointcut" method="rollback" /></aop:aspect></aop:config>
</beans>

8、编写测试类

package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;/*** @ClassName IEmployeeServiceImplTest* @Description* @Author simplelife* @Date 2022/11/23 11:05* @Version 1.0*/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceImplTest {@Autowiredprivate IEmployeeService iEmployeeService;@Testpublic void save() {System.out.println(iEmployeeService.getClass());iEmployeeService.save("张三", "123456");}
}

9、测试结果

在这里插入图片描述
内存解释:
在这里插入图片描述

5、基于注解配置AOP

2、创建一个Maven项目导入如下依赖

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.8.RELEASE</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency></dependencies>

4、提供一个service接口

package cn.simplelife.service;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/23 10:50* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}

5、书写接口实现类

package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/23 10:51* @Version 1.0*/
@Service
public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存:" + name + " " + password);}
}

6、书写增强方法

package cn.simplelife.utils;import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;/*** @ClassName MyTransactionManger* @Description* @Author simplelife* @Date 2022/11/23 10:52* @Version 1.0*/@Component
@Aspect
public class MyTransactionManger {@Pointcut("execution(* cn.simplelife.service.impl.*ServiceImpl.*(..))")public void txPoint() {}@Before("txPoint()")public void begin() {System.out.println("开启事务");}@AfterReturning("txPoint()")public void commit() {System.out.println("提交事务");}@AfterThrowing("txPoint()")public void rollback() {System.out.println("回滚事务");}
}

7、配置文件修改

<?xml version="1.0" encoding="UTF-8"?>
<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"xmlns:context="http://www.springframework.org/schema/context"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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--配置Ioc di注解解析器--><context:component-scan base-package="cn.simplelife"/><!--配置AOP的注解解析器--><aop:aspectj-autoproxy />
</beans>

8、书写测试类

package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;/*** @ClassName IEmployeeServiceImplTest* @Description* @Author simplelife* @Date 2022/11/23 11:05* @Version 1.0*/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceImplTest {@Autowiredprivate IEmployeeService iEmployeeService;@Testpublic void save() {System.out.println(iEmployeeService.getClass());iEmployeeService.save("张三", "123456");}
}

9、测试结果

在这里插入图片描述

10、相关注解解释

注解描述
@Aspect用于定义一个切面
@Pointcut用于定义切点表达式
@Before用于定义前置通知
@AfterReturning用于定义后置通知
@AfterThrowing用于定义异常时通知
@Around用于定义环绕通知
@After用于定义最终通知

http://chatgpt.dhexx.cn/article/UCIwsR8v.shtml

相关文章

SpringAop之joinPoint讲解

一、学习背景 摸鱼的时候继续复刻demo&#xff0c;没错&#xff0c;同之前一篇文章&#xff0c;在写aop时又发现自己对aop只停留在面试阶段&#xff0c;甚至还不如&#xff0c;完全不会实践&#xff0c;所以在此记录复刻aop用到的的一些且自己已经遗忘的知识。 那么复刻的一个…

SpringAOP详细配置与使用

目录 SpringAOP简介 AOP概念 Spring AOP简单流程图 Spring AOP之Annotation 前置通知(Before advice) 返回后通知(After reurning advice) 抛出异常后通知(After throwing advice) 后置通知(After (finally) advice) 环绕通知(Around advice) 引入(Introduction) Sp…

SpringAOP的注解形式

铁子们&#xff0c;快扫码关注啦&#xff01;或 wx搜索&#xff1a;“聊5毛钱的java”&#xff0c;关注可领取博主的Java学习视频资料&#xff0c;保证都是干货 上一篇讲了配置文件形式的SpringAOP&#xff1a;Spring中的AOP以及切入点表达式和各种通知 本篇继续看一下注解形…

Spring AOP超详细解析

AOP - 面向切面编程&#xff08;Aspect Oriented Programming&#xff09; Spring早期版本的核心功能&#xff1a;管理对象生命周期与对象分配。 即Bean本身的管理创建&#xff0c;以及它整个生命周期里跟其他对象相互之间引用装配 为了更好的实现管理和装配&#xff0c;一个…

Spring学习:AOP概述

一、AOP概念 AOP是指面向切面编程&#xff0c;利用 AOP 可以对业务逻辑的各个部分进行隔离&#xff0c;从而使得业务逻辑各部分之间的耦合度降低&#xff0c;提高程序的可重用性&#xff0c;同时提高了开发的效率。 通俗描述&#xff1a;不通过修改源代码方式&#xff0c;在主干…

SpringAOP学习--SpringAOP简介及原理

前文对AOP做了介绍&#xff0c;实际项目中&#xff0c;一般不会直接上手手动实现aop&#xff0c;而是使用一些高级封装的aop实现&#xff0c;如SpringAOP。 Spring是一个广泛应用的框架&#xff0c;SpringAOP则是Spring提供的一个标准易用的aop框架&#xff0c;依托Spring的IOC…

图文详解Spring AOP,你学会了吗?

如果说 IOC 是 Spring 的核心&#xff0c;那么面向切面编程AOP就是 Spring 另外一个最为重要的核心&#xff0c;需要重点掌握mikechen 本篇主要会详解以下六点&#xff1a; 1.AOP的定义 2.AOP的作用 3.AOP的应用场景 4.Spring AOP的术语 AOP核心概念Spring AOP 通知分类S…

Spring AOP全面详解(超级详细)

如果说IOC 是 Spring 的核心&#xff0c;那么面向切面编程AOP就是 Spring 另外一个最为重要的核心mikechen AOP的定义 AOP &#xff08;Aspect Orient Programming&#xff09;,直译过来就是 面向切面编程,AOP 是一种编程思想&#xff0c;是面向对象编程&#xff08;OOP&…

mysql执行SQL脚本

方法一 【Mysql的bin目录】\mysql –u用户名 –p密码 –D数据库<【sql脚本文件路径全名】 示例&#xff1a; 如果mysql配了全局变量&#xff0c;就不需要到Mysql的bin目录下执行&#xff0c;可以在任何地方使用用户名、密码、指定数据库等参数值与参数名不需要隔空格 不…

SpringBoot 实现SQL脚本自动执行

SpringBoot 实现配置SQL脚本自动执行 一. 背景 我们可能遇到过这种情况: 在公网开发时, 新增数据表非常容易, 直接登录到对应服务器的mysql / 使用Navicat访问mysql服务器. 然后去执行sql语句或脚本即可在内网开发时, 由于都在一个网段, 所以操作也比较方便但是在公网开发, 部…

flink-sql-client提交sql脚本文件

标题: flink-sql-client提交sql脚本文件 日期: 2021-10-22 22:11:34 标签: [flink,sql-client] 分类: flink 我们知道&#xff0c;sql-client.sh可以提供给我们一个sql交互界面&#xff0c;让我们没执行一个sql&#xff0c;就可以看到执行结果&#xff0c;也可以交互式查询表的…

如何在mysql中执行sql脚本文件

一、sql脚本文件 简介 xxxx.sql这种文件被称为sql脚本文件。sql脚本文件中编写了大量的sql语句。我们执行sql脚本文件的时候&#xff0c;该文件中所有的sql语句会全部执行&#xff01;批量的执行SQL语句&#xff0c;可以使用sql脚本文件。 上面这个vip.sql就是sql脚本文件&am…

使用sql脚本创建数据库表

准备脚本语句&#xff1a; CREATE TABLE test (title varchar(100) DEFAULT NULL,author varchar(10) DEFAULT NULL,digest varchar(255) DEFAULT NULL,content text,content_source_url varchar(500) DEFAULT NULL,thumb_media_id varchar(255) DEFAULT NULL,need_open_comme…

PowerDesigner生成Sql脚本

点击工具栏上的“Database”&#xff0c;选择“Change Current DBMS”进行修改导出脚本类型&#xff0c;可以选择mysql、sql server/ oracle 、db2等主流的数据库。 在DBMS中点击下拉菜单&#xff0c;选择要导出的数据库脚本&#xff0c;对名字进行自定义&#xff0c;点击确定即…

PowerDesigner导入sql脚本

一个好的数据库建模,不但可以让人直观的理解模型,充分的利用数据库技术,优化数据库的设计,而且还可以让新员工快速的熟悉数据库表结构与业务之间的关系.无奈的是随着开发过程中,数据库表结构字段的增删以及关联关系的变动给数据库模型带来维护上的巨大工作量.现为了维护上的简单…

dbeaver导入sql脚本

新建数据库 执行脚本 选择脚本文件 选择mysql 然后按确定就行了

springboot + mybatis启动时执行sql脚本

目录 1. 创建数据版本表&#xff0c;结构如下&#xff1a; 2. 创建HdVersion对象 3. 创建执行sql的dao 4. 创建dao对应的xml 5.创建sql执行器&#xff0c;实现ApplicationRunner 6. 结语 背景&#xff1a;项目开发或发布阶段修改表结构&#xff0c;项目更新时需要手动执行脚…

SpringBoot启动自动执行sql脚本

在开发当中我们每次发布服务都需要手动执行脚本&#xff0c;然后重启服务&#xff0c;而SpringBoot有服务启动自动执行sql脚本的功能的&#xff0c;可以为我们省去手动执行脚本的这一步&#xff0c;只需要部署新的服务即可。 这个功能是SpringBoot自带的不需要引入额外的依赖&a…

Excel数据转化为sql脚本

在实际项目开发中&#xff0c;有时会遇到客户让我们把大量Excel数据导入数据库的情况。这时我们就可以通过将Excel数据转化为sql脚本来批量导入数据库。 1 在数据前插入一列单元格&#xff0c;用来拼写sql语句。 具体写法&#xff1a;"insert into t_student (id,name,ag…

MySQL导出sql脚本文件

⭐️前言⭐️ sql脚本文件在我们做项目时&#xff0c;特别是学习别人的开源项目时经常需要进行导入导出操作&#xff0c;才能在自己的系统上跑起来&#xff0c;这篇文章主要介绍如何导出sql脚本文件&#xff0c;具体操作如下&#xff0c;附带截图详解。 &#x1f349;博客主页…