Spring声明式事务控制原理之声明式事务的重要组件在AOP中的应用

深入理解Spring(六) 事务是开发中必不可少的东西,使用JDBC开发时,我们使用connection对事务进行控制,使用MyBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,Spring就将这些技术基础上,提供了统一的控制事务的接口。Spring的事务分为:编程式事务和声明式事务控制。 阅读详情

重要组件有:
1.InfrastructureAdvisorAutoProxyCreator 后置处理器
2.BeanFactoryTransactionAttributeSourceAdvisor 事务通知器
3.TransactionAttributeSource 事务属性解析器
4.TransactionInterceptor 事务拦截器,作为通知advice

1. InfrastructureAdvisorAutoProxyCreator的作用

InfrastructureAdvisorAutoProxyCreator实现了SmartInstantiationAwareBeanPostProcessor是一个后置处理器,在bean创建过程的最后一步会调用InfrastructureAdvisorAutoProxyCreator的postProcessAfterInitialization方法对bean进行代理增强
AbstractAutowireCapableBeanFactory.class
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			// 遍历执行bean后置处理器的postProcessAfterInitialization方法
			Object current = processor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}
2. BeanFactoryTransactionAttributeSourceAdvisor的作用
这是一个Advisor(通知器),在AbstractAutoProxyCreator#wrapIfNecessary方法中获取当前bean相关的全部切面,用于代理类的生成
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		//
		if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
			return bean;
		}
		// 当前bean无切面,所以无需生成代理对象
		if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
			return bean;
		}
		//
		if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
			this.advisedBeans.put(cacheKey, Boolean.FALSE);
			return bean;
		}

		// Create proxy if we have advice.
		// 获取当前bean相关的全部切面
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);
			Object proxy = createProxy(
					bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}
		// 当前bean无切面
		this.advisedBeans.put(cacheKey, Boolean.FALSE);
		return bean;
	}
3.TransactionAttributeSource的作用
主要作用解析某方法中是否包含@Transactional,解析Transactional注解的各个属性封装成配置类备用;有两个重要地方用到第一个地方用来判断bean里是否包含@Transactional,以决定是否为此bean进行事务代理增强;第二个地方用来判断bean具体的方法上是否包含@Transactional,以决定是否对此方法进行事务拦截

第一个:在2中获取当前bean相关的全部切面的AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean#findEligibleAdvisors#findAdvisorsThatCanApply里用到

AbstractAdvisorAutoProxyCreator.class
protected Object[] getAdvicesAndAdvisorsForBean(
			Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {
		// 获取当前类相关的全部切面
		List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
		if (advisors.isEmpty()) {
			return DO_NOT_PROXY;
		}
		return advisors.toArray();
	}


protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
		// 获取容器里全部切面
		List<Advisor> candidateAdvisors = findCandidateAdvisors();
		// 过滤切入当前bean的切面
		List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
		extendAdvisors(eligibleAdvisors);
		if (!eligibleAdvisors.isEmpty()) {
			// 切面按指定顺序排序
			eligibleAdvisors = sortAdvisors(eligibleAdvisors);
		}
		return eligibleAdvisors;
	}

protected List<Advisor> findAdvisorsThatCanApply(
			List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {

		ProxyCreationContext.setCurrentProxiedBeanName(beanName);
		try {
			// 过滤于当前Bean无关的Advisor(通知器)
			return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
		}
		finally {
			ProxyCreationContext.setCurrentProxiedBeanName(null);
		}
	}
AopUtils.class
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
		if (candidateAdvisors.isEmpty()) {
			return candidateAdvisors;
		}
		List<Advisor> eligibleAdvisors = new ArrayList<>();
		for (Advisor candidate : candidateAdvisors) {
			if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
				eligibleAdvisors.add(candidate);
			}
		}
		boolean hasIntroductions = !eligibleAdvisors.isEmpty();
		for (Advisor candidate : candidateAdvisors) {
			if (candidate instanceof IntroductionAdvisor) {
				// already processed
				continue;
			}
			// 判断当前bean是否在这个切面的覆盖下
			if (canApply(candidate, clazz, hasIntroductions)) {
				eligibleAdvisors.add(candidate);
			}
		}
		return eligibleAdvisors;
	}

