上一個文章咱們瞭解了什麼事aop,以及aop的使用方法,主要是把本身想要加入的通知(advice)加入到咱們的方法裏,
好比上一章咱們說的事把myadvice類中的before方法織入到userservice類中的savedata方法裏,由於用的是before,根據applicationContext.xml配置,before方法用的<aop:before … />,因此在執行userservice方法時會調用before方法。
下面這一章主要是講解一下aop和tx聯合使用的兩種方法
所涉及到知識點:java
相比你們都知道如今企業中的項目基本上爲了更換數據庫方便,都使用了一個properties文件寫數據庫的信息,
那麼須要用到什麼東西呢?
首先須要Spring的context的約束導入
在配置文件中加入如下配置mysql
<context:property-placeholder location="classpath:db.properties"/>
以上代碼系統就會掃描到db.properties,而後根據鍵值對配置。web
如下是db.properties配置 jdbc.jdbcUrl=jdbc:mysql:///springtest jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=xiaofeng jdbc.password=xiaofeng
配置文件配置(applicationContext.xml)須要用到c3p0中的鏈接池combo---com.mchange.v2.c3p0.ComboPooledDataSource <context:property-placeholder location="classpath:db.properties"/> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
上面說的dataSource是用與鏈接數據庫的鏈接池,可是其中還須要jdbc模板去管理他,所以咱們須要在容器中聲明jdbc模板,並將dataSource加入到jdbc模板中。由於jdbc模板中含有數據庫中的增刪改查的方法,咱們須要調用它。spring
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
以後咱們若是在Dao層中調用jdbcTemplate中的方法,那麼咱們須要在Dao添加屬性jdbcTemplate。
好比sql
<bean name="userDao" class="cn.tsu.junittx.UserDaoImpl"> <property name="jt" ref="jdbcTemplate"></property> </bean>
此時咱們就能夠在Dao層聲明jdbcTemplate(注意記得加入set方法)調用數據庫的方法
可是此時就有一個問題,那就是事務的問題,一旦語法錯誤,形成損失,好比銀行轉帳,下面咱們就聯合事務再一塊兒說一下。數據庫
若是要把Spring中的事務加入進來的話,咱們就應該先了解一下事務的底層調用。它須要什麼支持express
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
而後再注入transactionTemplate事務模板對象,依賴注入transactionManagerapp
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"></property> </bean>
此時Spring有兩種方法進行事務管理
方法一(須要配置的多,比較麻煩)
把transactionTemplate給咱們的事務使用<tx:advice transaction-manager=「transactionManager」>svg
<aop:config> <aop:pointcut expression="execution(* cn.tsu.junittx.*Service.*(..))" id="txpc"/> <aop:advisor advice-ref="myAdvice" pointcut-ref="txpc"/> </aop:config> <tx:advice transaction-manager="transactionManager" id="myAdvice"> <tx:attributes> <tx:method name="update*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/> <tx:method name="transf" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
以上的大體意思爲 :把id名的myAdvice 的通知(此通知已寫好,用的是事務管理對象)織入到id爲txpc的類中,類中的方法須要與name=「 」一致或者表達式一致。學習
方法二
配置文件中直接輸入
<tx:annotation-driven/>
而後再去咱們須要添加事務的類名上面(方法一中用的是匹配表達式查詢該類方法)添加註解,也能夠在方法上添加註解
import javax.annotation.Resource; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; / * 與配置文件中的<tx:annotation-driven/>聯合使用*/ @Transactional(isolation=Isolation.REPEATABLE_READ,readOnly=false,propagation=Propagation.REQUIRED) public class UserService { @Resource(name="userDao") private UserDaoImpl ud ; public void transf(int from ,int to , double money) { //減錢 ud.decreasemoney(from, money); int i = 1 / 0; //加錢 ud.increasemoney(to, money); } }
下面把源代碼也發給你們,供新人蔘考學習
包名說明
學習下載連接:,也不貴,就1分。但願你們多多支持!!!