博客
关于我
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 install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>