// 用到了advisor的切入点PointCut来判断这个bean里的那个方法符合切入点,也就是存在@Transactional注解,这里切入点判断某方法是否包含@Transactional注解时用到了TransactionAttributeSource
public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
		if (advisor instanceof IntroductionAdvisor) {
			return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
		}
		else if (advisor instanceof PointcutAdvisor) {
			PointcutAdvisor pca = (PointcutAdvisor) advisor;
			return canApply(pca.getPointcut(), targetClass, hasIntroductions);
		}
		else {
			// It doesn't have a pointcut so we assume it applies.
			return true;
		}
	}

第二个地方:以JDK动态代理举例在代理类调用方法时会进入JdkDynamicAopProxy的invoke方法,在此方法里会获取当前方法的拦截器链,然后组装创建一个方法执行器,用来管理各个拦截方法按顺序调用,如第一个地方一样此处用到了TransactionAttributeSource来判断此方法是否符合BeanFactoryTransactionAttributeSourceAdvisor的切入点即是否有@Transactional注解

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object oldProxy = null;
		boolean setProxyContext = false;

		TargetSource targetSource = this.advised.targetSource;
		Object target = null;

		try {
			if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
				// The target does not implement the equals(Object) method itself.
				return equals(args[0]);
			}
			else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
				// The target does not implement the hashCode() method itself.
				return hashCode();
			}
			else if (method.getDeclaringClass() == DecoratingProxy.class) {
				// There is only getDecoratedClass() declared -> dispatch to proxy config.
				return AopProxyUtils.ultimateTargetClass(this.advised);
			}
			else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
					method.getDeclaringClass().isAssignableFrom(Advised.class)) {
				// Service invocations on ProxyConfig with the proxy config...
				return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
			}

			Object retVal;

			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);

			// Get the interception chain for this method.
			// 获取方法的拦截器链
			List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

			// Check whether we have any advice. If we don't, we can fallback on direct
			// reflective invocation of the target, and avoid creating a MethodInvocation.
			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...
				// 根据方拦截器链创建方法执行器
				MethodInvocation invocation =
						new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
				// Proceed to the joinpoint through the interceptor chain.
				// 方法执行器启动
				retVal = invocation.proceed();
			}

			// Massage return value if necessary.
			Class<?> returnType = method.getReturnType();
			if (retVal != null && retVal == target &&
					returnType != Object.class && returnType.isInstance(proxy) &&
					!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
				// Special case: it returned "this" and the return type of the method
				// is type-compatible. Note that we can't help if the target sets
				// a reference to itself in another returned object.
				retVal = proxy;
			}
			else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
				throw new AopInvocationException(
						"Null return value from advice does not match primitive return type for: " + method);
			}
			return retVal;
		}
		finally {
			if (target != null && !targetSource.isStatic()) {
				// Must have come from TargetSource.
				targetSource.releaseTarget(target);
			}
			if (setProxyContext) {
				// Restore old proxy.
				AopContext.setCurrentProxy(oldProxy);
			}
		}
	}
4.TransactionInterceptor
这是一个方法拦截器也可以理解为是一个定义了事务处理的横切逻辑的通知advice,主要用在代理对象执行带有@Transactional的方法时调用invoke方法对原方法进行拦截增强,织入事务横切逻辑(作为3中第二个地方拦截器链的一员)
TransactionInterceptor.class
public Object invoke(MethodInvocation invocation) throws Throwable {
		// Work out the target class: may be {@code null}.
		// The TransactionAttributeSource should be passed the target class
		// as well as the method, which may be from an interface.
		Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

		// Adapt to TransactionAspectSupport's invokeWithinTransaction...
		// 对原方法进行拦截增强,织入事务横切逻辑
		return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
	}

进入invokeWithinTransaction方法

