java框架-Spring-AOP

news2024/10/5 16:28:19

AOP:动态代理

开发步骤:

在这里插入图片描述

  1. 导入aop模块
  2. 定义业务逻辑类
  3. 定义切面类;
    -. 切面类标注:@Aspect
    -. 切面类注解:
    @Before: 前置通知, 在方法执行之前执行
    @After: 后置通知, 在方法执行之后执行 。
    @AfterRunning: 返回通知, 在方法返回结果之后执行
    @AfterThrowing: 异常通知, 在方法抛出异常之后
    @Around: 环绕通知, 围绕着方法执行
  4. 将切面类添加到容器
  5. 开启基于注解版的aop模式:@EnableAspectJAutoProxy

AOP原理

创建与注册

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

		@Override
		@Nullable
		public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
			Object oldProxy = null;
			boolean setProxyContext = false;
			Object target = null;
			TargetSource targetSource = this.advised.getTargetSource();
			try {
				if (this.advised.exposeProxy) {
					// Make invocation available if necessary.
					oldProxy = AopContext.setCurrentProxy(proxy);
					setProxyContext = true;
				}
				// Get as late as possible to minimize the time we "own" the target, in case it comes from a pool...
				target = targetSource.getTarget();
				Class<?> targetClass = (target != null ? target.getClass() : null);
				List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
				Object retVal;
				// Check whether we only have one InvokerInterceptor: that is,
				// no real advice, but just reflective invocation of the target.
				if (chain.isEmpty()) {
					// We can skip creating a MethodInvocation: just invoke the target directly.
					// Note that the final invoker must be an InvokerInterceptor, so we know
					// it does nothing but a reflective operation on the target, and no hot
					// swapping or fancy proxying.
					Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
					retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
				}
				else {
					// We need to create a method invocation...
					retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
				}
				return processReturnType(proxy, target, method, retVal);
			}
			finally {
				if (target != null && !targetSource.isStatic()) {
					targetSource.releaseTarget(target);
				}
				if (setProxyContext) {
					// Restore old proxy.
					AopContext.setCurrentProxy(oldProxy);
				}
			}
		}
	public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
		MethodCacheKey cacheKey = new MethodCacheKey(method);
		List<Object> cached = this.methodCache.get(cacheKey);
		if (cached == null) {
			cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
					this, method, targetClass);
			this.methodCache.put(cacheKey, cached);
		}
		return cached;
	}
	@Override
	public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
			Advised config, Method method, @Nullable Class<?> targetClass) {

		// This is somewhat tricky... We have to process introductions first,
		// but we need to preserve order in the ultimate list.
		AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
		Advisor[] advisors = config.getAdvisors();
		List<Object> interceptorList = new ArrayList<>(advisors.length);
		Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
		Boolean hasIntroductions = null;

		for (Advisor advisor : advisors) {
			if (advisor instanceof PointcutAdvisor pointcutAdvisor) {
				// Add it conditionally.
				if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
					MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
					boolean match;
					if (mm instanceof IntroductionAwareMethodMatcher iamm) {
						if (hasIntroductions == null) {
							hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
						}
						match = iamm.matches(method, actualClass, hasIntroductions);
					}
					else {
						match = mm.matches(method, actualClass);
					}
					if (match) {
						MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
						if (mm.isRuntime()) {
							// Creating a new object instance in the getInterceptors() method
							// isn't a problem as we normally cache created chains.
							for (MethodInterceptor interceptor : interceptors) {
								interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
							}
						}
						else {
							interceptorList.addAll(Arrays.asList(interceptors));
						}
					}
				}
			}
			else if (advisor instanceof IntroductionAdvisor ia) {
				if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
					Interceptor[] interceptors = registry.getInterceptors(advisor);
					interceptorList.addAll(Arrays.asList(interceptors));
				}
			}
			else {
				Interceptor[] interceptors = registry.getInterceptors(advisor);
				interceptorList.addAll(Arrays.asList(interceptors));
			}
		}

		return interceptorList;
	}

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

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

