AccountServiceImpl
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public AccountDao getAccountDao() {
return accountDao;
}
//在AccountServiceimpl中注入AccountDao,要提供set方法
//配置中有<property name="accountDao" ref="accountDao"></property>
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void account(String outName, String inName, double money) {
// TODO Auto-generated method stub
accountDao.accountOut(outName, money);
//设置异常
int i = 10/0;
accountDao.accountIn(inName, money);
}
}
AccountDao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
//AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate
//所以这里选择用this.getJdbcTemplate来获取JdbcTemplate
@Override
public void accountIn(String inName, double money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set money = money+? where name = ?",money,inName);
}
@Override
public void accountOut(String outName, double money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set money = money-? where name = ?",money,outName);
}
}
Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AccountTest {
@Autowired//(将applicationContext中的accountService注入)
private AccountService accountService;
@Test
public void test1() {
accountService.account("tom", "fox", 500);
}
}
applicationContext.xml
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///springtest"/>
<property name="user" value="root"/>
<property name="password" value="root"></property>
</bean>
<!--(配置AccountServiceImpl) -->
<bean id="accountService" class="service.AccountServiceImpl">
<!-- 注入dao,此时需要在AccountServiceImplements中为accountDao创建set方法 -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置AccountDaoImpl -->
<bean id="accountDao" class="dao.AccountDaoImpl">
<!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate -->
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name:必须的,对哪些方法进行事务控制
isolation 可选 设置事务隔离级别 默认是DEFAULT
propagation:可选 设置事务传播 默认值 REQUIRED
timeout 可选 超时时间 默认值-1
read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。
rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚
no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚
-->
<tx:method name="account"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* service.AccountService.account(..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 配置采用注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate,这里便不用再配置 -->
<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0dataSource"/>
</bean> -->
问题:关于xml方式与annotation方式进行事务管理的优缺点?
1.从简单上来说,使用注解更方便。
2.使用配置的方案,可以对事务配置进行集中维护:
<tx:method name="get*" />
<tx:method name="save*" />