04、SpringAOP详解
创始人
2024-04-12 22:53:11
0

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、元素及其子元素

在这里插入图片描述

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

org.springframeworkspring-context5.0.8.RELEASEorg.springframeworkspring-test5.0.8.RELEASEtestjunitjunit4.12testorg.aspectjaspectjweaver1.8.13

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、书写配置




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项目导入如下依赖

org.springframeworkspring-context5.0.8.RELEASEorg.springframeworkspring-test5.0.8.RELEASEtestjunitjunit4.12testorg.aspectjaspectjweaver1.8.13

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、配置文件修改




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用于定义最终通知

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...