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

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...