SpringAOP是什么?为什么要有SpringAOP?

news/2025/2/12 3:31:38/

SpringAOP是什么?为什么要有SpringAOP?

原文:SpringAOP是什么?为什么要有SpringAOP?

一、有SpringAOP之前

简单的开发场景,只需要写简单的业务逻辑,比如CRUD

但是在执行具体的逻辑之前,需要进行权限校验,或者在具体的逻辑之后,需要日志记录呢?

这样就增加了很多代码,而且增加的这些代码都是差不多的

既然如此,那我们抽出来吧!在这个类里面写一个私有的方法。

代码少了很多,但是如果其他的类也需要用到权限校验和日志记录呢?

难道也要在其他类里面写私有方法吗?这也太麻烦了。

为了解决这个问题,有两种方法。

第一种:将这个私有方法抽出来不就好了,我直接写两个工具类,一个是权限校验,一个是日志记录,谁用谁调用。

第二种:我直接搞一个父类,谁用我就让它继承这个父类,这样就能直接调用父类的方法。

但是不论是第一种还是第二种其实都会侵入业务类的方法逻辑,那么有没有一种方法,能在没有对业务核心代码侵入性的前提下,给业务核心代码添加额外的功能呢?

这时候AOP就出来了,也就是所谓的面向切面编程。

首先来看一下AOP的概念:

切入点:

想额外添加功能的方法

切面:

权限检查、日志记录等这些增强逻辑的方法在的类

通知:

在目标方法运行的什么时机来执行某个增强逻辑

目标方法前还是目标方法后

切入点表达式:

规定增强逻辑需要去增强什么方法

二、有SpringAOP之后

使用方法:

maven依赖

<dependencies><!--导入Spring Context核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.23</version></dependency><!--Spring AOP--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.1</version></dependency><!--Lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency><!--Junit单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>

切入点

package com.moon.aop.bean;/*** @Author moon* @Date 2023/10/15 15:55* @Description 切入点*/
public class UserService {public void addUser() {System.out.println("添加用户...");}public void queryUser() {System.out.println("查询用户...");}public void editUser() {System.out.println("编辑用户...");}public void deleteUser() {System.out.println("删除用户...");}
}

切面

package com.moon.aop.bean;/*
* 底层依赖Aspect的注解【但是原理不同】
* */
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;/*** @Author moon* @Date 2023/10/15 15:58* @Description 切面,里面放的增强逻辑*/@Aspect
public class UserAspect {/** 这就是传说中上文所谓的切入点表达式* */@Pointcut(value = "execution(* com.moon.aop.bean.UserService.*(..))")public void point() {}/** 这就是增强逻辑* @Before、@After就是所谓的通知* */@Before(value = "point()")public void beforeRun() {System.out.println("在目标方法执行前开始执行");}@After(value = "point()")public void afterRun() {System.out.println("在目标方法执行后开始执行");}
}

配置类:用来初始化和加载Spring的IOC容器

package com.moon.aop.bean;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** @Author moon* @Date 2023/10/15 16:07* @Description 用该类来初始化和加载IOC容器*/@Configuration
public class SpringConfig {@Beanpublic UserAspect userAspect() {return new UserAspect();}@Beanpublic UserService userService() {return new UserService();}
}

Junit单元测试

import com.moon.aop.bean.SpringConfig;
import com.moon.aop.bean.UserService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** @Author moon* @Date 2023/10/15 16:32* @Description*/
public class AOPTest {@Testpublic void test01() {/** 通过配置类初始化IOC容器* */AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);/** 获取目标对象,Bean* */UserService userService = context.getBean(UserService.class);userService.addUser();}
}

在这里插入图片描述

AOP并没有生效

此时userService.getClass().getName()的值为

在这里插入图片描述

那是因为Spring并没有开启AOP,需要我们手动开启

package com.moon.aop.bean;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** @Author moon* @Date 2023/10/15 16:07* @Description 用该类来初始化和加载IOC容器*/@Configuration
/*
* 手动开启AOP功能
* */
@EnableAspectJAutoProxy
public class SpringConfig {@Beanpublic UserAspect userAspect() {return new UserAspect();}@Beanpublic UserService userService() {return new UserService();}
}

在这里插入图片描述

成功!

此时userService.getClass().getName()的值为

在这里插入图片描述


http://www.ppmy.cn/news/1155942.html

相关文章

4.0 Beta2版本编译RK3588错误问题解决

最近有小伙伴在问4.0 Beta2版本编译RK3588&#xff08;也就是dayu210&#xff09;时&#xff0c;会有各种莫名奇妙的报错 &#xff08;1&#xff09;subsystem name config incorrect in ....... 这个原因是OH代码加入了编译检查&#xff0c;临时措施是把需要编译检查的文件放…

设计模式的学习顺序

设计模式的学习顺序可以按照以下步骤进行&#xff1a; 掌握基础知识&#xff1a;先确保你对编程语言和软件开发的基本概念有深入的理解&#xff0c;包括面向对象编程、继承、多态等。学习常用设计模式&#xff1a;首先学习并理解一些常用的设计模式&#xff0c;例如单例模式、…

flink教程

文章目录 来自于尚硅谷教程1. Flink概述1.1 特点1.2 与SparkStreaming对比 2. Flink部署2.1 集群角色2.2 部署模式2.3 Standalone运行模式2.3.1 本地会话模式部署2.3.2 应用模式 2.4 YARN运行模式2.4.1 会话模式部署2.4.2 应用模式部署 2.5 历史服务 3. 系统架构3.1 并行度3.2 …

JAVA总结01

1.变量在定义的时候可以不赋初始值&#xff0c;但在使用的时候变量必须有值 2.数据类型 数据类型字节数整型byte1short2int4long8浮点型float4double8字符型char2布尔型boolean1 浮点数默认都是double类型 上面代码报错的原因就是5.0是double类型&#xff0c;不是float类型 当…

ubuntu mmdetection配置

mmdetection配置最重要的是版本匹配&#xff0c;特别是cuda&#xff0c;torch与mmcv-full 本项目以mmdetection v2.28.2为例介绍 1.查看显卡算力 因为gpu的算力需要与Pytorch依赖的CUDA算力匹配&#xff0c;低版本GPU可在相对高的CUDA版本下运行&#xff0c;相反则不行 算力…

xtrabackup全备 增备

版本针对mysql8.0版本 官方下载地址 https://www.percona.com/downloads 自行选择下载方式 yum安装方式 1、下载上传服务器 安装软件 [rootmaster mysql]# ll percona-xtrabackup-80-8.0.33-28.1.el7.x86_64.rpm -rw-r--r--. 1 root root 44541856 Oct 10 13:25 percona-x…

Shell命令笔记2

大家好&#xff0c;分享下最近工作中用得比较多的shell命令&#xff0c;希望对大家有帮助。 获取数组长度&#xff1a; ${#array_name[*]}获取脚本相对路径 script_path$(dirname "$0")获取脚本的名字 script_name$(basename "$0")获取脚本的绝对路径 …

【LeetCode】双指针 滑动窗口

文章目录 一、双指针简介双指针模式使用双指针来解决的问题题目移动0盛最多水的容器接雨水 二、滑动窗口简介题目最小子串找到字符串中所有字母异位词 一、双指针简介 双指针是指在算法中同时使用两个指针来追踪数组或序列中的元素位置。这两个指针可以朝着相同方向移动&#…