1,复制项目spring404 ,改名spring404-3。修改BankServiceImpl类,添加注解,
package com.cruise.service.impl;
import org.springframework.transaction.annotation.Transactional;
import com.cruise.dao.BankDao;
import com.cruise.service.BankService;
@Transactional
public class BankServiceImpl implements BankService{
private BankDao bankDao;
public void setBankDao(BankDao bankDao) {
this.bankDao = bankDao;
}
@Override
public void transferAccount(int money, int userA, int userB) {
bankDao.OutMoney(money, userA);
bankDao.InMoney(money, userB);
}
}
2,修改beans.xml文件,删除配置事务通知,删除配置事务切面。添加注解驱动。
<?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:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--jdbc事物管理器-->
<bean id="transactionManager1"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager1"/>
<bean id="namedParameterJdbcTemplate1"class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource1"></constructor-arg>
</bean>
<bean id="bankDao1" class="com.cruise.dao.impl.BankDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate1"></property>
</bean>
<bean id="bankService" class="com.cruise.service.impl.BankServiceImpl">
<property name="bankDao" ref="bankDao1"></property>
</bean>
</beans>
3,测试-运行
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.service.BankService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=new ClassPathXmlApplicationContext("beans.xml");
BankService bankService = (BankService) CPXAC.getBean("bankService");
bankService.transferAccount(120, 1, 2);
}
}
第十二讲:12,spring声明式事务管理-注解式
猜你喜欢
转载自blog.csdn.net/u010393325/article/details/83747614
今日推荐
周排行