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

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


          Spring1.2.8


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


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


          ?<!-- 聲明一個抽象Bean,這個Bean是不能實例化的,提供給其它需要AOP事務(wù)的Bean用,其它需要AOP事務(wù)的只要繼承這個Bean就會被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>

          ?<!-- 繼承之前實現(xiàn)的抽象Bean,讓這個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?

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

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

          ?<!--這一處與之前有了變化,在1.2.8版本中,此處的Bean被聲明為由一個FactoryBean生成,而此處只是一個普通的Bean,要簡單許多,透明性要好很多---->
          ?<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>


          ?<!---下面的兩處配置,才是整個事務(wù)AOP的核心,在1.2.8版本中,通過FactoryBean把事務(wù)對象(dataSource),與需要進行事務(wù)控制的對象PetStoreImpl串起來,對PetStoreImpl有侵入性----->
          ?<!---而在之前的兩處配置中,事務(wù)對象(dataSource)與,需要進行事務(wù)控制的對象PetStoreImpl沒有什么關(guān)系,它們的關(guān)系全部體現(xiàn)在下面的兩處配置中----->
          ?
          ?
          ?<!---pointcut屬性定義了哪此點需要去攔截,此處的配置的意思是所有的PetStoreFacade接口中的方法都要攔截,而攔截之后要如何處理則由advice-ref指定的Bean處理----->
          ?<!---配置文件中各個屬性的含義參考: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屬性原配置中沒有,如果缺少此配置,默認值就是“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>

          主站蜘蛛池模板: 华安县| 樟树市| 越西县| 无锡市| 武强县| 阳谷县| 河池市| 新竹县| 东海县| 公主岭市| 湘阴县| 轮台县| 道真| 小金县| 吉水县| 郯城县| 叶城县| 博湖县| 崇左市| 万安县| 深水埗区| 蒙山县| 大厂| 大埔县| 横山县| 桦川县| 全州县| 辽阳市| 泰安市| 县级市| 沧州市| 讷河市| 康平县| 鹿泉市| 闻喜县| 扎囊县| 施甸县| 醴陵市| 武鸣县| 长岛县| 穆棱市|