【复习使用】Spring基础知识点回顾

2023/8/11

基于XML管理Bean

入门案例

  1. 导入依赖
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.3.1</version>
</dependency>
  1. 创建类
package com.march.context.beans;

public class Hello {
    public void say(){
        System.out.println("heplllw");
    }
}

  1. 创建配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="hello" class="com.march.context.beans.Hello"></bean>
</beans>
  1. 创建容器并加载
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello)ioc.getBean("hello");
hello.say();

注意:遇到创建容器报错的可以pom文件中加入build

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
  </resources>
</build>

获取Bean

image.png

  1. 根据id获取
    :::tips
    Hello hello = (Hello)ioc.getBean(“hello”);
    :::

  2. 根据类型获取
    :::tips
    Hello hello1 = ioc.getBean(Hello.class);
    :::

  3. 根据id和类型
    :::tips
    Hello hello2 = ioc.getBean(“hello”, Hello.class);
    :::
    注意:

  • 获取Bean的时候,IOC容器中的Bean有且仅有一个。否者会报错:NoUniqueBeanDefinitionException
  • 如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型可以获取 bean 吗? 不行,因为bean不唯一

Setter注入

  1. 编写实体类
@Data
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}
  1. xml配置属性
<bean id="studentOne" class="com.march.context.beans.Student">
    <!-- property标签:通过组件类的setXxx()方法给组件对象设置属性 -->
    <!-- name属性:指定属性名(这个属性名是getXxx()setXxx()方法定义的,和成员变量无关)-->
    <!-- value属性:指定属性值 -->
    <property name="id" value="1001"></property>
    <property name="name" value="张三"></property>
    <property name="age" value="23"></property>
    <property name="sex" value="男"></property>
</bean>
  1. 测试
@Test
public void setterTest(){

    ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
    Student bean = ioc.getBean(Student.class);
    System.out.println(bean);
}

构造器注入

  1. 实体类加入相应的构造器
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;

}
  1. 配置XML文件
<bean id="studentTwo" class="com.march.context.beans.Student">
    <constructor-arg value="1002"></constructor-arg>
    <constructor-arg value="李四"></constructor-arg>
    <constructor-arg value="33"></constructor-arg>
    <constructor-arg value="女"></constructor-arg>
</bean>
  • constructor-arg 赋值顺序与构造器参数顺序一致
  • 会找四个参数的构造器,找不到会报错

image.png

  • 指定顺序可以用constructor-arg标签上的两个属性
    • index属性:指定参数所在位置的索引(从0开始)。
    • name属性:指定参数名称。
  1. 测试
@Test
public void argTest(){

    ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
    Student bean = ioc.getBean("studentTwo",Student.class);
    System.out.println(bean);
}

注入时的特殊值处理

:::tips

:::
在注入时,value属性值会赋值给id,这里的1001会看成字面量

  1. null值
<property name="name">
	<null />
</property>

注意:
:::tips

:::
这种写法,为name所赋的值是字符串null

  1. XML实体
<!-- 小于号在XML文档中用来定义标签的开始,不能随便使用 -->
<!-- 解决方案一:使用XML实体来代替 -->
<property name="expression" value="a &lt; b"/>
  1. CDATA
<property name="expression">
<!-- 解决方案二:使用CDATA-->
<!-- CDATA中的C代表Character,是文本、字符的含义,CDATA就表示纯文本数据 -->
<!-- XML解析器看到CDATA节就知道这里是纯文本,就不会当作XML标签或属性来解析 -->
<!-- 所以CDATA节中写什么符号都随意 -->
<value><![CDATA[a < b]]></value>
</property>

类类型属性赋值

  1. 引用外部已声明的Bean

ref和value的区别

<bean id="studentOne" class="com.march.context.beans.Student">
    <property name="id" value="1001"></property>
    <property name="name" value="张三"></property>
    <property name="age" value="23"></property>
    <property name="sex" value="男"></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazzOne"></property>

</bean>

注意: 如果错把ref属性写成了value属性,会抛出异常: Caused by: java.lang.IllegalStateException

  1. 内部Bean