相关文章

[杂谈]-快速了解半波和全波整流

快速了解半波和全波整流 文章目录 快速了解半波和全波整流1、滤波2、半波整流器3、全波整流器4、常见问题 整流器是一种将交流信号转换为脉动直流信号以及将交流电转换为直流电的电子电路。 我们日常生活中几乎所有的电子项目都会用到它。 根据周期传导&#xff0c;本文我们介绍…

【Python】ModuleNotFoundError: No module named ‘Crypto‘

今天在使用一个新的库Crypto时发生了报错 Crypto安装成功~ 导入Crypto模块也没有问题 运行时却发生了报错&#xff1a; 没有这个模块&#xff1f; 我明明安装成功了&#xff0c;为什么报错没有这个库呢&#xff1f; 于是我去查看了一下是不是没有安装上呢&#xff1f; 为什么…

sql注入挖掘

出现的条件 只要是和数据库有交互 没有过滤拼接的sql语句可以执行 判断 这个是在url筐里的

想要精通算法和SQL的成长之路 - 双指针【数组】

想要精通算法和SQL的成长之路 - 双指针【数组】 前言一. 合并两个有序数组二. 删除有序数组中的重复项 II 前言 想要精通算法和SQL的成长之路 - 系列导航 一. 合并两个有序数组 原题链接 抓住重点信息&#xff1a; 两个数组都是非递减顺序排列。num1数组&#xff0c;末尾包…

在Bat To Exe Converter,修改为当异常结束或终止时,程序重新启动执行

在Bat To Exe Converter&#xff0c;修改为当异常结束或终止时&#xff0c;程序重新启动执行 .bat中的代码部分&#xff1a; .bat中的代码echo offpython E:\python\yoloProjectTestSmallLarge\detect.pypause&#xff0c;我想你能帮在Bat To Exe Converter&#xff0c;修改成…

JAVA入坑之嵌套类

一、嵌套类入门 1.1概述 Java嵌套类是指在一个类中定义另一个类的一种方式&#xff0c;它可以提高代码的可读性、可维护性和封装性。Java嵌套类分为两种类型&#xff1a;静态嵌套类和非静态嵌套类。 静态嵌套类&#xff1a;Static nested classes,即类前面有static修饰符 非静…

【进阶C语言】字符串与内存库函数认识与模拟实现

本章内容大致目录&#xff1a; 1.strlen函数 2.strcpy函数 3.strcmp函数 4.strcat函数 5.strstr函数 6.strtok函数 7.strerror与perror函数 8.字符操作函数 9.内存操作函数 10.总结 以上函数均属于库函数&#xff0c;有的函数则会介绍如何模拟实现。 一、strlen函数…

conda创建虚拟环境安装aix360

目录 创建虚拟环境查看已有虚拟环境进入所创建的虚拟环境查看已安装的程序查看已安装的python模块配置镜像pipconda 安装aix360将环境添加到jupyter删除虚拟环境 创建虚拟环境 建议装python3.7python3.7python3.7python3.7python3.7python3.7python3.7python3.7python3.7pytho…

YTM32的LIN通信协议引擎LinFlexD外设模块详解

YTM32的LIN通信协议引擎LinFlexD外设模块详解 文章目录 YTM32的LIN通信协议引擎LinFlexD外设模块详解LINFlexD外设简介LINFlexD工作机制初始化主机模式从机模式错误状态标志位超时错误&#xff08;Timeout Error&#xff09;ID过滤机制接收器检测帧间隔段和帧间隔段分隔符产生波…

[游戏开发][Shader]GLSLSandBox转Unity-CG语言

