所有的java类都是用的上一篇文章:Spring初学之spring的事务管理
不同的是,这时xml配置事务,所以就要把java类中的那些关于spring的注解都删掉,然后在xml中配置,ApplicationContext.xml如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 11 12 <!-- 导入资源文件 --> 13 <context:property-placeholder location="classpath:jdbc.properties"/> 14 15 <!-- 配置c3p0数据源 --> 16 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="user" value="${user}"></property> 18 <property name="password" value="${password}"></property> 19 <property name="driverClass" value="${driverClass}"></property> 20 <property name="jdbcUrl" value="${jdbcUrl}"></property> 21 22 <property name="initialPoolSize" value="${initPoolSize}"></property> 23 <property name="maxPoolSize" value="${maxPoolSize}"></property> 24 </bean> 25 26 <!-- 配置spring 的JdbcTemplate --> 27 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 28 <property name="dataSource" ref="dataSource"></property> 29 </bean> 30 31 <bean id="bookShopDao" class="spring.tx.xml.BookShopDaoImp"> 32 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 33 </bean> 34 35 <bean id="bookShopService" class="spring.tx.xml.BookShopServiceImpl"> 36 <property name="bookShopDao" ref="bookShopDao"></property> 37 </bean> 38 39 <bean id="cashier" class="spring.tx.xml.CashierImpl"> 40 <property name="bookShopService" ref="bookShopService"></property> 41 </bean> 42 43 <!-- 配置事务管理器 --> 44 <bean id="transactionManager" 45 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 46 <property name="dataSource" ref="dataSource"></property> 47 </bean> 48 49 <!-- 配置事务属性 --> 50 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 51 <tx:attributes> 52 <tx:method name="*" /> 53 </tx:attributes> 54 </tx:advice> 55 56 <!-- 配置事务切入点,并把事务切入点与事务属性关联起来 --> 57 <aop:config> 58 <aop:pointcut expression="execution(* spring.tx.xml.BookShopService.*(..))" id="txPointcut"/> 59 60 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> 61 </aop:config> 62 63 </beans>