posts - 82, comments - 269, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          本文章比較了Spring自己帶的例子:JPetStore ,通過配置文件詳細(xì)講解了,Spring1.2.8與2.0如何實(shí)現(xiàn)聲明式事務(wù)管理。


          Spring1.2.8


          Spring以前對(duì)一個(gè)事務(wù)攔截要通過代理實(shí)現(xiàn)下面的配置文件是從不同的文件中找來的,不是單獨(dú)的一個(gè)Spring配置文件。


          ?<!-- Transaction manager for a single JDBC DataSource -->
          ?<!-- 聲明一個(gè)事務(wù)管理器 -->
          ?<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          ??<property name="dataSource" ref="dataSource"/>
          ?</bean>


          ?<!-- 聲明一個(gè)抽象Bean,這個(gè)Bean是不能實(shí)例化的,提供給其它需要AOP事務(wù)的Bean用,其它需要AOP事務(wù)的只要繼承這個(gè)Bean就會(huì)被AOP接管-->
          ?<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
          ??? abstract="true">
          ??<property name="transactionManager" ref="transactionManager"/>
          ??<property name="transactionAttributes">
          ???<props>
          ????<prop key="insert*">PROPAGATION_REQUIRED</prop>
          ????<prop key="update*">PROPAGATION_REQUIRED</prop>
          ????<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          ???</props>
          ??</property>
          ?</bean>

          ?<!-- 繼承之前實(shí)現(xiàn)的抽象Bean,讓這個(gè)Bean通過代理工廠生成,交給AOP托管。至于哪些方法被接管在控制Bean中已經(jīng)配置了-->
          ?<bean id="petStore" parent="baseTransactionProxy">
          ??<property name="target">
          ???<bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
          ????<property name="accountDao" ref="accountDao"/>
          ????<property name="categoryDao" ref="categoryDao"/>
          ????<property name="productDao" ref="productDao"/>
          ????<property name="itemDao" ref="itemDao"/>
          ????<property name="orderDao" ref="orderDao"/>
          ???</bean>
          ??</property>
          ??<!-- Uncomment the following in order to enable mail sending aspect -->
          ??<!--
          ??<property name="postInterceptors">
          ???<list>
          ????<ref bean="emailAdvisor"/>
          ???</list>
          ??</property>
          ???-->
          ?</bean>


          最早發(fā)表于 http://www.openj.cn

          Spring2.0?

          ?下面的配置與上面的配置完全對(duì)應(yīng)

          ?<!--這一個(gè)Bean的配置與之前完全一樣,沒有變化---->
          ?<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          ??<property name="dataSource" ref="dataSource"/>
          ?</bean>

          ?<!--這一處與之前有了變化,在1.2.8版本中,此處的Bean被聲明為由一個(gè)FactoryBean生成,而此處只是一個(gè)普通的Bean,要簡(jiǎn)單許多,透明性要好很多---->
          ?<bean id="petStore" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
          ??<property name="accountDao" ref="accountDao"/>
          ??<property name="categoryDao" ref="categoryDao"/>
          ??<property name="productDao" ref="productDao"/>
          ??<property name="itemDao" ref="itemDao"/>
          ??<property name="orderDao" ref="orderDao"/>
          ?</bean>


          ?<!---下面的兩處配置,才是整個(gè)事務(wù)AOP的核心,在1.2.8版本中,通過FactoryBean把事務(wù)對(duì)象(dataSource),與需要進(jìn)行事務(wù)控制的對(duì)象PetStoreImpl串起來,對(duì)PetStoreImpl有侵入性----->
          ?<!---而在之前的兩處配置中,事務(wù)對(duì)象(dataSource)與,需要進(jìn)行事務(wù)控制的對(duì)象PetStoreImpl沒有什么關(guān)系,它們的關(guān)系全部體現(xiàn)在下面的兩處配置中----->
          ?
          ?
          ?<!---pointcut屬性定義了哪此點(diǎn)需要去攔截,此處的配置的意思是所有的PetStoreFacade接口中的方法都要攔截,而攔截之后要如何處理則由advice-ref指定的Bean處理----->
          ?<!---配置文件中各個(gè)屬性的含義參考:http://www.redsaga.com/spring_ref/2.0/html/aop.html#aop-schema ----->

          ?<aop:config>
          ??<aop:advisor pointcut="execution(* *..PetStoreFacade.*(..))" advice-ref="txAdvice"/>?
          ?</aop:config>


          ?
          ?<!--下面的transaction-manager屬性原配置中沒有,如果缺少此配置,默認(rèn)值就是“transactionManager”在此加上,讓人看的更明白。-->
          ?<!-- 參考 http://blog.xmnow.cn/doc/cn/spring2.0-reference_final_zh_cn/ch09s05.html --->
          ?<tx:advice id="txAdvice"? transaction-manager="transactionManager">
          ??<tx:attributes>
          ???<tx:method name="insert*"/>
          ???<tx:method name="update*"/>
          ???<tx:method name="*" read-only="true"/>
          ??</tx:attributes>
          ?</tx:advice>

          主站蜘蛛池模板: 甘南县| 曲靖市| 南岸区| 永平县| 日照市| 娱乐| 蓝山县| 伊通| 航空| 鲜城| 岫岩| 称多县| 天水市| 康平县| 长葛市| 民和| 崇礼县| 乡宁县| 连云港市| 孙吴县| 灵石县| 临夏市| 乐亭县| 儋州市| 张家港市| 宜兰县| 丹棱县| 永嘉县| 平罗县| 彝良县| 富裕县| 陇川县| 舒城县| 米林县| 抚顺市| 绍兴县| 龙海市| 湖口县| 金寨县| 闽清县| 潞西市|