欧洲外贸网站有哪些,未来做那个网站能致富,云服务器怎么建立网站,网站注册搜索引擎的目的在 Spring 中#xff0c;除了使用基于 XML 的方式可以实现声明式事务管理以外#xff0c;还可以通过 Annotation 注解的方式实现声明式事务管理。
使用 Annotation 的方式非常简单#xff0c;只需要在项目中做两件事#xff0c;具体如下。 1 在 Spring 容器中注册驱动除了使用基于 XML 的方式可以实现声明式事务管理以外还可以通过 Annotation 注解的方式实现声明式事务管理。
使用 Annotation 的方式非常简单只需要在项目中做两件事具体如下。 1 在 Spring 容器中注册驱动代码如下所示
tx:annotation-driven transaction-managertxManager/2 在需要使用事务的业务类或者方法中添加注解 Transactional并配置 Transactional 的参数。关于 Transactional 的参数如下图所示。
下面通过修改Spring声明式事务管理点击跳转至Spring声明式事务管理中银行转账的案例讲解如何使用 Annotation 注解的方式实现 Spring 声明式事务管理。
1. 注册驱动 修改 Spring 配置文件 applicationContext.xml修改后如下所示。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd!-- 加载properties文件 --context:property-placeholder locationclasspath:c3p0-db.properties /!-- 配置数据源读取properties文件信息 --bean iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.driverClass} /property namejdbcUrl value${jdbc.jdbcUrl} /property nameuser value${jdbc.user} /property namepassword value${jdbc.password} //bean!-- 配置jdbc模板 --bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdataSource //bean!-- 配置dao --bean idaccountDao classcom.mengma.dao.impl.AccountDaoImplproperty namejdbcTemplate refjdbcTemplate //bean!-- 配置service --bean idaccountService classcom.mengma.service.impl.AccountServiceImplproperty nameaccountDao refaccountDao //bean!-- 事务管理器依赖于数据源 --bean idtxManagerclassorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource //bean!-- 注册事务管理驱动 --tx:annotation-driven transaction-managertxManager/
/beans上述代码中可以看出与原来的配置文件相比这里只修改了事务管理器部分新添加并注册了事务管理器的驱动。
注意在学习 AOP 注解方式开发时需要在配置文件中开启注解处理器指定扫描哪些包下的注解这里没有开启注解处理器是因为在第 3335 行手动配置了 AccountServiceImpl而 Transactional 注解就配置在该类中所以会直接生效。
2. 添加 Transactional 注解 修改 AccountServiceImpl在文件中添加 Transactional 注解及参数添加后如下所示。
package com.mengma.service.impl;import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.mengma.dao.AccountDao;Transactional(propagation Propagation.REQUIRED, isolation Isolation.DEFAULT, readOnly false)
public class AccountServiceImpl {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao accountDao;}public void transfer(String outUser, String inUser, int money) {this.accountDao.out(outUser, money);// 模拟断电int i 1 / 0;this.accountDao.in(inUser, money);}
}注意在使用 Transactional 注解时参数之间用“”进行分隔。
使用 JUnit 测试再次运行 test() 方法时控制台同样会输出如下图所示的异常信息这说明使用基于 Annotation 注解的方式同样实现了 Spring 的声明式事务管理。如果注释掉模拟断电的代码进行测试则转账操作可以正常完成。