博客
关于我
Spring中基于XML的声明式事务控制
阅读量:686 次
发布时间:2019-03-17

本文共 2869 字,大约阅读时间需要 9 分钟。

Spring中基于XML的声明式事务控制配置详细指南

在Spring框架中,基于XML 的声明式事务控制是一个体现Spring强大配置能力的重要特点。以下将详细介绍如何在Spring应用中配置基于XML的声明式事务控制。

1. 配置事务管理器

首先,需要在项目的Spring配置文件中添加一个配置bean,用于创建事务管理器。以下是一个典型的配置示例:

这里,DataSourceTransactionManager需要一个数据源作为决策器。数据源通常使用spring:jdbc中的DriverManagerDataSource配置。

2. 配置事务的通知

接下来,需要定义一个通用的事务通知,其作用是捕捉方法前后进行事务开始和提交、回滚操作。可以参考以下配置:

此外,还可以根据具体需求添加事务的隔离级别、超时设置等属性:

3. 配置切入点表达式

在Spring AOP中,需要定义一个通用的切入点表达式,以便为特定的方法或类方法创建事务通知。例如:

这里,execution(* com.qublog.service.impl.*.*(..)) 定义了一个切入点,表示所有在com.qublog.service.impl包下定义的方法都会被Spring的AOP拦截并应用事务通知。

4. 业务逻辑类与事务集成

在业务逻辑实现类中,通常会在需要事务支持的方法上使用事务注解。例如:

@Repositorypublic class AccountServiceImpl implements AccountService {    // 用@Autowired注入数据源    private DataSource dataSource;    // 已经注入的事务管理器    private TransactionManager transactionManager;    public void transfer(String sourceName, String targetName, Float money) throws Exception {        System.out.println("开始转账...");                // 获取转出账户        Account sourceAccount = findAccountByName(sourceName);        // 获取转入账户        Account targetAccount = findAccountByName(targetName);        // 修改转出账户金额        sourceAccount.setMoney(sourceAccount.getMoney() - money);        // 修改转入账户金额        targetAccount.setMoney(targetAccount.getMoney() + money);        // 提交事务        transactionManager.beginTransaction();                try {            AccountDaoume updateAccount(sourceAccount);            System.out.println("更新转出账户成功...");                        AccountDaoume updateAccount(targetAccount);            System.out.println("更新转入账户成功...");        } catch (Exception e) {            System.out.println("事务回滚...");            transactionManager.rollback();            throw e;        } finally {            transactionManagercommit();        }    }}

5. 数据源配置

确保在bean.xml中已正确配置数据源。以下是一个常用的配置示例:

6. 完整示例配置(bean.xml

以下是一个简化的bean.xml示例,包含了上述配置:

7. 测试类

测试类可以使用Spring Boot测试框架进行单元测试。例如:

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest@ContextConfiguration(locations = "classpath:bean.xml")public class AccountServiceTest {    @Autowired    private AccountService accountService;    @Autowired    private ApplicationContext applicationContext;    @Test    public void testTransfer() {        accountService.transfer("aaa", "bbb", 100f);        System.out.println("测试通过!");    }    @Test    public void testFindByname() {        Account account = accountService.findAccountById(1);        System.out.println(account);    }}

以上配置和相关代码,便利于在Spring应用中实现基于XML的声明式事务控制。

转载地址:http://jfchz.baihongyu.com/

你可能感兴趣的文章
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>