04、SpringAOP详解

news2025/8/3 0:00:40

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 {

    @Override
    public 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 {

    @Autowired
    private IEmployeeService iEmployeeService;

    @Test
    public 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 {

    @Override
    public 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 {

    @Autowired
    private IEmployeeService iEmployeeService;

    @Test
    public void save() {
        System.out.println(iEmployeeService.getClass());
        iEmployeeService.save("张三", "123456");
    }
}

9、测试结果

在这里插入图片描述

10、相关注解解释

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

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

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

相关文章

蒙泰转债上市价格预测

蒙泰转债基本信息转债名称&#xff1a;蒙泰转债&#xff0c;评级&#xff1a;A&#xff0c;发行规模&#xff1a;3.0亿元。正股名称&#xff1a;蒙泰高新&#xff0c;今日收盘价&#xff1a;31.3&#xff0c;转股价格&#xff1a;26.15。当前转股价值 转债面值 / 转股价格 * 正…

【Java进阶】学好常用类,code省时省力

一、工具类 所谓工具类&#xff0c;即将完成通用功能的方法分类放到类中&#xff0c;工具类能够被高效地重复使用&#xff0c;使我们的编码快速、高效。 工具类的设计 工具方法使用public static修饰&#xff0c;通过工具类名调用工具方法。对于工具类&#xff0c;我们通常都…

AI内容生成时代:该如何和AI对话?

北大出版社&#xff0c;人工智能原理与实践 人工智能和数据科学从入门到精通 详解机器学习深度学习算法原理 人工智能原理与实践 全面涵盖人工智能和数据科学各个重要体系经典 AI自动生成内容&#xff08;AIGC)最近可以说非常热门。而如何给AI有效输入提示&#xff0c;从而达…

基于JSP的保险业务管理系统【数据库设计、源码、开题报告】

数据库脚本下载地址&#xff1a; https://download.csdn.net/download/itrjxxs_com/86467452 主要使用技术 SpringStruts2HibernateJSPJSCSSMysql 功能介绍 本系统旨在为当今的保险行业提供一套综合性的管理系统业务&#xff0c;系统的主要用户为保险的购买者以及系统的管理…

安信可Ai-WB1系列固件烧录指导

文章目录前言1 准备材料2 硬件连接3 烧录软件的使用联系我们前言 本文主要介绍如何使用Ai-WB1系列模组以及开发板更新固件烧录操作说明。 1 准备材料 AI-WB1系列模组或者开发板USB转TTL模块/Type-C数据线固件详见链接常见固件中的出厂固件串口工具链接烧录工具详见链接 烧录…

在github上部署静态页面

使用github-page部署静态页面 需求 假如你辛辛苦苦写好了一个静态网页&#xff0c;很想要炫耀一下&#xff0c;让大家都可以通过公网访问看到我的网页。但是不想太麻烦&#xff0c;买服务器&#xff0c;安装软件&#xff0c;部署环境&#xff0c;配置域名&#xff0c;备案&…

navicate的安装使用

1 navicat概述 Navicat for MySQL 是管理和开发 MySQL 或 MariaDB 的理想解决方案。这套全面的前端工具为数据库管理、开发和维护提供了一款直观而强大的图形界面。官网&#xff1a; http://www.navicat.com.cn 2 navicat安装 网上有教程 3 navicat使用 3.1 建立和mysql服务…

金融行业数据安全法律法规清单

近年来&#xff0c;随着业务快速发展&#xff0c;金融机构积累了大量的数据&#xff0c;其中包含大量的客户信息等敏感数据&#xff0c;数据信息一旦泄露&#xff0c;不仅会给客户造成直接经济损失&#xff0c;也会给金融业的声誉带来负面影 响&#xff0c;甚至会导致金融机构承…

dreamweaver作业静态HTML网页设计 大学美食菜谱网页制作教程(web前端网页制作课作业)

&#x1f380; 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

Linux之权限【读、写、执行】【详细总结】

目录权限相关介绍rwx权限详解rwx作用到文件rwx作用到目录文件及目录权限实际案例权限修改第一种方式&#xff0c;&#xff0c;-&#xff0c;变更权限案例演示&#xff1a;第二种方式&#xff1a;通过数字变更权限chmod urwx,grx,ox 文件目录名 chmod 751 文件目录名修改文件所…

工时管理:警惕员工时间偷窃!企业应该如何避免?

员工时间偷窃是指员工捏造工时&#xff0c;对工时进行四舍五入&#xff0c;或故意延长休息时间&#xff0c;从事与工作无关的活动&#xff0c;却谎报了工作时间&#xff0c;接受了公司在这期间支付的劳动报酬。大家都知道的“带薪摸鱼”这个词&#xff0c;就是员工时间偷窃的一…

Redis学习

Redis1.NoSQL数据库概述NoSQL使用场景NoSQL不适用场景2.Redis2.1应用场景2.1.1 配合关系型数据库做高速缓存2.1.2 多样的数据结构存储持久化数据2.1.3 Redis内存管理2.1.3.1 删除策略2.1.4 Redis持久化机制2.1.4.1 什么是RDB持久化&#xff1f;2.1.4.2 RDB创建快照时会阻塞主线…

小啊呜产品读书笔记001:《邱岳的产品手记-12》第22讲 产品经理的图文基本功(上):产品文档 23讲产品经理的图文基本功(下):产品图例

小啊呜产品读书笔记001&#xff1a;《邱岳的产品手记-12》第22讲 产品经理的图文基本功&#xff08;上&#xff09;&#xff1a;产品文档 & 23讲产品经理的图文基本功&#xff08;下&#xff09;&#xff1a;产品图例一、今日阅读计划二、泛读&知识摘录1、第22讲 产品经…

WPF中使用MVVM模型进行数据绑定

文章目录前言一、声明一个类用来实现接口 INotifyPropertyChanged二、实例化ViewModel对象1.新建MainViewModel模型类2.实例化对象三、在界面设计代码中进行绑定四、应用前言 WPF数据绑定对于WPF应用程序来说尤为重要&#xff0c;本文将讲述使用MVVM模式进行数据绑定的四步走用…

如何实现一个优秀的 HashTable 散列表?

本文已收录到 AndroidFamily&#xff0c;技术和职场问题&#xff0c;请关注公众号 [彭旭锐] 提问。 前言 大家好&#xff0c;我是小彭。 在前几篇文章里&#xff0c;我们聊到了 Java 中的几种线性表结构&#xff0c;包括 ArrayList、LinkedList、ArrayDeque 等。今天&#xf…

ArcGIS绘制地球

下面这个图是非常不错的&#xff0c;截取自论文的一张图&#xff1a; 学了十几年地理学&#xff0c;最初的兴趣恐怕还是小时候常常摆弄的地球仪&#xff1b;现在终于有机会尝试地球仪风格制作了。 虽然迟到了十几年&#xff0c;不过今天还是有机会“复现”小时候的地球仪。 先…

使用docker-compose部署达梦DEM管理工具,mac m1系列适用

之前搭建了mac m1下基于docker的达梦库&#xff08;地址&#xff09;&#xff0c;但是没有一个好用的管理端。 用过DBeaver&#xff0c;可以使用自定jar创建dm链接&#xff0c;只做简单查询还行&#xff0c;要是用到一些修改、大文本查看、配置修改等高级点的功能就不行了。 …

Redis-使用java代码操作Redis

目录 Java连接Redis Java链接 测试是否连接 Java操作Redis Redis字符串(String) Redis哈希(Hash) Redis列表&#xff08;List&#xff09; Redis集合&#xff08;Set&#xff09; Java连接Redis 前置条件&#xff1a;Redis的服务要开启 pom依赖 <dependency>&l…

小熊U租港交所上市:市值28亿港元 京东联想腾讯是股东

雷递网 雷建平 11月24日小熊U租母公司凌雄科技集团有限公司&#xff08;简称&#xff1a;“凌雄科技”&#xff0c;股票代码为&#xff1a;“02436”&#xff09;今日在港交所上市。凌雄科技发行价为7.6港元&#xff0c;募资总额为3.37亿港元。凌雄科技开盘价为7.9港元&#xf…

C++17 --- 多态性、虚函数、多态的条件、联编(捆绑,绑定)

一、多态性 1、多态 多态性是面向对象程序设计的关键技术之一。 若程序设计语言不支持多态性&#xff0c;不能称为面向对象的语言。 多态性(polymorphism) 多态性是考虑在不同层次的类中&#xff0c;以及在同一类中&#xff0c;同名的成员函数之间的关系问题。 函数的重载&…