基于XML和注解的声明式事务控制

本文以账户转账的demo演示spring分别基于xml和注解的声明式事务控制。讲述声明式事务控制配置步骤。

1. 基于XML的声明式事务控制

1.1 环境搭建

  • 1.1 导入必要的jar包坐标
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  • 1.2 创建数据库test和账户实体类account表,包含id,name,money3个字段
CREATE databbase test;

USE DATABASE;

CREATE TABLE account (
	id INT NOT NULL AUTO_INCREMENT,
	NAME VARCHAR (20),
	money DOUBLE,
	PRIMARY KEY (id)
) CHARACTER
SET utf8 COLLATE utf8_general_ci;

INSERT INTO `account` VALUES ('1', '叶子', '100');
INSERT INTO `account` VALUES ('2', '菜鸟', '100');
public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double money;
    //此处省略get,set以及toString方法
}
  • 1.3 编写dao接口和实现类,定义查询和更新方法
public interface IAccountDao {
    //根据Id查询账户
    Account findAccountById(Integer accountId);

    //更新账户
    void updateAccount(Account account);
}

持久层实现类中使用JdbcTemplate进行操作

public class AccountDaoImpl implements IAccountDao {
    //注入JdbcTemplate
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = jdbcTemplate.query("select * from account where id=?",
                new BeanPropertyRowMapper<Account>(Account.class), accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where id = ?",account.getMoney(),account.getId());
    }
}
  • 1.4 编写业务层接口和实现类,定义转账方法transfer(),方法中三个参数,分别代表转出账户,转入账户和转账金额。
public interface IAccountService {

    Account findAccountById(Integer accountId);

    void transfer(Integer sourceId,Integer targetId,Double money);
}
public class AccountServiceImpl implements IAccountService{

    private IAccountDao accountDao;
    public void setAccountDao(IAccountDao accountDao){
        this.accountDao = accountDao;
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void transfer(Integer sourceId, Integer targetId, Double money) {
        //根据id查询两个账户
        Account source = accountDao.findAccountById(sourceId);
        Account target = accountDao.findAccountById(targetId);
        if(source!=null&&target!=null){
            //修改两个账户的金额
            if(source.getMoney()<money){
                System.out.println("余额不足");
            }else{
                source.setMoney(source.getMoney() - money);
                target.setMoney(target.getMoney() + money);
                //更新账户
                accountDao.updateAccount(source);
                int i = 1/0;//模拟异常
                accountDao.updateAccount(target);
            }
        }else{
            System.out.println("没有找到账户!");
        }
    }
}
  • 1.5 创建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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置service-->
    <bean id="accountService" class="com.wink.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--配置dao-->
    <bean id="accountDao" class="com.wink.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!--配置JdbcTemplate数据源-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///test"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
</bean>

1.2 事务控制配置步骤

  • 完成以上的基本环境搭建之后,可以对事物控制进行配置,通常事务控制配置有这几步:
    1)配置事务管理器; 2)配置事务的通知;3)配置事务的属性;4)配置AOP;
  • 配置事务的通知,此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的,使用tx:advice标签配置事务通知
    属性:
      id:给事务通知起一个唯一标识
      transaction-manager:给事务通知提供一个事务管理器引用
  • 配置事务的属性,是在事务的通知tx:advice标签的内部,有以下属性:
属性描述默认值及描述
isolation用于指定事务的隔离级别DEFAULT,表示使用数据库的默认隔离级别
propagation用于指定事务的传播行为REQUIRED,表示一定会有事务,增删改的选择,查询方法可以选择SUPPORTS
read-only用于指定事务是否只读,只有查询方法才能设置为truefalse,表示读写
timeout用于指定事务的超时时间-1,表示永不超时。如果指定了数值,以秒为单位
rollback-for用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚没有默认值,表示任何异常都回滚
no-rollback-for用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚没有默认值,表示任何异常都回滚

下面是在bean.xml中的事物配置:

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<!--配置事务的属性-->
        <tx:attributes>
            <tx:method name="*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP-->
    <aop:config>
        <!--配置AOP中的通用切入点表达式-->
        <aop:pointcut id="pt" expression="execution(* com.wink.service.impl.*.*(..))"/>
        <!--配置事务通知和切入点表达式的对应关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

最后进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class testTX {
    @Autowired
    private IAccountService as;

    @Test
    public void testTransfer(){
        as.transfer(1,2,100.0);
        System.out.println(as.findAccountById(1));
        System.out.println(as.findAccountById(2));
    }
}

这时候,你会以为运行结果会是账户1的余额为0,账户2的余额为200。并没有,因为在业务层实现类AccountServiceImpl中的转账方法里定义了一个by zero异常,int i = 1/0;所以此时每个账户余额不变,说明事物控制成功了
在这里插入图片描述
将int i = 1/0;注掉,再次运行,发现转账成功
在这里插入图片描述

2. 基于注解的声明式事务控制

这里根据上述基于xml的配置进行修改。

  • 首先使用注解配置持久层和业务层实现类,以及数据的注入
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
}
/*----------------------------------------------------*/

@Service("accountService")
public class AccountServiceImpl implements IAccountService{
    @Autowired
    private IAccountDao accountDao;
}
  • 然后在业务层使用@Transactional 注解对事物进行配置
@Service("accountService")
//只读型事务的配置
@Transactional(propagation= Propagation.SUPPORTS,readOnly=true)
public class AccountServiceImpl implements IAccountService{

    //需要的是读写型事务配置
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false)
    public void transfer(Integer sourceId, Integer targetId, Double money) {

	}
}
  • 在配置文件中配置要扫描的包,和开启 spring 对注解事务的支持
    此时配置文件中还有以下配置:
    <!-- 配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="com.wink"/>

    <!--配置JdbcTemplate数据源-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///test"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 开启spring对注解事务的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

3. 纯注解事务的支持

根据上述基于注解的配置进一步修改,把bean.xml中的配置一步步转为注解配置。
首先创建数据库配置文件jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=root

然后创建JdbcConfig类,对数据库进行配置

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    //创建JdbcTemplate
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    //创建数据源对象
    @Bean(name = "dataSource")
    public DataSource createDataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}

接着创建事务相关的配置类,配置事务管理器

public class TransactionConfig {
    @Bean("transactionManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

最后创建spring配置类取代bean.xml

@Configuration
@ComponentScan("com.wink")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("classpath:jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}

总结:已上介绍了spring基于xml、基于注解、以及纯注解的声明式事务控制。配置事务的步骤:(1)配置事务管理器; (2)配置事务的通知;(3)配置事务的属性;(4)配置AOP中的通用切入点表达式(5)配置事务通知和切入点表达式的对应关系.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值