SpringMVC —— 第二十章 基于 XML 的声明式事务控制
1. 什么是声明式事务控制
Spring 的声明式事务控制顾名思义就是 采用声明的方式来处理事务 。这里所说的声明,就是在配置文件中声明,用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。
声明式事务处理的作用:
- 事务管理不侵入开发的组件。具体来说,业务逻辑对象就不会意识到正在事务管理之中,事实上也应该如此,因为事务的管理是属于系统层面的服务,而不是业务逻辑的一部分,如果想该改变事务管理策划的话,也只需要在定义文件中重新配置即可
- 在不需要事务管理的时候,只要在设定文件上修改一下,即可移去事务管理服务,无需改变代码重新编译,这样维护起来极其方便
注意⚠️:Spring 声明式事务控制的底层就是 AOP!
2. 声明式事务控制的实现
声明式事务控制明确事项:
- 谁是切点?
- 谁是通知?
- 配置切面?
3. 切点方法的事务参数的配置
<!-- Advice, Transaction enhance -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- Set the attribute information of the transaction -->
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" read-only="false" />
</tx:attributes>
</tx:advice>
其中,<tx:method>
代表切点方法的事务参数的配置,例如:
<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" read-only="false" />
name
:切点方法的名称isolation
:事务的隔离行为propogation
:事务的传播行为timeout
:超时时间read-only
:是否只读
示例代码:
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
applicationContet.xml
<bean id="accountDao" class="com.gregperlinli.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="accountService" class="com.gregperlinli.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Target object, The internal method is the pointcut -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Configure platform transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Advice, Transaction enhance -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- Set the attribute information of the transaction -->
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- Configure AOP weaving of transactions -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.gregperlinli.service.impl.*.*(..))" />
</aop:config>
com.yourname.dao.AccountDao
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public interface AccountDao {
/**
* out
*
* @param outMan outMan
* @param money money
*/
void out(String outMan, BigDecimal money);
/**
* in
*
* @param inMan inMan
* @param money money
*/
void in(String inMan, BigDecimal money);
}
com.yourname.dao.impl.AccountDaoImpl
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void out(String outMan, BigDecimal money) {
jdbcTemplate.update("update account set money = money - ? where name = ?", money, outMan);
}
@Override
public void in(String inMan, BigDecimal money) {
jdbcTemplate.update("update account set money = money + ? where name = ?", money, inMan);
}
}
com.yourname.service.AccountService
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public interface AccountService {
/**
* Transfer
*
* @param outMan inMan
* @param inMan outMan
* @param money money
*/
void transfer(String outMan, String inMan, BigDecimal money);
}
com.yourname.service.impl.AccountServiceImpl
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outMan, String inMan, BigDecimal money) {
accountDao.out(outMan, money);
accountDao.in(inMan, money);
}
}
com.yourname.controller.AccountController
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
accountService.transfer("Tom", "Lucy", BigDecimal.valueOf(100));
}
}
4. 知识要点
XML 声明式事务控制配置要点
- 平台事务管理器的配置
- 事务通知的配置
- 事务 AOP 织入的配置