除了可以使用注解的方式配置事务,我们还可以通过XML文件来配置事务。
事前准备
创建接口
public interface BookShopDAO {
public int findBookPriceByISBN(int ISBN);
public void updateBookStock(int ISBN);
public void updateUserAccount(String username, int price);
}
public interface BookShopService {
public void purchase(String username, int ISBN);
}
public interface Cashier {
public void checkout(String username, List<Integer> isbns);
}
实现类
public class BookShopDAOImpl implements BookShopDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public int findBookPriceByISBN(int ISBN) {
String sql = "select price from book where isbn = ? ";
return jdbcTemplate.queryForObject(sql, Integer.class, ISBN);
}
@Override
public void updateBookStock(int ISBN) {
String sql = "update book_stock set stock = stock - 1 where isbn = ? ";
jdbcTemplate.update(sql, ISBN);
}
@Override
public void updateUserAccount(String username, int price) {
String sql = "update account set balance = balance - ? where username = ? ";
jdbcTemplate.update(sql, price, username);
}
}
public class BookShopServiceImpl implements BookShopService {
private BookShopDAO bookShopDAO;
public void setBookShopDAO(BookShopDAO bookShopDAO) {
this.bookShopDAO = bookShopDAO;
}
@Override
public void purchase(String username, int ISBN) {
int price = bookShopDAO.findBookPriceByISBN(ISBN);
bookShopDAO.updateBookStock(ISBN);
bookShopDAO.updateUserAccount("AA", price);
}
}
public class CashierImpl implements Cashier {
private BookShopService bookShopService;
public void setBookShopService(BookShopService bookShopService) {
this.bookShopService = bookShopService;
}
@Override
public void checkout(String username, List<Integer> isbns) {
for(Integer isbn: isbns){
bookShopService.purchase(username, isbn);
}
}
}
配置文件
首先在Spring的配置为文件中配置事务管理需要用到的类
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="bookShopDAO" class="com.spring.xml.BookShopDAOImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookShopService" class="com.spring.xml.BookShopServiceImpl">
<property name="bookShopDAO" ref="bookShopDAO"></property>
</bean>
<bean id="cashier" class="com.spring.xml.CashierImpl">
<property name="bookShopService" ref="bookShopService"></property>
</bean>
配置事务管理器
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
配置事务属性
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
配置事务切入点,以及把事务切入点和事务属性关联起来
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.spring.xml.BookShopDAO.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
最后是测试类
public class TransactionTest {
private ApplicationContext ctx = null;
private Cashier cashier = null;
{
ctx = new ClassPathXmlApplicationContext("application-context-tx.xml");
cashier = ctx.getBean(Cashier.class);
}
@Test
public void test(){
cashier.checkout("AA", Arrays.asList(1001));
}
}
网友评论