TransactionAspectSupport.class
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
			final InvocationCallback invocation) throws Throwable {

		// If the transaction attribute is null, the method is non-transactional.
		// 获取事务属性解析器ProxyTransactionManagementConfiguration中注入
		TransactionAttributeSource tas = getTransactionAttributeSource();
		final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
		// 获取事务管理器
		final PlatformTransactionManager tm = determineTransactionManager(txAttr);
		final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

		if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
			// Standard transaction demarcation with getTransaction and commit/rollback calls.
			TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);

			Object retVal;
			try {
				// This is an around advice: Invoke the next interceptor in the chain.
				// This will normally result in a target object being invoked.
				// 执行原方法
				retVal = invocation.proceedWithInvocation();
			}
			catch (Throwable ex) {
				// target invocation exception
				// 如果目标方法剖出异常执行回滚操作,并且抛出异常
				completeTransactionAfterThrowing(txInfo, ex);
				throw ex;
			}
			finally {
				cleanupTransactionInfo(txInfo);
			}
			// 如果目标方法正常执行执行事务提交操作。
			commitTransactionAfterReturning(txInfo);
			return retVal;
		}

		else {
			final ThrowableHolder throwableHolder = new ThrowableHolder();

			// It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
			try {
				Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> {
					TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
					try {
						return invocation.proceedWithInvocation();
					}
					catch (Throwable ex) {
						if (txAttr.rollbackOn(ex)) {
							// A RuntimeException: will lead to a rollback.
							if (ex instanceof RuntimeException) {
								throw (RuntimeException) ex;
							}
							else {
								throw new ThrowableHolderException(ex);
							}
						}
						else {
							// A normal return value: will lead to a commit.
							throwableHolder.throwable = ex;
							return null;
						}
					}
					finally {
						cleanupTransactionInfo(txInfo);
					}
				});

				// Check result state: It might indicate a Throwable to rethrow.
				if (throwableHolder.throwable != null) {
					throw throwableHolder.throwable;
				}
				return result;
			}
			catch (ThrowableHolderException ex) {
				throw ex.getCause();
			}
			catch (TransactionSystemException ex2) {
				if (throwableHolder.throwable != null) {
					logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
					ex2.initApplicationException(throwableHolder.throwable);
				}
				throw ex2;
			}
			catch (Throwable ex2) {
				if (throwableHolder.throwable != null) {
					logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
				}
				throw ex2;
			}
		}
	}

Spring的事务控制-基于XML的声明式事务控制(上) 他是不是我们的serive,因为Spring你不指定,你根本不知道Dao层有哪些技术,我用那个平台的管理器。 阅读详情

相关推荐

我的架构梦:(七)Spring框架中的AOP源码分析