<bean id="studentFour" class="com.march.context.beans.Student">
        <property name="id" value="1004"></property>
        <property name="name" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="sex" value="女"></property>
        <property name="clazz">
            <!-- 在一个bean中再声明一个bean就是内部bean -->
            <!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
            <bean class="com.march.context.beans.Clazz">
                <property name="clazzId" value="2222"></property>
                <property name="clazzName" value="前程似锦"></property>
            </bean>
        </property>
    </bean>
  1. 级联属性赋值
<bean id="studentFour" class="com.march.context.beans.Student">
        <property name="id" value="1004"></property>
        <property name="name" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="sex" value="女"></property>
        <!-- 一定先引用某个bean为属性赋值,才可以使用级联方式更新属性 -->
        <property name="clazz" ref="clazzOne"></property>
        <property name="clazz.clazzId" value="3333"></property>
        <property name="clazz.clazzName" value="最强王者班"></property>
    </bean>

数组类型赋值

<property name="hobbies">
    <array>
        <value>抽烟</value>
        <value>喝酒</value>
        <value>烫头</value>
    </array>
</property>

集合类型赋值

  1. List
<property name="students">
    <list>
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </list>
</property>
  1. Map
<property name="teacherMap">
    <map>
        <entry>
            <key>
            	<value>10010</value>
        	</key>
        	<ref bean="teacherOne"></ref>
        </entry>
        <entry>
            <key>
            	<value>10086</value>
            </key>
       		 <ref bean="teacherTwo"></ref>
        </entry>
    </map>
</property>
  1. 集合类型的Bean
<!--list集合类型的bean-->
<util:list id="students">
    <ref bean="studentOne"></ref>
    <ref bean="studentTwo"></ref>
    <ref bean="studentThree"></ref>
</util:list>
<!--map集合类型的bean-->
<util:map id="teacherMap">
    <entry>
        <key>
     	   <value>10010</value>
        </key>
   		<ref bean="teacherOne"></ref>
    </entry>
    <entry>
        <key>
        	<value>10086</value>
        </key>
    	<ref bean="teacherTwo"></ref>
    </entry>
</util:map>

使用util:list、util:map标签必须引入相应的命名空间,可以通过idea的提示功能选择

所以在第二步中可以写成:

<property name="teacherMap" ref="teacherMap"></property>

命名空间

p根据idea提示创建

 <bean  id="student" class="com.march.context.beans.Student"
	    p:id="1006" p:name="小明" p:clazz-ref="clazzOne" >
</bean>

引入外部属性文件

以配置数据库为例

  1. 引入依赖
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.31</version>
</dependency>
  1. jdbc.properties文件
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
  1. 配置IOC-XML
<!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
  1. 测试
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
DruidDataSource druidDataSource = ioc.getBean("druidDataSource", DruidDataSource.class);
System.out.println(druidDataSource);

打印结果:

{
  CreateTime:"2023-08-11 15:33:52",
  ActiveCount:0,
  PoolingCount:0,
  CreateCount:0,
  DestroyCount:0,
  CloseCount:0,
  ConnectCount:0,
  Connections:[
  ]
}

Bean的作用域

在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义参加下表:

取值含义创建对象的时机
singleton (默认)在IOC容器中,这个bean的对象始终为单实例IOC容器初始化时
prototype这个bean在IOC容器中有多个实例获取bean时

配置:

<bean class="com.march.bean.User" scope="prototype"></bean>

测试:

@Test
public void testBeanScope(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  User user1 = ac.getBean(User.class);
  User user2 = ac.getBean(User.class);
System.out.println(user1==user2);
}

Bean的生命周期

  1. 生命周期过程
  • bean对象创建(调用无参构造器)
  • 给bean对象设置属性( setter注入)
  • bean对象初始化之前操作(由bean的前置处理器负责)
  • bean对象初始化(需在配置bean时指定初始化方法 init-method=“initMethod”)
  • bean对象初始化之后操作(由bean的后置处理器负责)
  • bean对象就绪可以使用
  • bean对象销毁(需在配置bean时指定销毁方法 destroy-method=“destroyMethod”)
  • IOC容器关闭

初始化前置、后置处理器:

public class MyBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
    throws BeansException {
        System.out.println("☆☆☆" + beanName + " = " + bean);
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
    throws BeansException {
        System.out.println("★★★" + beanName + " = " + bean);
        return bean;
    }
}

基于XML自动装配

自动装配:

根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类 型属性赋值

配置Bean

属性名:autowire

  1. 自动装配方式byType

