SpringMVC 第二十章 基于 XML 的声明式事务控制


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="$&#123;jdbc.driver&#125;"/>
    <property name="jdbcUrl" value="$&#123;jdbc.url&#125;"/>
    <property name="user" value="$&#123;jdbc.username&#125;"/>
    <property name="password" value="$&#123;jdbc.password&#125;"/>
</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 &#123;
    /**
     * 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);
&#125;

com.yourname.dao.impl.AccountDaoImpl

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
public class AccountDaoImpl implements AccountDao &#123;
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) &#123;
        this.jdbcTemplate = jdbcTemplate;
    &#125;
    @Override
    public void out(String outMan, BigDecimal money) &#123;
        jdbcTemplate.update("update account set money = money - ? where name = ?", money, outMan);
    &#125;
    @Override
    public void in(String inMan, BigDecimal money) &#123;
        jdbcTemplate.update("update account set money = money + ? where name = ?", money, inMan);
    &#125;
&#125;

com.yourname.service.AccountService

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
public interface AccountService &#123;
    /**
     * Transfer
     *
     * @param outMan inMan
     * @param inMan outMan
     * @param money money
     */
    void transfer(String outMan, String inMan, BigDecimal money);
&#125;

com.yourname.service.impl.AccountServiceImpl

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
public class AccountServiceImpl implements AccountService &#123;
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) &#123;
        this.accountDao = accountDao;
    &#125;
    @Override
    public void transfer(String outMan, String inMan, BigDecimal money) &#123;
        accountDao.out(outMan, money);
        accountDao.in(inMan, money);
    &#125;
&#125;

com.yourname.controller.AccountController

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
public class AccountController &#123;
    public static void main(String[] args) &#123;
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = app.getBean(AccountService.class);
        accountService.transfer("Tom", "Lucy", BigDecimal.valueOf(100));
    &#125;
&#125;

4. 知识要点

XML 声明式事务控制配置要点

  • 平台事务管理器的配置
  • 事务通知的配置
  • 事务 AOP 织入的配置


文章作者: gregPerlinLi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 gregPerlinLi !
  目录