(一) 創建 xml文件
引入 Aop beans tx 命名空間(注意不用引入 Context )
(二) 配置數據庫
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="1099026712"> </property>
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
</bean>
(三) 配置 jdbctemplate的bean屬性
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
(四)配置每個類的bean屬性,注意類之間的引用關係
<!-- 配置bean -->
<bean id="daoImpl" class="jdbc.tx.xml.DaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookshopImpl" class="jdbc.tx.xml.impl.BookshopImpl">
<property name="book" ref="daoImpl"></property>
</bean>
<bean id="bookshopListImpl" class="jdbc.tx.xml.impl.BookshopListImpl">
<property name="bk" ref="bookshopImpl"></property>
</bean>
(五) 配置 事務管理器,並開啓事務
在tx 中添加屬性信息
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
(六) 配置Aop 事務切入點信息
<!-- 配置事務切入點 -->
<aop:config >
<aop:pointcut expression="execution(* jdbc.tx.xml.impl.*.*(..))"
id="pointcut"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
</aop:config>
(七) 運行執行