Spring Transaction事务实现流程源码解析

目录
  • 一、基于xml形式开启Transaction
    • 1. 创建数据库user
    • 2. 创建一个maven 项目
    • 3. 通过xml形式配置事务
      • 1) 创建Spring命名空间
      • 2) 开启事务配置
      • 3) 创建UserService类
    • 4. 测试事务
      • 1) 抛出RuntimeException
      • 2) 注释掉RuntimeException
  • 二、事务开启入口TxNamespaceHandler
    • AnnotationDrivenBeanDefinitionParser
  • 三、AOP驱动事务
    • TransactionInterceptor
    • 创建事务
    • 回滚事务

一、基于xml形式开启Transaction

1. 创建数据库user

/*
 Navicat Premium Data Transfer
 Source Server         : win-local
 Source Server Type    : MySQL
 Source Server Version : 50737
 Source Host           : localhost:3306
 Source Schema         : db0
 Target Server Type    : MySQL
 Target Server Version : 50737
 File Encoding         : 65001
 Date: 24/04/2022 20:27:41
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

2. 创建一个maven 项目

不用Springboot依赖,引入mysql驱动依赖、spring-beans、spring-jdbc、Spring-context依赖

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.18</version>
        </dependency>
        <!-- spring jdbc 依赖spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.18</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>

3. 通过xml形式配置事务

1) 创建Spring命名空间

首先在resources目录下创建一个spring.xml文件,Spring框架为了声明自己的Xml规范,在<beans>标签里定义了spring 框架指定模块的协议配置, 我们可以通过Index of /schema访问spring框架的所有模块包,各模块包含了不同版本的xsd文件。

点击进入context目录,查看xsd文件:

比如我要通过xml的形式配置一个bean, 需要在beans标签中声明 xmln的值为:

http://www.springframework.org/schema/beans

如果我想用spring的context模块,那么需要声明

xmlns:context="http://www.springframework.org/schema/context"

同时在xsi: schemeLocation里添加context的url和spring-contxt.xsd的url:

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd

例如我创建一个能在xml中使用spring-beans模块,spring-txt模块,spring-context模块的配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
">
</beans>

如果没有在beans标签里声明协议,那么在配置bean时会出现找不到指定标签的问题。

2) 开启事务配置

在spring.xml文件中添加配置事务配置,使用 annotation-driven 属性开启事务启动,

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" mode="proxy"/>

proxy-target-class默认为false, mode默认模式为proxy,可不用配置,待会从源码角度分析不同模式的事务开启。

接着配置transactionManager, 指定class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" mode="proxy"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"/>
    </bean>

DataSourceTransactionManager里包含了DataSource属性配置:

因此我们需要接着配置数据源bean 别名为

    <bean id="ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db0?useSSL=false"/>
        <property name="initialSize" value="5"/>
        <property name="maxIdle" value="2"/>
        <property name="maxTotal" value="100"/>
    </bean>

接着给Service配置一个bean, 引用dataSource数据源。

    <!-- 配置bean,指定数据源-->
    <bean id="userService" class="service.UserService">
        <property name="dataSource" ref="ds"/>
    </bean>

3) 创建UserService类

通过dataSouce bean注入JDBCTemplate, 添加一个update(int id,String name)方法, 类上添加@Transactional(propagation = Propagation.REQUIRED)。

package service;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
@Transactional(propagation = Propagation.REQUIRED)
public class UserService {
    private JdbcTemplate jdbcTemplate;
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
    public String getUserName(int id) {
        return jdbcTemplate.query("select * from db0.user where id= ?", rs -> rs.next() ? rs.getString(2) : "", new Object[]{id});
    }
    public void updateUser(int id, String name) {
        jdbcTemplate.update(" update  user set name =? where id= ?", new Object[]{name, id});
//        throw new RuntimeException("error!");
    }
}

4. 测试事务

使用ClassPathXmlApplicationContext类加载spring.xml文件

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class UserServiceTests {
    @Test
    public void testTransaction() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = context.getBean("userService", UserService.class);
        String name = userService.getUserName(1);
        System.out.println("名字:"+name);
        userService.updateUser(1, "bing");
        String updateName = userService.getUserName(1);
        System.out.println("更新后的名字:" + updateName);
    }
}

数据库一条记录:

1) 抛出RuntimeException

update方法里放开//throw new RuntimeException("error!"); 注释,执行后

数据库里的记录没有修改,@Tranasctional注解生效。

2) 注释掉RuntimeException

重新执行后,观察结果

数据库也更新过来了。

前面的篇幅从xml的配置形式解释了Transaction集成过程,为什么要从xml形式入手transaction, 是为了后面阅读Spring-tx源码做准备。

二、事务开启入口TxNamespaceHandler

根据spring.xml文件里配置的tx:annitation-driven 关键字在Spring框架里全局搜索,找到目标类TxNamespaceHandler。位于spring-tx模块中的 org.springframework.transaction.config包下。

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.transaction.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
 * {@code NamespaceHandler} allowing for the configuration of
 * declarative transaction management using either XML or using annotations.
 *
 * <p>This namespace handler is the central piece of functionality in the
 * Spring transaction management facilities and offers two approaches
 * to declaratively manage transactions.
 *
 * <p>One approach uses transaction semantics defined in XML using the
 * {@code <tx:advice>} elements, the other uses annotations
 * in combination with the {@code <tx:annotation-driven>} element.
 * Both approached are detailed to great extent in the Spring reference manual.
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @since 2.0
 */
