spring2
基于注释的容器配置
注释在配置Spring方面比XML更好吗?
注释注入在XML注入之前执行。因此,XML配置将覆盖通过两种方法连接的属性的注释。
使用java方式配置spring
完全不是有spring配置文件,完全交给java'来做
javaConfig 早时是spring的一个子项 spring 之后成为了一个核心功;
import com.sl.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//类上使用@Configuration注解声明这个是配置类配spring接管,注册到了容器中;
@Configuration
//导入其他的配置类
@Import(com.sl.config.MyConfig.class)
public class MyConfig {
//由配置来创建对象 @Bean声明对象;
@Bean
public User getUser(){
return new User();
}
}
//Bean
public class User{
private String Name;
public String getName() {
return Name;
}
//为属性注入值;
@Value("张三")
public void setName(String name) {
Name = name;
}
@Override
public String toString() {
return "User{" +
"Name='" + Name + '\'' +
'}';
}
}
//测试类
import com.sl.config.MyConfig;
import com.sl.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Tests {
public static void main(String[] args) {
//如果使用这种配置(纯java) 这种配置需要使用 AnnotationConfig来获取上下文;
//这种纯java配值在spring Boot 随处可见;
ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
User user = (User) app.getBean("getUser");
System.out.println(user.getName());
}
}
代理模式
为什么要学代理模式?因为这就是spring AOP的低层【spring 和 spring MVC】
静态代理
角色分析
抽象角色 一般使用抽象的类或接口;
真实角色 被代理的角色;
代理角色 代理真实的角色 代理真实角色后 我们一般会进行一些附属操作;
客户 访问代理角色的人;
代理模式的好处:
- 可以使真实角色更纯粹,不用关心公共的业务;
- 代理角色负责公共的业务,实现了业务的分工;
- 公共业务拓展时,方便集中管理 ;
- 缺点很明显,不能做到动态的代理真实对象 , 一个真实对象就需要哟个代理;
代码步骤
- 1.接口 ```java package com.sl.dao;
public interface Rent { public void rent(); }
-
2.真实对象
```java
package com.sl.pojo;
import com.sl.dao.Rent;
public class Fd implements Rent {
public void rent() {
System.out.println("房东出租房子");
}
}
- 3.代理对象 ```java package com.sl.config;
import com.sl.pojo.Fd;
public class Dl { private Fd fd; public Dl(){
}
public Dl(Fd fd) {
this.fd = fd;
}
public void rent(){
Kan();
fd.rent();
Qian();
}
//看房
public void Kan(){
System.out.println("看房");
}
//签合同
public void Qian(){
System.out.println("签合同");
}
}
-
4.客户端访问代理对象
```java
import com.sl.config.Dl;
import com.sl.pojo.Fd;
public class Tests {
public static void main(String[] args) {
//真实对象
Fd f = new Fd();
//代理真实对象
Dl dl = new Dl(f);
//开始业务
dl.rent();
}
}
动态代理
动态代理的类是自动生成的;
角色与静态代理一致
动态代理分为两大类 基于接口的 和基于类的
- 基于接口的代理 ——>JDK 动态代理
- 基于类 : cglib
- java字节码实现 : javasist
代理模式的好处:
//这个类自动生成 代理类 实现接口InvocationHandler
public class Dl implements InvocationHandler {
//被代理的接口
private Object targer;
//设置需要代理的类
public void setRent(Object rent) {
this.targer = rent;
}
//生成得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(
this.getClass().getClassLoader(),
targer.getClass().getInterfaces(),
this);
}
//处理代理实例,并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object object = method.invoke(targer,args);
return object;
}
}
//测试类
public static void main(String[] args) {
//真实角色
Fd f = new Fd();
//代理对象;暂时没有
Dl d =new Dl();
//设置需要代理了的对象
d.setRent(f);
//动态生成生成代理类 (代理的那个接口 就转成那个接口)
Rent rent = (Rent) d.getProxy();
//调用方法即
rent.rent();
}
Aop 面向切面思想
aop在spring中的作用
=提供声明事务 , 允许用户自定切面=
横切关注点:跨应用多个模块的方法 或者功能 ;及与业务逻辑无关 ,且需要我们关注的功能点;称之为 横切关注点
比如:日志 事务 安全
切面(ASPCET):横切关注点被模块化为一个特殊的对象 : 类
通知(Advice):必须要完成的一个工作 : 必须执行的方法
通知目标(Target):被通知的对象
代理(Proxy):向目标对象应用通知后创建的对象
切入点(PointCut):切面通知执行 "地点" 的定义
接入点(JOinPoint):与切入点匹配的执行点;
使用 spring aop
1.需要导入的mave
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
方式 一 时用spring API 接口
//执行前
public class Log implements MethodBeforeAdvice {
//method : 要执行的目标对象方法
//object 参数类型
//target 目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"方法");
}
}
//执行后
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"方法"
+"的返回结果为"+returnValue);
}
}
//接口
public interface UserService {
void add();
void select();
void update();
void delete();
}
//接口实现
public class UserServiceImple implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void select() {
System.out.println("查询用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
}
//测试
public class MyTest {
public static void main(String [] args){
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applictionContext.xml");
Hello h = (Hello) context.getBean("h");
System.out.println(h.toString());
}
}
<!--配置aop ;需要导入aop约束-->
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 方式一 使用原生态Spring API核心-->
<aop:config>
<!--切入点 : expression:表达式 execution(需要执行的位置)-->
<!--
execution(* sl.service.UserServiceImple.*(..))
第一个 * 表示返回值
sl.service.UserServiceImple 表示切入的类
第二个 * 代表 类下的所有方法
(..) 方法参数 表示 方法内参数可有可无
-->
<aop:pointcut id="pointcut" expression="execution(* sl.service.UserServiceImple.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
方式 二 自定义类 实现AOP
//定义 diy 类
public class AopDiy {
public void before(){
System.out.println("执行前");
}
public void after(){
System.out.println("执行后");
}
}
//测试
//测试
public class MyTest {
public static void main(String [] args){
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applictionContext.xml");
Hello h = (Hello) context.getBean("h");
System.out.println(h.toString());
}
}
<!--方式二 自定义diy类 实现aop-->
<!--注册 beans-->
<bean id="aopDiy" class="sl.diy.AopDiy"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="aopDiy">
<!-- 定义切入点-->
<aop:pointcut id="point" expression="execution(* sl.service.UserServiceImple.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式 三 注解 实现 AOP
//使用注解实现 aop
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//标注这是一个切面
public class AnnotationPointCut {
//后置增强
@Before("execution(* sl.service.UserServiceImple.*(..))")
public void before(){
System.out.println("执行前");
}
//前置增强
@After("execution(* sl.service.UserServiceImple.*(..))")
public void after(){
System.out.println("执行后");
}
//环绕增强 我们可以定义一个参数 ,代表我们要切入的点;
@Around("execution(* sl.service.UserServiceImple.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
//获取签名
Signature signature = joinPoint.getSignature();
//执行方法
Object o = joinPoint.proceed();
System.out.println("环绕后");
System.out.println(signature);
}
}
<!--方式三-->
<bean id="annotationPointCut" class="sl.zhujie.AnnotationPointCut"/>
<!--开启注解支持
两种
1. 默认 JDK(proxy-target-class="false")
2. ture cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy />
整合 mybatis
步骤
1.导入相关jar 包
junit
mybatis
mysql 数据库
spring 相关
aop 植入
mybatis - spring [new]
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
2.编写程序
3.测试
回忆mybatis
1.编写实体类
@Data
public class EasybuyUser {
private long id;
private String loginName;
private String userName;
private String password;
private long sex;
private String identityCode;
private String email;
private String mobile;
private long type;
}
2.编写核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0/EN "
"http://mybatis.org/dtd/mybatis-3-config.dtd"
>
<!--mybatis的主要配置文件-->
<configuration>
<!--导入配置文件-->
<properties resource="db.properties">
<property name="driver" value="com.mysql.jdbc.Driver"/>
</properties>
<!--是否开启事务日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="sl.pojo"/>
</typeAliases>
<!-- 配置环境-->
<environments default="mysql">
<!--配置MySQL环境-->
<environment id="mysql">
<!--配置事务类型-->
<transactionManager type="JDBC"/>
<!--配置数据源-->
<dataSource type="POOLED">
<!--连接数据库的四个基本信息-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="sl.mapper.UserMapper"/>
</mappers>
</configuration>
3.编写接口
public interface UserMapper {
public List<EasybuyUser> select();
}
4.编写Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0/EN "
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sl.mapper.UserMapper">
<select id="select" resultType="easybuyUser">
select * from easybuy_user;
</select>
</mapper>
5.测试
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import sl.mapper.UserMapper;
import sl.pojo.EasybuyUser;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Tests {
@Test
public void test() throws IOException {
String res = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(res);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<EasybuyUser> list = userMapper.select();
for (EasybuyUser user : list) {
System.out.println(user);
}
}
}
4.整合spring mybatis 01
Mapper mybatis 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0/EN "
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sl.mapper.UserMapper">
<select id="select" resultType="easybuyUser">
select * from easybuy_user;
</select>
</mapper>
spring 配置文件
<?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:p ="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--DataSource :使用Spring的数据源配置 Mybatis 的配置
使用 Spring 提供的jdbc
org.springframework.jdbc.datasource.DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easybuy"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--SqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定 Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:sl/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate :这是我们使用的 sqlSession'-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--使用构造方法注入 因为没有 set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="sl.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
配置主 xml 文件
<?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:p ="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<import resource="springdao.xml"/>
<bean id="userMapper" class="sl.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
实现类
public class UserMapperImpl implements UserMapper {
//我们所有的操作 都使用SqlSessionTemplate来执行 ,在原来使用的时SqlSeesion;
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<EasybuyUser> select() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.select();
}
}
测试
public class Tests {
@Test
public void test() throws IOException {
ApplicationContext context =new ClassPathXmlApplicationContext("appLoctionContext.xml");
UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
for (EasybuyUser user : userMapper.select()) {
System.out.println(user);
}
}
}
spring 配置文件详解
<!--1.配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easybuy"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--2.SqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定 Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:sl/mapper/*.xml"/>
</bean>
<!--3.SqlSessionTemplate :这是我们使用的 sqlSession'-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--使用构造方法注入 因为没有 set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
5.整合spring mybatis 02
Mapper mybatis 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0/EN "
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sl.mapper.UserMapper">
<select id="select" resultType="easybuyUser">
select * from easybuy_user;
</select>
</mapper>
spring配置文件
<?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:p ="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--1.配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easybuy"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--2.SqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定 Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:sl/mapper/*.xml"/>
</bean>
主配置文件
<?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:p ="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<import resource="springdao.xml"/>
<bean id="userMapper" class="sl.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
<bean id="userMapper2" class="sl.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
mybatis 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0/EN "
"http://mybatis.org/dtd/mybatis-3-config.dtd"
>
<!--mybatis的主要配置文件-->
<configuration>
<!--是否开启事务日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="sl.pojo"/>
</typeAliases>
</configuration>
继承 SqlSessionDaoSupport
import org.mybatis.spring.support.SqlSessionDaoSupport;
import sl.pojo.EasybuyUser;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
public List<EasybuyUser> select() {
return getSqlSession().getMapper(UserMapper.class).select();
}
}
测试
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sl.mapper.UserMapper;
import sl.pojo.EasybuyUser;
import java.io.IOException;
public class Tests {
@Test
public void test() throws IOException {
ApplicationContext context =new ClassPathXmlApplicationContext("appLoctionContext.xml");
UserMapper userMapper = context.getBean("userMapper2",UserMapper.class);
for (EasybuyUser user : userMapper.select()) {
System.out.println(user);
}
}
}
声明式事务
1.回顾事务
1.把一组业务当成一个业务来做要么都成功,要么都失败
2.数据的一致性 和完整性
事务的ACID 原则
1.原子型
事务是数据库的逻辑工作单位,事务中包含的各操作要么都做,要么都不做
2.一致性
事 务执行的结果必须是使数据库从一个一致性状态变到另一个一致性状态。因此当数据库只包含成功事务提交的结果时,就说数据库处于一致性状态
3.隔离性
一个事务的执行不能被其它事务干扰。即一个事务内部的操作及使用的数据对其它并发事务是隔离的,并发执行的各个事务之间不能互相干扰。
4.持久性
事务一旦提交,无论系统发生什么问题,结果都不会被影响,被持久化的写到储存器当中;
2.声明式事务 配置
在spring 配置
<!--配置声明式事务-->
<bean id="transactioManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSources"/>
</bean>
<!--结合AOP 实现事务的植入-->
<!--配置事务通知-->
<tx:advice id="txtADVICE" transaction-manager="transactioManager">
<!--给那些方法配置事务-->
<tx:attributes>
<tx:method name="addUser" propagation="REQUIRED"/>
<tx:method name="delUser" propagation="REQUIRED"/>
<tx:method name="selectUser" propagation="REQUIRED"/>
<!-- <tx:method name="selectUser" read-only="true"/>-->
<!--所有方法开启事务-->
<!--<tx:method name="*" propagation="REQUIRED"/>-->
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPoinyCut" expression="execution(* sl.mapper.*.*(..))"/>
<aop:advisor advice-ref="txtADVICE" pointcut-ref="txPoinyCut"/>
</aop:config>
事务的传播性
1.什么是事务的传播事务传播行为
用来描述由某一个事务传播行为修饰的方法被嵌套进另一个方法的时事务如何传播。
用伪代码说明:
public void methodA(){
methodB();
//doSomething
}
@Transaction(Propagation=XXX)
public void methodB(){
//doSomething
}
代码中methodA()方法嵌套调用了methodB()方法,methodB()的事务传播行为由@Transaction(Propagation=XXX)设置决定。这里需要注意的是methodA()并没有开启事务,某一个事务传播行为修饰的方法并不是必须要在开启事务的外围方法中调用。
2. Spring中七种事务传播行为
| 事务传播行为类型 | 说明 |
|---|---|
| PROPAGATION_REQUIRED | 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 |
| PROPAGATION_SUPPORTS | 支持当前事务,如果当前没有事务,就以非事务方式执行。 |
| PROPAGATION_MANDATORY | 使用当前的事务,如果当前没有事务,就抛出异常。 |
| PROPAGATION_REQUIRES_NEW | 新建事务,如果当前存在事务,把当前事务挂起。 |
| PROPAGATION_NOT_SUPPORTED | 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 |
| PROPAGATION_NEVER | 以非事务方式执行,如果当前存在事务,则抛出异常。 |
| PROPAGATION_NESTED | 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。 |
在外围方法未开启事务的情况下Propagation.REQUIRED修饰的内部方法会新开启自己的事务,且开启的事务相互独立,互不干扰。
在外围方法开启事务的情况下Propagation.REQUIRED修饰的内部方法会加入到外围方法的事务中,所有Propagation.REQUIRED修饰的内部方法和外围方法均属于同一事务,只要一个方法回滚,整个事务均回滚。
在外围方法未开启事务的情况下Propagation.REQUIRES_NEW修饰的内部方法会新开启自己的事务,且开启的事务相互独立,互不干扰。
在外围方法开启事务的情况下Propagation.REQUIRES_NEW修饰的内部方法依然会单独开启独立事务,且与外部方法事务也独立,内部方法之间、内部方法和外部方法事务均相互独立,互不干扰。
在外围方法未开启事务的情况下Propagation.NESTED和Propagation.REQUIRED作用相同,修饰的内部方法都会新开启自己的事务,且开启的事务相互独立,互不干扰。
在外围方法开启事务的情况下Propagation.NESTED修饰的内部方法属于外部事务的子事务,外围主事务回滚,子事务一定回滚,而内部子事务可以单独回滚而不影响外围主事务和其他子事务
NESTED和REQUIRED修饰的内部方法都属于外围方法事务,如果外围方法抛出异常,这两种方法的事务都会被回滚。但是REQUIRED是加入外围方法事务,所以和外围事务同属于一个事务,一旦REQUIRED事务抛出异常被回滚,外围方法事务也将被回滚。而NESTED是外围方法的子事务,有单独的保存点,所以NESTED方法抛出异常被回滚,不会影响到外围方法的事务。
NESTED和REQUIRES_NEW都可以做到内部方法事务回滚而不影响外围方法事务。但是因为NESTED是嵌套事务,所以外围方法回滚之后,作为外围方法事务的子事务也会被回滚。而REQUIRES_NEW是通过开启新的事务实现的,内部事务和外围事务是两个事务,外围事务回滚不会影响内部事务。
数据库事务隔离级别
1.隔离级别
数据库事务的隔离级别有4个,由低到高依次为Read uncommitted 、Read committed 、Repeatable read 、Serializable ,这四个级别可以逐个解决脏读 、不可重复读 、幻读 这几类问题。
√: 可能出现 ×: 不会出现
| 隔离级别 | |||
|---|---|---|---|
| Read uncommitted | √ | √ | √ |
| Read committed | × | √ | √ |
| Repeatable read | × | × | √ |
| Serializable | × | × | × |
注意:我们讨论隔离级别的场景,主要是在多个事务并发 的情况下,因此,接下来的讲解都围绕事务并发。
Read uncommitted 读未提交
公司发工资了,领导把5000元打到singo的账号上,但是该事务并未提交,而singo正好去查看账户,发现工资已经到账,是5000元整,非常高 兴。可是不幸的是,领导发现发给singo的工资金额不对,是2000元,于是迅速回滚了事务,修改金额后,将事务提交,最后singo实际的工资只有 2000元,singo空欢喜一场。
出现上述情况,即我们所说的脏读 ,两个并发的事务,“事务A:领导给singo发工资”、“事务B:singo查询工资账户”,事务B读取了事务A尚未提交的数据。
当隔离级别设置为Read uncommitted 时,就可能出现脏读,如何避免脏读,请看下一个隔离级别。
Read committed 读提交
singo拿着工资卡去消费,系统读取到卡里确实有2000元,而此时她的老婆也正好在网上转账,把singo工资卡的2000元转到另一账户,并在 singo之前提交了事务,当singo扣款时,系统检查到singo的工资卡已经没有钱,扣款失败,singo十分纳闷,明明卡里有钱,为 何......
出现上述情况,即我们所说的不可重复读 ,两个并发的事务,“事务A:singo消费”、“事务B:singo的老婆网上转账”,事务A事先读取了数据,事务B紧接了更新了数据,并提交了事务,而事务A再次读取该数据时,数据已经发生了改变。
当隔离级别设置为Read committed 时,避免了脏读,但是可能会造成不可重复读。
大多数数据库的默认级别就是Read committed,比如Sql Server , Oracle。如何解决不可重复读这一问题,请看下一个隔离级别。
Repeatable read 重复读
当隔离级别设置为Repeatable read 时,可以避免不可重复读。当singo拿着工资卡去消费时,一旦系统开始读取工资卡信息(即事务开始),singo的老婆就不可能对该记录进行修改,也就是singo的老婆不能在此时转账。
虽然Repeatable read避免了不可重复读,但还有可能出现幻读 。
singo的老婆工作在银行部门,她时常通过银行内部系统查看singo的信用卡消费记录。有一天,她正在查询到singo当月信用卡的总消费金额 (select sum(amount) from transaction where month = 本月)为80元,而singo此时正好在外面胡吃海塞后在收银台买单,消费1000元,即新增了一条1000元的消费记录(insert transaction ... ),并提交了事务,随后singo的老婆将singo当月信用卡消费的明细打印到A4纸上,却发现消费总额为1080元,singo的老婆很诧异,以为出 现了幻觉,幻读就这样产生了。
注:Mysql的默认隔离级别就是Repeatable 2。
Serializable 序列化
Serializable 是最高的事务隔离级别,同时代价也花费最高,性能很低,一般很少使用,在该级别下,事务顺序执行,不仅可以避免脏读、不可重复读,还避免了幻像读。
2.脏读、幻读、不可重复读
1.脏读:
脏读就是指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。
2.不可重复读:
是指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的的数据可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。(即不能读到相同的数据内容)
例如,一个编辑人员两次读取同一文档,但在两次读取之间,作者重写了该文档。当编辑人员第二次读取文档时,文档已更改。原始读取不可重复。如果只有在作者全部完成编写后编辑人员才可以读取文档,则可以避免该问题。
3.幻读:
是指当事务不是独立执行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象
发生了幻觉一样。
例如,一个编辑人员更改作者提交的文档,但当生产部门将其更改内容合并到该文档的主复本时,发现作者已将未编辑的新材料添加到该文档中。如果在编辑人员和生产部门完成对原始文档的处理之前,任何人都不能将新材料添加到文档中,则可以避免该问题。