根据类型匹配IOC容器中的某个兼容类型的bean,为属性自动赋值

  1. 自动装配方式byName

将自动装配的属性的属性名,作为bean的id在IOC容器中匹配相对应的bean进行赋值

<bean id="studentSix" class="com.march.context.beans.Student"
      autowire="byType"
      init-method="initMethod"
      destroy-method="destroyMethod">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value="女"></property>
</bean>

基于注解管理bean

和 XML 配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测 到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。

包扫描

  1. 基本扫描方式
<context:component-scan base-package="com.march"></context:component-scan>
  1. 指定要排除的组件
<context:component-scan base-package="com.march">
<!-- context:exclude-filter标签:指定排除规则 -->
<!--
    type:设置排除或包含的依据
    type="annotation",根据注解排除,expression中设置要排除的注解的全类名
    type="assignable",根据类型排除,expression中设置要排除的类型的全类名
-->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<!--<context:exclude-filter type="assignable"
expression="com.atguigu.controller.UserController"/>-->
</context:component-scan>

  1. 扫描指定组件
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<!-- context:include-filter标签:指定在原有扫描规则的基础上追加的规则 -->
<!-- use-default-filters属性:取值false表示关闭默认扫描规则 -->
<!-- 此时必须设置use-default-filters="false",因为默认规则即扫描指定包下所有类 -->
<!--
      type:设置排除或包含的依据
      type="annotation",根据注解排除,expression中设置要排除的注解的全类名
      type="assignable",根据类型排除,expression中设置要排除的类型的全类名
-->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<!--<context:include-filter type="assignable"
expression="com.atguigu.controller.UserController"/>-->
</context:component-scan>

注解Bean的ID

在我们使用XML方式管理bean的时候,每个bean都有一个唯一标识,便于在其他地方引用。现在使用 注解后,每个组件仍然应该有一个唯一标识。
默认情况:类名首字母小写就是bean的id。例如:UserController类对应的bean的id就是userController。 、
自定义id: @Service(“userService”) //默认为userServiceImpl public class UserServiceImpl implements UserService {}

4.基于注解自动装配

@Autowired注解

在成员变量上直接标记@Autowired注解即可完成自动装配,不需要提供setXxx()方法。以后我们在项 目中的正式用法就是这样。

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    public void saveUser(){
        userService.saveUser();
    }
}

@Autowired工作流程

image.png

  • 首先根据所需要的组件类型到IOC容器中查找
    • 能够找到唯一的bean:直接执行装配
    • 如果完全找不到匹配这个类型的bean:装配失败
    • 和所需类型匹配的bean不止一个
      • 没有@Qualifier注解:根据@Autowired标记位置成员变量的变量名作为bean的id进行 匹配
        • 能够找到:执行装配
        • 找不到:装配失败
      • 使用@Qualifier注解:根据@Qualifier注解中指定的名称作为bean的id进行匹配
        • 能够找到:执行装配
        • 找不到:装配失败

@Autowired中有属性required,默认值为true,因此在自动装配无法找到相应的bean时,会装 配失败 可以将属性required的值设置为true,则表示能装就装,装不上就不装,此时自动装配的属性为 默认值

5.AOP

概述

AOP(Aspect Oriented Programming)是一种设计思想,是软件设计领域中的面向切面编程,它是面 向对象编程的一种补充和完善,它以通过预编译方式和运行期动态代理方式实现在不修改源代码的情况 下给程序动态统一添加额外功能的一种技术。 

理解

简单理解AOP代表的是一个横向的关 系,将“对象”比作一个空心的圆柱体,其中封装的是对象的属性和行为;则面向方面编程的方法,就是将这个圆柱体以切面形式剖开,选择性的提供业务逻辑。而 剖开的切面,也就是所谓的“方面”了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹,但完成了效果。
在以往函数式编程中,例如用户详情和用户删除,都是传用户id来进行用户的查找,仅仅是中间有段业务代码所做功能的不同,这种函数没法拆开封装,导致过多重复的代码片段。这里如果用AOP将公共代码抽取出来,通过切面加入功能即可。

相关术语

横切关注点
从每个方法中抽取出来的同一类非核心业务。在同一个项目中,我们可以使用多个横切关注点对相关方 法进行多个不同方面的增强。
通知