public class TxNamespaceHandler extends NamespaceHandlerSupport {
	static final String TRANSACTION_MANAGER_ATTRIBUTE = "transaction-manager";
	static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";
	static String getTransactionManagerName(Element element) {
		return (element.hasAttribute(TRANSACTION_MANAGER_ATTRIBUTE) ?
				element.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE) : DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
	}
	@Override
	public void init() {
		registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
		// 注册事务
		registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
		registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
	}
}

找到了annotation-driven, 这个地方创建了一个AnnotationDrivenBeanDefinitionParser实例。

AnnotationDrivenBeanDefinitionParser

AnnotationDrivenBeanDefinitionParser 类的作用是解析spring.xml里的配置<tx:annotation-driven>标签,并根据配置的mode选择不同的模式取创建Transaction的整个初始化流程,此处也就是整个架Transaction架构的开始地方。

Spring事务注册的模式为动态代理模式,具体实现有2种: aspectj和proxy,可通过配置来选择使用那种形式的事务注册, 如果不配置mode那么使用默认的proxy形式创建,如果我们要使用aspectj模式开启事务,那么就配置mode="aspectj"。

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj">

我们可以看到Spring事务的开启是默认是以AOP为基础的。

三、AOP驱动事务

AopAutoProxyConfigurer 的configureAutoProxyCreator方法注册了3个Bean, 该3个Bean 是驱动Spring 事务架构的核心支柱,分别是TransactionAttributeSource、TransactionInterceptor、TransactionAttributeSourceAdvisor。

	private static class AopAutoProxyConfigurer {
		public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
			AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);
			String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
			if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
				Object eleSource = parserContext.extractSource(element);
				// Create the TransactionAttributeSource definition.
				RootBeanDefinition sourceDef = new RootBeanDefinition(
						"org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
				sourceDef.setSource(eleSource);
				sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
				String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);
				// Create the TransactionInterceptor definition.
				RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
				interceptorDef.setSource(eleSource);
				interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
				registerTransactionManager(element, interceptorDef);
				interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
				String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);
				// Create the TransactionAttributeSourceAdvisor definition.
				RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
				advisorDef.setSource(eleSource);
				advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
				advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
				advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
				if (element.hasAttribute("order")) {
					advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
				}
				// 事务通知器 transaction advisor, 基于AOP实现的advisor
				parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);
				CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
				compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
				compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
				compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
				parserContext.registerComponent(compositeDef);
			}
		}
	}