官网 GLSL Sandbox Galleryhttps://glslsandbox.com/ 屏幕坐标计算 fragCoord与_ScreenParams mat2矩阵转换 vec2向量 在GLSL里mat2(a, b, c, d)函数返回vec2但是在CG语言里 没有mat2函数&#xff0c;用下面的值替换mat2方法vec2(a * 1. c * 1., b * 1. d * 1.);举例&…

一台电脑远程内网的另外一台电脑,禁止远程的电脑连接外网,只允许内网连接

一台电脑远程内网的另外一台电脑&#xff0c;禁止远程的电脑连接外网&#xff0c;只允许内网连接 1.找到右下角网卡图标&#xff0c;右键图标选择“打开网络和共享中心”。 3、点击“更改适配器设置”。 4、右键正在使用的网卡“本地连接”打开属性 5、找到“internet协…

vue简单案例----小张记事本

小张记事本 具体效果如图所示&#xff0c;这里就简单展示&#xff0c;还有很多不足的地方&#xff0c;希望大家可以对这个小项目进行改进&#xff0c;话不多说可以参考下面的代码 源代码如下 <html lang"en"><head><meta charset"UTF-8"…

1786_MTALAB代码生成把通用函数生成独立文件

全部学习汇总&#xff1a; GitHub - GreyZhang/g_matlab: MATLAB once used to be my daily tool. After many years when I go back and read my old learning notes I felt maybe I still need it in the future. So, start this repo to keep some of my old learning notes…

1400*C. No Prime Differences(找规律数学)

解析&#xff1a; 由于 1 不是质数&#xff0c;所以我们令每一行的数都相差 1 对于行间&#xff0c;分为 n、m之中有存在偶数和都为奇数两种情况。 如果n、m存在偶数&#xff0c;假设m为偶数。 如果都为奇数&#xff0c;则&#xff1a; #include<bits/stdc.h> using name…

解决0-1背包问题(方案二):一维dp数组(滚动数组)

往期文章&#xff1a;解决0-1背包问题&#xff08;方案一&#xff09;:二维dp数组_呵呵哒(&#xffe3;▽&#xffe3;)"的博客-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/133207350?spm1001.2014.3001.5501 >>探索一维dp数组和二维dp数组的…

基于Spring Boot的医院预约挂号系统设计与实现(源码+lw+部署文档+讲解等)

前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f447;&#x1f3fb;…

想搭建数字藏品平台怎么做?

搭建数字藏品平台涉及多个步骤和技术方面的考虑。以下是创建数字藏品平台的一般步骤&#xff1a; 技术架构 选择合适的技术架构和平台。您可以选择使用现有的电子商务平台、自定义开发、或使用数字藏品管理系统等。云托管服务如Amazon Web Services 或Microsoft Azure也可以提…

fpga内嵌逻辑分析仪使用方法

文章目录 前言一、方法1 — 使用 IP 核创建 ILA 调试环境1、创建 ILA ip 核2、进行例化3、生成比特流文件4、下载程序5、进行在线调试 二、方法2 — 使用 Debug 标记创建 ILA1、Debug 标记相关信号2、综合操作3、设置 Set Up Debug4、生成比特文件5、下载程序6、进行在线调试 三…

Java数据结构与算法从初阶到大牛

一、我们从一个小例题看起 因为该二维数组的很多默认值为0&#xff0c;因此记录了很多没有意义的数据-->稀疏数组 二、基本介绍 当一个数组中大部分元素为0&#xff0c;或者为同一个值时&#xff0c;可以使用洗漱数组来保存数组 稀疏数组的处理方法是&#xff1a; 1&…

私域流量的变现方式,你知道多少?

私域流量的变现方式是指通过有效的管理和运营自有的用户群体&#xff0c;将流量转化为实际收益的过程。私域流量的变现方式多样&#xff0c;下面将介绍其中几种常见的方式。 1. 电商平台入驻 通过将自有流量引导到电商平台&#xff0c;开设店铺进行商品销售&#xff0c;从中获…