每一个横切关注点上要做的事情都需要写一个方法来实现,这样的方法就叫通知方法。

  1. 前置通知:在被代理的目标方法前执行
  2. 返回通知:在被代理的目标方法成功结束后执行(寿终正寝)
  3. 异常通知:在被代理的目标方法异常结束后执行(死于非命)
  4. 后置通知:在被代理的目标方法最终结束后执行(盖棺定论)
  5. 环绕通知:使用try…catch…finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所 有位置
切面
封装通知方法的类。

作用

  1. 简化代码:把方法中固定位置的重复的代码抽取出来,让被抽取的方法更专注于自己的核心功能, 提高内聚性。
  2. 代码增强:把特定的功能封装到切面类中,看哪里有需要,就往上套,被套用了切面逻辑的方法就 被切面给增强了。

切入点表达式

image.png

代码示例

切面类

@Aspect
@Component
public class LogAspect {

    @Before("execution(public int com.march.context.aop.CalculatorPureImpl.* (..))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
    }
    @After("execution(* com.march.context.aop.CalculatorPureImpl.* (..))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->后置通知,方法名:"+methodName);
    }
    @AfterReturning(value = "execution(* com.march.context.aop.CalculatorPureImpl.* (..))", returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
    }
    @AfterThrowing(value = "execution(* com.march.context.aop.CalculatorPureImpl.* (..))", throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
        return;
    }

    @Around(value = "execution(* com.march.context.aop.CalculatorPureImpl.* (..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){

        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        Object result = null;
        try {
            System.out.println("环绕通知-->目标对象方法执行之前");
            //目标对象(连接点)方法的执行
            result = joinPoint.proceed();
            System.out.println("环绕通知-->目标对象方法返回值之后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知-->目标对象方法出现异常时");
        } finally {
            System.out.println("环绕通知-->目标对象方法执行完毕");
        }
        return result;
    }
}

测试打印结果

环绕通知-->目标对象方法执行之前
Logger-->前置通知,方法名:div,参数:[1, 1]
方法内部 result = 1
Logger-->返回通知,方法名:div,结果:1
Logger-->后置通知,方法名:div
环绕通知-->目标对象方法返回值之后
环绕通知-->目标对象方法执行完毕

重用切入点表达式

声明
@Pointcut("execution(* com.march.context.aop.CalculatorPureImpl.* (..))")
public void pointCut(){}
使用
@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

获取通知的相关信息

获取连接点信息
//获取连接点的签名信息
String methodName = joinPoint.getSignature().getName();
//获取目标方法到的实参信息
String args = Arrays.toString(joinPoint.getArgs());
获取目标方法的返回值
@AfterReturning(value = "execution(* com.march.context.aop.CalculatorPureImpl.* (..))", returning = "result")
public void afterReturningMethod(JoinPoint joinPoint, Object result){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
}
获取目标方法的异常
@AfterThrowing(value = "execution(* com.march.context.aop.CalculatorPureImpl.* (..))", throwing = "ex")
优先级

相同目标方法上同时存在多个切面时,切面的优先级控制切面的内外嵌套顺序。

  • 优先级高的切面:外面
  • 优先级低的切面:里面

使用 @Order 注解可以控制切面的优先级:

  • @Order(较小的数):优先级高
  • @Order(较大的数):优先级低

image.png

6. 声明式事务

@Transactional注解

  • @Transactional标识在方法上,咋只会影响该方法
  • @Transactional标识的类上,咋会影响类中所有的方法

事务属性:只读

 对一个查询操作来说,如果我们把它设置成只读,就能够明确告诉数据库,这个操作不涉及写操作。这 样数据库就能够针对查询操作来进行优化。  

@Transactional(readOnly = true)

事务属性:超时

事务在执行过程中,有可能因为遇到某些问题,导致程序卡住,从而长时间占用数据库资源。而长时间 占用资源,大概率是因为程序运行出现了问题(可能是Java程序或MySQL数据库或网络连接等等)。 此时这个很可能出问题的程序应该被回滚,撤销它已做的操作,事务结束,把资源让出来,让其他正常 程序可以执行。  

@Transactional(timeout = 3)

事务属性:回滚策略

声明式事务默认只针对运行时异常回滚,编译时异常不回滚。
可以通过@Transactional中相关属性设置回滚策略

  • rollbackFor属性:需要设置一个Class类型的对象
  • rollbackForClassName属性:需要设置一个字符串类型的全类名
  • noRollbackFor属性:需要设置一个Class类型的对象
  • rollbackFor属性:需要设置一个字符串类型的全类名