其中TransactionInterceptor是Spring事务的目标方法的增强,通过代理完成Spring 事务的提交、异常处理和回滚。

TransactionInterceptor

TransactionInterceptor是Spring 事务对目标方法的增强器,说简单点就是一层代理,基于Aop实现,实现了spring-aop的Advice接口,同时实现了IntializingBean和BeanFactoryAware接口,只要有事务的执行,那么目标方法的调用类在invoke()方法会生成一个代理对象,通过invoke()方法对目标调用方法进行增强。

@FunctionalInterface
public interface MethodInterceptor extends Interceptor {
	/**
	 * Implement this method to perform extra treatments before and
	 * after the invocation. Polite implementations would certainly
	 * like to invoke {@link Joinpoint#proceed()}.
	 * @param invocation the method invocation joinpoint
	 * @return the result of the call to {@link Joinpoint#proceed()};
	 * might be intercepted by the interceptor
	 * @throws Throwable if the interceptors or the target object
	 * throws an exception
	 */
	Object invoke(MethodInvocation invocation) throws Throwable;
}

TransactionInterceptor的invoke()实现:

	@Override
	@Nullable
	public Object invoke(final 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);
	}

创建事务

Spring 创建事务的方式有二种: 声明式事务和编程式事务, 我们可以通过分析一种理解核心流程和原理即可。

进入invokeWithinTransaction()方法直接看声明式事务执行过程的源代码:

	if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
			// Standard transaction demarcation with getTransaction and commit/rollback calls.
			// 创建事务
			TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
			Object retVal = null;
			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;
		}

invokeWithinTransaction方法做了哪些事?

1)通过createTransactionIfNecessary方法创建一个事务,相当于此处开启一个事务。

2)invocation.proceedWithInvocation() 执行目标方法调用。

3) 如果出现异常,那么completeTransactionAfterThrowing处理异常。

4) 在finally 清除掉transaction相关的信息,同时在commitTransactionAfterReturning 提交事务。

回滚事务

我们可以从上面的源码中发现通过transactionManager执行回滚操作。

txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());

