spring的事務處理分為兩種:
1、編程式事務:在程序中控制事務開始,執行和提交;(不建議使用,所以這裡我就不說明太多)
2、聲明式事務:在Spring配置文件中對事務進行配置,無須在程序中寫代碼;(建議使用) 我對”聲明式“的理解是這樣的:Spring配置文件中定義好了這樣一個規則, 這個規則可以指定對哪些類的哪些方法在執行的時候添加事務控制,並配置好了事務的相關執行屬性, 就是在這些類的這些方法執行的時候隱式地添加事務開始、執行、提交或回滾的代碼(當然我們看不到) 聲明式事務又分了兩種寫法: 2.1、xml配置文件 2.2、注解 下面我們首先講解下在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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 類似於財務部門一樣,類就是錢,所有需要類的實例都由srping去管理 -->
<!-- <context:component-scan>:
有一個use-default-filters屬性,該屬性默認為true,
這就意味著會掃描指定包下的全部的標有注解的類,並注冊成bean.
可以發現這種掃描的粒度有點太大,如果你只想掃描指定包下面的Controller,
該怎麼辦?此時子標簽<context:incluce-filter>就起到了勇武之地。如下所示
<context:component-scan base-package="news" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
如果use-dafault-filters在上面並沒有指定,默認就為true,
也就意味著你現在加<context:exclude-filter/>跟沒加是一樣的
所有你要記住,你若想要用到<context:component-scan>的子標簽,
必須要把use-dafault-filters的值改為false
當然還有一個是與之相反的而已這裡就不啰嗦了
上面這一對解釋換成一句話就是:
Use-dafault-filters=”false”的情況下:<context:exclude-filter>指定的不掃描,<context:include-filter>指定的掃描
<context:component-scan>的base-package屬性作用:設置要被掃描的包 -->
<!-- (本案例不用到,只是用了一個全盤掃描,以上內容只是為了讓大家了解它) -->
<context:component-scan base-package="news.."/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 每300秒檢查所有連接池中的空閒連接 -->
<property name="idleConnectionTestPeriod" value="300"/>
<!-- 最大空閒時間,900秒內未使用則連接被丟棄。若為0則永不丟棄 -->
<property name="maxIdleTime" value="900"/>
<!-- 最大連接數 -->
<property name="maxPoolSize" value="2"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>news/entity/News.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 創建事務管理器, 管理sessionFactory(因為所有的session都是從sessionFactory獲取的) -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置通知, 那些方法需要切入什麼類型的事務 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面表達式, 並且讓 tx與切面表達式合二為一 -->
<aop:config>
<!-- 表達式, 定義哪個包的哪些類需要切入事務,但是此處並且沒有制定類中哪些方法,需要切入什麼樣 事務 -->
<aop:pointcut expression="execution(* news.service.*.*(..))" id="pointcut" />
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
相信這段代碼對於我們初學者來說不是那麼容易看懂,下面我來給大家分析吧:
1、創建事務管理器

2、配置通知

Exception(s);以逗號分開。 如:'news.serviceImpl'
no-rollback-for
不是
不 被觸發進行回滾的 Exception(s);以逗號分開。 如:'news.serviceImpl'
3、配置切面表達式, 並且讓 tx與切面表達式合二為一 
我解釋一下execution(* news.service.*.*(..))"中幾個通配符的含義: 第一個 * —— 通配 任意返回值類型 第二個 * —— 通配 包news.service下的任意class 第三個 * —— 通配 包news.service下的任意class的任意方法 第四個 .. —— 通配 方法可以有0個或多個參數 綜上:包news.service下的任意class的具有任意返回值類型、任意數目參數和任意名稱的方法
做到這裡xml方式的事務管理基本就做完了,還差最後一步,那就是導入它相關的缺包

如果沒有這些包的朋友們可以去一下網址下載:http://pan.baidu.com/s/1eSlyY1G
注意:在以下情況中spring的事務管理會失效: ● private 方法無法添加事務管理. ● final 方法無法添加事務管理. ● static 方法無法添加事務管理. ● 當繞過代理對象, 直接調用添加事務管理的方法時, 事務管理將無法生效.