@Transactional(noRollbackFor = ArithmeticException.class)

事务属性:事务隔离级别

数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事 务与其他事务隔离的程度称为隔离级别。SQL标准中规定了多种事务隔离级别,不同隔离级别对应不同 的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱。 

** 隔离级别一共有四种:**

  • 读未提交:READ UNCOMMITTED

允许Transaction01读取Transaction02未提交的修改。

  • 读已提交:READ COMMITTED

要求Transaction01只能读取Transaction02已提交的修改。

  • 可重复读:REPEATABLE READ

确保Transaction01可以多次从一个字段中读取到相同的值,即Transaction01执行期间禁止其它 事务对这个字段进行更新。

  • 串行化:SERIALIZABLE

确保Transaction01可以多次从一个表中读取到相同的行,在Transaction01执行期间,禁止其它 事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
使用方式

@Transactional(isolation = Isolation.DEFAULT)//使用数据库默认的隔离级别
@Transactional(isolation = Isolation.READ_UNCOMMITTED)//读未提交
@Transactional(isolation = Isolation.READ_COMMITTED)//读已提交
@Transactional(isolation = Isolation.REPEATABLE_READ)//可重复读
@Transactional(isolation = Isolation.SERIALIZABLE)//串行化

** 各个隔离级别解决并发问题的能力见下表: **
image.png

脏读:

脏读指的是读到了其他事务未提交的数据,未提交意味着这些数据可能会回滚,也就是可能最终不会存到数据库中,也就是不存在的数据。读到了并一定最终存在的数据,这就是脏读。
image.png
脏读最大的问题就是可能会读到不存在的数据。比如在上图中,事务B的更新数据被事务A读取,但是事务B回滚了,更新数据全部还原,也就是说事务A刚刚读到的数据并没有存在于数据库中。

不可重复读

不可重复读指的是在一个事务内,最开始读到的数据和事务结束前的任意时刻读到的同一批数据出现不一致的情况。
image.png
事务 A 多次读取同一数据,但事务 B 在事务A多次读取的过程中,对数据作了更新并提交,导致事务A多次读取同一数据时,结果 不一致。

幻读

select 某记录是否存在,不存在,准备插入此记录,但执行 insert 时发现此记录已存在,无法插入,此时就发生了幻读。
这里是在RR级别下研究(可重复读),因为 RU / RC 下还会存在脏读、不可重复读,故我们就以 RR 级别来研究 幻读,排除其他干扰。
1、事务A,查询是否存在 id=5 的记录,没有则插入,这是我们期望的正常业务逻辑。
2、这个时候 事务B 新增的一条 id=5 的记录,并提交事务。
3、事务A,再去查询 id=5 的时候,发现还是没有记录(因为这里是在RR级别下研究(可重复读),所以读到依然没有数据)
4、事务A,插入一条 id=5 的数据。
最终 事务A 提交事务,发现报错了。这就很奇怪,查的时候明明没有这条记录,但插入的时候 却告诉我 主键冲突,这就好像幻觉一样。这才是所有的幻读。

事务属性:事务传播行为

当事务方法被另一个事务方法调用时,必须指定事务应该如何传播。例如:方法可能继续在现有事务中 运行,也可能开启一个新事务,并在自己的事务中运行。  

@Transactional中的propagation属性设置事务传播行为

@Transactional(propagation = Propagation.REQUIRED)

默认情况,表示如果当前线程上有已经开 启的事务可用,那么就在这个事务中运行。经过观察,购买图书的方法buyBook()在checkout()中被调 用,checkout()上有事务注解,因此在此事务中执行。所购买的两本图书的价格为80和50,而用户的余 额为100,因此在购买第二本图书时余额不足失败,导致整个checkout()回滚,即只要有一本书买不 了,就都买不了

@Transactional(propagation = Propagation.REQUIRES_NEW)

表示不管当前线程上是否有已经开启 的事务,都要开启新事务。同样的场景,每次购买图书都是在buyBook()的事务中执行,因此第一本图 书购买成功,事务结束,第二本图书购买失败,只在第二次的buyBook()中回滚,购买第一本图书不受 影响,即能买几本就买几本

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值