到此这篇关于Spring Transaction事务实现流程源码解析的文章就介绍到这了,更多相关Spring Transaction内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • spring中12种@Transactional的失效场景(小结)

    目录 一.失效场景集一:代理不生效 二.失效场景集二:框架或底层不支持的功能 三.失效场景集三:错误使用@Transactional 四.总结 数据库事务是后端开发中不可缺少的一块知识点.Spring为了更好的支撑我们进行数据库操作,在框架中支持了两种事务管理的方式: 编程式事务声明式事务 日常我们进行业务开发时,基本上使用的都是声明式事务,即为使用@Transactional注解的方式. 常规使用时,Spring能帮我们很好的实现数据库的ACID (这里需要注意哦,Spring只是进行了编程上

  • springboot编程式事务TransactionTemplate的使用说明

    目录 TransactionTemplate的使用 1.为何用? 2.如何用 TransactionTemplate简单使用 TransactionTemplate的使用 总结:在类中注入TransactionTemplate,即可在springboot中使用编程式事务. spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager.对于编程式事务管理,spring推荐使用

  • Spring事务处理Transactional,锁同步和并发线程

    Spring事务传播机制和数据库隔离级别 在标准SQL规范中定义了4个事务隔离级别,不同隔离级别对事务处理不同 . 未授权读取(Read Uncommitted): 也称 未提交读.允许脏读取但不允许更新丢失,如果一个事务已经开始写数据则另外一个数据则不允许同时进行写操作但允许其他事务读此行数据.该隔离级别可以通过 "排他写锁"实现.事务隔离的最低级别,仅可保证不读取物理损坏的数据.与READ COMMITTED 隔离级相反,它允许读取已经被其它用户修改但尚未提交确定的数据. 授权读取

  • Spring中的@Transactional的工作原理

    目录 1.原理 2.用法 3.拓展 1.原理 事务的概念想必大家都很清楚,其ACID特性在开发过程中占有重要的地位.同时在并发过程中会出现一些一致性问题,为了解决一致性问题,也出现了四种隔离级别,这里就不再详述了,感兴趣的可以去查一下.下面我们讨论一下Spring中的事务. Spring中的事务有两种: 编程式事务 声明式事务 通常情况下我们使用声明式事务,它是基于SpringAOP实现的.基于AOP实现的事务极大得帮助了我们的开发效率,其本质是对方法进行前后拦截,在目标方法前加入一个事务,在目

  • 基于Spring中的事务@Transactional细节与易错点、幻读

    目录 为什么要使用事务? 如何使用事务? 事务的传播带来的几种结果 两个特例 事务传播属性propagation 数据库隔离级别 1.未提交读(会有脏读的现象) 2.已提交读 3.可重复读 (有可能覆盖掉其他事务的操作) 4.串行化(没有并发操作) Spring事务隔离级别比数据库事务隔离级别多一个default ACID,事务内的一组操作具有 原子性 .一致性.隔离性.持久性. Atomicity(原子性):一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束

  • Spring事务@Transactional注解四种不生效案例场景分析

    目录 背景 示例代码 1. 类内部访问 2. 私有方法 3. 异常不匹配 4. 多线程 父线程抛出异常 子线程抛出异常 源码解读 @Transactional 执行机制 private 导致事务不生效原因 异常不匹配原因 背景 在我们工作中,经常会用到 @Transactional 声明事务,不正确的使用姿势会导致注解失效,下面就来分析四种最常见的@Transactional事务不生效的 Case: 类内部访问:A 类的 a1 方法没有标注 @Transactional,a2 方法标注 @Tra

  • 浅谈Spring中@Transactional事务回滚及示例(附源码)

    一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用.下面举个栗子:比如一个部门里面有很多成员,这两者分别保存在部门表和成员表里面,在删除某个部门的时候,假设我们默认删除对应的成员.但是在执行的时候可能会出现这种情况,我们先删除部门,再删除成员,但是部门删除成功了,删除成员的时候出异常了.这时候我们希望如果成员删除失败了,之前删除的部门也取消删除.这种场景就可以使用@Transactional事物回滚. 二.checked异常和unc

  • spring声明式事务@Transactional底层工作原理

    目录 引言 工作机制简述 事务AOP核心类释义 @Transactional TransactionAttribute SpringTransactionAnnotationParser AnnotationTransactionAttributeSource TransactionAttributeSourcePointcut TransactionInterceptor BeanFactoryTransactionAttributeSourceAdvisor ProxyTransaction

  • spring声明式事务 @Transactional 不回滚的多种情况以及解决方案

    目录 一. spring 事务原理 问题一.@Transactional 应该加到什么地方,如果加到Controller会回滚吗? 问题二. @Transactional 注解中用不用加rollbackFor = Exception.class 这个属性值 问题三:事务调用嵌套问题具体结果如下代码: 四.总结 五. 参考链接 本文是基于springboot完成测试测试代码地址如下: https://github.com/Dr-Water/springboot-action/tree/master

  • Spring Transaction事务实现流程源码解析

    目录 一.基于xml形式开启Transaction 1. 创建数据库user 2. 创建一个maven 项目 3. 通过xml形式配置事务 1) 创建Spring命名空间 2) 开启事务配置 3) 创建UserService类 4. 测试事务 1) 抛出RuntimeException 2) 注释掉RuntimeException 二.事务开启入口TxNamespaceHandler AnnotationDrivenBeanDefinitionParser 三.AOP驱动事务 Transacti

  • Dubbo3的Spring适配原理与初始化流程源码解析

    目录 引言 Spring Context Initialization FactoryBean BeanDefinition 初始化bean 解决依赖 解决属性 Dubbo Spring的一些问题及解决办法 Dubbo spring 2.7 初始化过程 Dubbo spring 3的初始化过程 属性占位符解决失败 ReferenceBean被过早初始化问题 Reference注解可能出现@Autowire注入失败的问题 引言 Dubbo 国内影响力最大的开源框架之一,非常适合构建大规模微服务集群

  • 浅析Spring Security登录验证流程源码

    一.登录认证基于过滤器链 Spring Security的登录验证流程核心就是过滤器链.当一个请求到达时按照过滤器链的顺序依次进行处理,通过所有过滤器链的验证,就可以访问API接口了. SpringSecurity提供了多种登录认证的方式,由多种Filter过滤器来实现,比如: BasicAuthenticationFilter实现的是HttpBasic模式的登录认证 UsernamePasswordAuthenticationFilter实现用户名密码的登录认证 RememberMeAuthe

  • Kubernetes kubectl中Pod创建流程源码解析

    目录 确立目标 先写一个Pod的Yaml 部署Pod 查询Pod kubectl create 的调用逻辑 Main Match Command Create RunCreate Summary 确立目标 从创建pod的全流程入手,了解各组件的工作内容,组件主要包括以下 kubectl kube-apiserver kube-scheduler kube-controller kubelet 理解各个组件之间的相互协作,目前是kubectl 先写一个Pod的Yaml apiVersion: v1

  • SpringMVC请求流程源码解析

    目录 一.SpringMVC使用 1.工程创建 2.工程配置 3.启动工程 二.SpringMVC启动过程 1.父容器启动过程 2.子容器启动过程(SpringMvc容器) 3.九大组件的初始化 1.处理器映射器的初始化 2.处理器适配器的初始化 4.拦截器的初始化 三.SpringMVC请求过程 1.请求流程图 2.业务描述 一.SpringMVC使用 1.工程创建 创建maven工程. 添加java.resources目录. 引入Spring-webmvc 依赖. <dependency>

  • SpringBoot应用启动流程源码解析

    前言 Springboot应用在启动的时候分为两步:首先生成 SpringApplication 对象 ,运行 SpringApplication 的 run 方法,下面一一看一下每一步具体都干了什么 public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args);

  • Redisson延迟队列执行流程源码解析

    目录 引言 demo示例 SUBSCRIBE指令 zrangebyscore和zrange指令 BLPOP指令 最后定时器源码解析 总结: 引言 在实际分布式项目中延迟任务一般不会使用JDK自带的延迟队列,因为它是基于JVM内存存储,没有持久化操作,所以当服务重启后就会丢失任务. 在项目中可以使用MQ死信队列或redisson延迟队列进行处理延迟任务,本篇文章将讲述redisson延迟队列的使用demo和其执行源码. demo示例 通过脚手架创建一个简易springboot项目,引入rediss

  • Spring Security 实现用户名密码登录流程源码详解

    目录 引言 探究 登录流程 校验 用户信息保存 引言 你在服务端的安全管理使用了 Spring Security,用户登录成功之后,Spring Security 帮你把用户信息保存在 Session 里,但是具体保存在哪里,要是不深究你可能就不知道, 这带来了一个问题,如果用户在前端操作修改了当前用户信息,在不重新登录的情况下,如何获取到最新的用户信息? 探究 无处不在的 Authentication 玩过 Spring Security 的小伙伴都知道,在 Spring Security 中

  • SpringSecurity 默认表单登录页展示流程源码

    SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityInterceptor, 2.ExceptionTranslationFilc,xmccmc,ter , 3.DefaultLoginPageGeneratingFilter 过滤器, 并且简单介绍了 AccessDecisionManager 投票机制  1.准备工作(体验SpringSecurity默认表单认证

  • 详解Android布局加载流程源码

    一.首先看布局层次 看这么几张图 我们会发现DecorView里面包裹的内容可能会随着不同的情况而变化,但是在Decor之前的层次关系都是固定的.即Activity包裹PhoneWindow,PhoneWindow包裹DecorView.接下来我们首先看一下三者分别是如何创建的. 二.Activity是如何创建的 首先看到入口类ActivityThread的performLaunchActivity方法: private Activity performLaunchActivity(Activi

随机推荐