Spring框架中的AOP源码分析一、代理对象创建二、Spring声明式事务控制 一、代理对象创建 1、AOP基础用例准备 Bean定义 @Component public class RiemannBean { public void print(){ System.out.println("RiemannBean print..."); } } Aspect定义 @Component @Aspect public class RiemannAspect { @Pointcut("execu

riemann_的博客 888

Spring源码解析之-- 事务InfrastructureAdvisorAutoProxyCreator 分析

目录一、介绍二、InfrastructureAdvisorAutoProxyCreator 分析2.1 结构2.1 解析2.1.1 postProcessBeforeInstantiation2.1.2 postProcessAfterInitialization2.1.3 wrapIfNecessary2.1.4 getAdvicesAndAdvisorsForBean2.1.4 findEligibleAdvisors2.1.4 findAdvisorBeans2.1.5 AopUtils#findAd

一直打铁 2372

Spring框架中声明式事务的AOP实现

在软件工程中,关注点的横切是面向对象编程的一个常见问题,这正是面向切面编程(AOP)的用武之地。Spring框架通过AOP能够为我们的应用提供声明式服务,如事务管理和安全控制。在计算机科学中,事务是由一系列操作组成的逻辑工作单元,这些操作要么全部成功,要么全部失败。事务是数据库管理系统(DBMS)的关键特性之一,确保了数据的一致性和完整性。事务通常具有以下四个基本特性,这四个特性也被称为ACID属性:原子性(Atomicity)

weixin_33816734的博客 633

Spring注解驱动开发第35讲——声明式事务原理的源码分析

写在前面 上一讲,我向大家简单介绍了一下注解版的声明式事务,相信大家已经会初步使用了。这一讲,咱们就从源码的角度分析一下声明式事务的原理,让大家对声明式事务的原理有一个更加深入的认识。 声明式事务的原理 其实,要想知道声明式事务的原理,只需要搞清楚@EnableTransactionManagement注解给容器中注册了什么组件,以及这些组件工作时候的功能是什么就行了,一旦把这个研究透了,那么声明式事务的原理我们就清楚了。 你有没有发现,咱们之前研究AOP的原理时,是从@EnableAspectJAutoP

李阿昀的博客 3861

数据标注的下一代SOP,可能不是Word,而是一段Prompt

再给几个反例:“这个只是正常纹理,不算。真正用于数据生产的Prompt,可能至少包含:任务定义、正例、反例、边界案例、优先级、输出格式、验收逻辑。一个项目100个人,如果100个人对这句话有100种理解,最后返工的就不是一个框,而可能是几十万帧数据。一个项目启动以后,项目经理先研究SOP,培训组长,组长再培训标注员,最后几百个人按照同一套规则干活。实际上一个成熟的项目经理,最重要的能力之一,就是:把客户的业务语言翻译成生产规则。真正让项目难交付的,从来不是80%的普通数据,而是那20%的长尾数据。

AI数据标注猿 300

雷光联动自动化标校方法

动态标校需要利用动目标,在雷达和光电同时看到目标时记录光电的方位、俯仰,以及利用雷达给的目标信息计算的方位俯仰,手动记录两者的数值,多方位,多距离段记录,费时费力,且人工记录数据不全,计算容易出差错,耗费成本和精力太大。目标相距设备的水平距离 = acos(cos(设备纬度弧度值) * cos(目标纬度弧度值) * cos(设备经度弧度值 – 目标经度弧度值) + sin(设备纬度弧度值) * sin(目标纬度弧度值)) * 6378137;本发明涉及光电技术领域,安防领域,尤其涉及雷达光电动态标校等。

u013358093的博客 1583

Spring 事务第四篇 - Spring 事务遇到AOP会怎么样?

前言 前面的文章探索了怎么拦截事务方法,怎么对其进行增强。但是我们讲解 AOP 的时候也聊过,如果想对一个对象进行增强,首先要生成一个代理对象,然后找到增强对象的拦截器链,这样才能逐个执行拦截方法进行增强。此外本文还会解答下上文提到的一个问题,就是当 AOP 和事务同时开启的话会怎么办呢? InfrastructureAdvisorAutoProxyCreator 我们前文也已经分析过,通过@EnableTransactionManagement 会引入该后置处理器,...

paralysed的博客 2111

金智维KRPA实战:Excel自动化数据处理全流程解析

本文详细解析了金智维KRPA在Excel自动化数据处理中的全流程应用,涵盖COM接口和文件读写两种核心技术方案,以及销售报表生成、数据清洗和可视化仪表盘等实战案例。通过企业级应用的异常处理、性能优化和安全合规实践,显著提升数据处理效率和准确性,助力企业降本增效。

weixin_30329623的博客 599

吃透Spring源码(二十一):事物初始化流程之注解配置方式

一,例子准备 BookDao.java public class BookDao { @Autowired JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplat

吃透Java 506

6 赫斯曼网管软件industrial hivision用户管理

文章目录前言一、打开Industrial HiVision服务器二、在用户管理界面添加策略三、使用可选策略四、接受策略总结 前言 安装 Industrial HiVision 后,所有人都能广泛使用程序。安全起见,请进行相 关配置,对访问权限进行限制。 本文主要介绍用户管理基于某种角色模型。在访问角色中定义角色名称,通过分配访问权限给这些角色分配角色功能。例如,给管理人员自然要分配可用的权限,而观察员尽管可以登录,但没有能够进行更改的权限。Industrial HiVision 提供以下访问角色: 

t98746194的博客 1384

SVN使用:使用TortoiseSVN遇到提示请执行“清理”命令的解决方法

作为程序员来说,在团队开发过程中关于代码管理的工具使用并不陌生,比较常用的代码合并管理工具如:Git、SVN等,虽然相关工具的使用并不复杂,但是还是要掌握常用的使用方法,尤其是相关的第三方软件的使用也是极为重要的,而且有时候使用这些三方软件来进行代码管理的时候也会遇到一些问题。本篇博文就来分享一下在SVN使用的时候使用TortoiseSVN遇到提示请执行“清理”命令的解决方法,方便查阅使用。

全沾软贱开发攻城狮 1万+

Spring源码分析笔记--事务管理

核心类 InfrastructureAdvisorAutoProxyCreator 本质是一个后置处理器,和AOP的后置处理器类似,但比AOP的使用级别低。当开启AOP代理模式后,优先使用AOP的后置处理器。 AopConfigUtils: /** * The bean name of the internally managed auto-proxy creator. */ ...

weixin_30493321的博客 171

零基础教你学RDMA驱动开发--第8篇:队列对(QP)、完成队列(CQ)与工作请求(WR)机制剖析

本文深入剖析了RDMA(远程直接内存访问)中的核心机制:队列对(QP)、完成队列(CQ)和工作请求(WR)。QP作为通信端点包含发送队列(SQ)和接收队列(RQ);WR是用户提交的操作指令,转换为工作队列元素(WQE)后由网卡异步执行;CQ则提供完成通知机制。文章详细介绍了三者的协作原理,包括QP状态机、WR提交方式、CQ轮询机制,以及关键的信号控制技术(Signaled Mode)来优化性能。同时对比了SEND/RECV和RDMA WRITE两种模式的差异,提供了调试技巧和最佳实践。这些机制共同构成了RD

amy_mhd的博客 164

Spring事务实现的方式及底层原理

spring事务的实现方式 spring框架提供了两种事务实现方式:编程式事务、声明式事务 编程式事务:在代码中进行事务控制。优点:精度高。缺点:代码耦合度高 声明式事务:通过@Transactional注解实现事务控制 spring事务的底层原理 事务的操作本来应该由数据库进行控制,但是为了方便用户进行业务逻辑的控制,spring对事务功能进行了扩展实现。一般我们很少使用编程式事务,更多的是使用@Transactional注解实现。当使用了@Transactional注解后事务的自动功能就会关闭,由sp.

qq_34886456的博客 1万+

学Simulink——基于故障诊断与容错控制的场景实例:并网逆变器在短路故障下的动态响应及恢复策略

本文详细介绍了使用Simulink进行并网逆变器短路故障模拟与保护策略设计的方法。通过构建高精度逆变器模型,模拟IGBT击穿和桥臂直通故障,分析动态响应特性。重点设计了基于Stateflow的微秒级快速保护逻辑,包括过流检测、PWM闭锁和故障报警。同时提出完整的恢复策略流程,涵盖放电、去磁、自检和软启动等关键步骤。仿真结果表明,该方法能在200μs内有效限制短路电流,保护器件安全,并实现系统可靠恢复。文章还探讨了硬件保护协同、多级保护和预测保护等高级策略,为新能源系统的安全运行提供了重要参考。

amy_mhd的博客 148

【SemiDrive源码分析】【X9芯片启动流程】11 - freertos_safetyos目录Cortex-R5 DIL2.bin 引导程序源代码分析

【SemiDrive源码分析】【X9芯片启动流程】11 - freertos_safetyos目录Cortex-R5 DIL2.bin 引导程序源代码分析

|~~~热爱生活、努力学习的小伙汁~~~| 2115

Spring事务控制详解:从概念到声明式事务(AOP实现)

本文系统介绍了Spring框架中声明式事务管理的核心原理与应用。以转账案例说明事务控制对数据一致性的重要性,阐释了PlatformTransactionManager、TransactionDefinition和TransactionStatus三大核心API的角色。重点讲解了如何通过AOP(面向切面编程)配置声明式事务,包括配置事务管理器、定义事务增强(通知)与切点,从而将事务逻辑与业务代码彻底解耦,实现“零侵入”的事务管理。最终,Spring容器通过动态代理自动为业务方法提供事务的开启、提交与回滚,是J

2503_94734310的博客 657

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

【Spring系列-08】声明式事务实现原理

Spring 声明式事务实现原理深度剖析 摘要:本文深入分析了Spring声明式事务的核心实现原理,重点探讨了事务抽象设计思想和传播行为机制。Spring通过PlatformTransactionManager接口实现了事务管理的抽象,支持多种事务管理器实现(如JDBC、JPA、JTA等),并通过TransactionDefinition定义事务属性。文章详细解析了7种事务传播行为在源码中的实现逻辑,包括PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW等,并展示了转

专注后端技术架构,AI技术分享 2126

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Spring基于AOP事务控制实现原理

对于一个系统应用而言,使用数据库进行数据存储是必然的,意味着开发过程中事务的使用及控制也是必不可少的,当然事务是数据库层面的知识点并不是Spring框架所提出的。使用JDBC开发时,我们使用对事务进行控制,使用MyBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,所以Spring就在这些技术基础上,提供了统一的控制事务的接口。Spring的事务分为:编程式事务控制和声明式事务控制。编程式事务控制。

weixin_41821642的博客 446

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Spring声明式事务控制

1)事务开始时,通过AOP机制,生成一个代理的connection对象,并将其放入DataSource实例的某个与DataSourceTransactionManager相关的容器中。在接下来的整个事务中,客户端与数据库的交互都使用该connection。 2)事务结束时,回滚在第一步中得到的代理的connection中的数据库命令,然后关闭该代理connection对象。 具体分析: 声明式事务很⽅便,尤其纯注解模式,仅仅⼏个注解就能控制事务了? @Enable...

qq_38658642的博客 1336

Spring的xml方式声明式事务控制

而开启事务和提交事务又是在invoke方法执行的,所以而这个invoke方法又是属于TransactionInterceptor(实现了 MethodInterceptor),所以我们在配置的时候就通过Advisor来配置TransactionInterceptor,实际上声明式事务本质上就是AOP。在这里Dao层使用的框架是mybatis,mybatis底层是jdbc,jdbc需要connection,所以要数据源,配置的是DataSourceTransactionManager,且要指定数据源。

2301_80749359的博客 597

Spring AOP与事务管理精讲:提升你的开发技能

在当今的软件开发领域,Spring框架无疑是Java后端开发的基石。AOP(面向切面编程)和事务管理是Spring框架中两个极其重要的特性,它们共同为构建高性能、高可靠性的应用提供了强大的支持。然而,许多开发者对这两个特性的深入理解和有效应用还存在一定的差距。本文将带你深入探索Spring AOP与事务管理的完美结合,从理论基础到实际应用,从最佳实践到性能优化,全方位解读如何利用这两个特性打造高效稳定的应用。让我们一起开启这段技术之旅,提升你的开发技能,构建出更加出色的后端应用。

qq_51749909的博客 998

Spring的事务管理 - 声明式事务

基于Spring AOP实现。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。编程式事务管理使用可实现更细粒度的事务控制。声明式事务管理不需要入侵代码,通过就可以进行事务操作,更快捷而且简单且大部分业务都可以满足,推荐使用。其实不管是编程式事务还是声明式事务,最终调用的底层核心代码是一致的。

埃泽漫笔 1562

Spring学习笔记7,基于AOP使用声明式事务控制

什么是AOP AOP是Spring提供的关键特性之一。AOP即面向切面编程,是OOP编程的有效补充。使用AOP技术,可以将一些系统性相关的编程工作,独立提取出来,独立实现,然后通过切面切入进系统。从而避免了在业务逻辑的代码中混入很多的系统相关的逻辑——比如权限管理,事物管理,日志记录等等。这些系统性的编程工作都可以独立编码实现,然后通过AOP技术切入进系统即可。从而达到了 将不同的关注点分离出来...

小辣鸡的博客 544

Spring AOP源码剖析:Spring声明式事务控制

Spring AOP源码剖析:Spring声明式事务控制 Spring声明式事务控制 声明式事务很方便,尤其纯注解模式,仅仅几个注解就能控制事务了 那这些注解都做了什么呢?好神奇!好厉害! @EnableTransactionManagement @Transactional 1、 @EnableTransactionManagement @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Impor

丿涛哥哥的博客 248

科普文:spring声明式事务@Transactional原理与实现

在使用事务的时候需要添加@EnableTransactionManagement注解来开启事务,Spring事务底层是通过AOP来实现的,所以启用事务后,同样会向容器中注入一个代理对象创建器,AOP使用的是AnnotationAwareAspectJAutoProxyCreator,事务使用的是InfrastructureAdvisorAutoProxyCreator。

为无为,事无事,味无味。 1875
上一篇: Spring声明式事务控制原理之声明式事务的重要组件(注解实现)以及之间关系
下一篇: Servlet、过滤器(Filter)、监听器(Listener)、拦截器(Interceptor)的作用和区别
躺平程序猿
博客等级 码龄8年 4870粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺平程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址