<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    閑人野居
    好好學(xué)習(xí),天天向上
    posts - 57,  comments - 137,  trackbacks - 0

    在 1.x中,spring 的事務(wù)聲明,一直是采用動態(tài)代理bean 實(shí)現(xiàn)的,也就是采用ProxyFactoryBean或者子類TransactionProxyFactoryBean來實(shí)現(xiàn)的

    考慮下面的例子:(用1.x實(shí)現(xiàn))
    <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    ??? <property name="sessionFactory" ref="mySessionFactory"/>
    ? </bean>

    ? <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean">
    ??? <property name="proxyInterfaces" value="product.ProductService"/>
    ??? <property name="target">
    ??????? <bean class="product.DefaultProductService">
    ??????????? <property name="productDao" ref="myProductDao"/>
    ??????? </bean>
    ??? </property>
    ??? <property name="interceptorNames">
    ????? <list>
    ??????? <value>myTxInterceptor</value> <!-- the transaction interceptor (configured elsewhere) -->
    ????? </list>
    ??? </property>
    ? </bean>

    ?<bean id="myTxInterceptor"

    class="org.springframework.transaction.interceptor.TransactionInterceptor">

    <property name="transactionManager">

    ?? <ref bean="myTransactionManager"/>

    </property>

    <property name="transactionAttributeSource">

    ? <value>

    ?? product.ProductService.increasePrice*=PROPAGATION_REQUIRED

    ? product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY

    ? </value>

    </property>

    </bean>

    或者

    <bean id="myProductService"

    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

    <property name="transactionManager">

    <ref bean="myTransactionManager"/>

    </property>

    <property name="target">

    <ref bean="myProductServiceTarget"/>

    </property>

    <property name="transactionAttributes">

    <props>

    <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>

    <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>

    </props>

    </property>
    </bean>

    當(dāng)然,這需要每個服務(wù)接口都要聲明一個事務(wù)bean ,這比較麻煩,當(dāng)然,spring 也提供了另外一種解決方案

    采用BeanNameAutoProxyCreator 自動代理聲明入口來全局聲明所有的事務(wù)。
    <bean id="matchAllWithPropReq"
    ?????????class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
    ?????????<property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property>
    </bean>

    <bean id="matchAllTxInterceptor"
    ?????????class="org.springframework.transaction.interceptor.TransactionInterceptor">
    ?????????<property name="transactionManager"><ref bean="transactionManager"/></property>
    ?????????<property name="transactionAttributeSource"><ref bean="matchAllWithPropReq"/></property>
    </bean>
    <bean id="autoProxyCreator"
    ?????????class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    ?????????<property name="interceptorNames">
    ????????????<list>
    ???????????????<idref local="matchAllTxInterceptor"/>
    ???????????????<idref bean="hibInterceptor"/>
    ????????????</list>
    ?????????</property>
    ?????????<property name="beanNames">
    ????????????<list>
    ???????????????<idref local="core-services-applicationControllerSevice"/>
    ???????????????<idref local="core-services-deviceService"/>
    ???????????????<idref local="core-services-authenticationService"/>
    ???????????????<idref local="core-services-packagingMessageHandler"/>
    ???????????????<idref local="core-services-sendEmail"/>
    ???????????????<idref local="core-services-userService"/>

    ????????????</list>
    ?????????</property>
    </bean>
    list中包含了所有需要實(shí)現(xiàn)事務(wù)的服務(wù)bean?

    ?spring 2.0帶來的aop變化和bean xml schema的變化,使得事務(wù)的處理變得更加的簡單,
    同aop一樣,事務(wù)也采用兩種方式來處理,一種主要為xml 聲明,另外的一種也就是注釋的引入。
    先來看第一種情況:
    <beans xmlns="http://www.springframework.org/schema/beans"
    ?????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    ?????? xmlns:aop="http://www.springframework.org/schema/aop"
    ?????? xmlns:tx="http://www.springframework.org/schema/tx"
    ?????? xsi:schemaLocation="
    ?????? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    ?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    ?????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    ">

    ? <!-- SessionFactory, DataSource, etc. omitted -->

    ? <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    ??? <property name="sessionFactory" ref="mySessionFactory"/>
    ? </bean>
    ?
    ? <aop:config>
    ?? <!-- 這定義了主要的切面,也就是那些接口可以使用事務(wù),這里只是說執(zhí)行ProductService的所有方法-->
    ??? <aop:pointcut id="productServiceMethods" expression="execution(* product.ProductService.*(..))"/>
    ??? <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
    ? </aop:config>
    ??<!--主要的事務(wù) advice 聲明事務(wù)的相關(guān)屬性-->
    ? <tx:advice id="txAdvice" transaction-manager="myTxManager">
    ??? <tx:attributes>
    ????? <tx:method name="increasePrice*" propagation="REQUIRED"/>
    ????? <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
    ????? <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
    ??? </tx:attributes>
    ? </tx:advice>

    ? <bean id="myProductService" class="product.SimpleProductService">
    ??? <property name="productDao" ref="myProductDao"/>
    ? </bean>

    </beans>

    第二種方式,使用 @Transactional 注釋

    @Transactional

    public interface FooService {

    ?

    ??? Foo getFoo(String fooName);

    ?

    ??? Foo getFoo(String fooName, String barName);

    ?

    ??? void insertFoo(Foo foo);

    ?

    ??? void updateFoo(Foo foo);

    }


    xml 配置

    <!-- from the file 'context.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:aop="http://www.springframework.org/schema/aop"

    ?????? xmlns:tx="http://www.springframework.org/schema/tx"

    ?????? xsi:schemaLocation="

    ?????? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    ?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

    ?????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    ?

    ? <!-- this is the service object that we want to make transactional -->

    ? <bean id="fooService" class="x.y.service.DefaultFooService"/>

    ?

    ? <!-- enable the configuration of transactional behavior based on annotations -->

    ? <tx:annotation-driven/>

    ?

    ? <!-- a PlatformTransactionManager is still required -->

    ? <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    ??? <!-- sourced from somewhere else -->

    ??? <property name="dataSource" ref="dataSource"/>

    ? </bean>

    ?

    ? <!-- other <bean/> definitions here -->

    ?

    </beans>

    public class DefaultFooService implements FooService {

    ?

    ??? public Foo getFoo(String fooName) {

    ??????? // do something

    ??? }

    ?

    ??? // these settings have precedence

    ??? @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)

    ??? public void updateFoo(Foo foo) {

    ??????? // do something

    ??? }

    }

    posted on 2006-09-18 20:13 布衣郎 閱讀(2651) 評論(0)  編輯  收藏 所屬分類: spring

    <2006年9月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357268
    • 排名 - 155

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 57PAO成人国产永久免费视频 | 亚洲精品无码久久不卡| 免费一级做a爰片久久毛片潮| 亚洲AV无码国产精品色午友在线| 中国在线观看免费高清完整版| 无套内谢孕妇毛片免费看看| 亚洲人成网站影音先锋播放| 全免费a级毛片免费看不卡| 全黄大全大色全免费大片| 亚洲精品中文字幕无乱码麻豆| 久久久久噜噜噜亚洲熟女综合| 日韩精品免费一级视频| 色费女人18女人毛片免费视频| 久久久久久亚洲AV无码专区| www.亚洲精品.com| 成人免费无码视频在线网站| AAA日本高清在线播放免费观看| 亚洲精品理论电影在线观看| 最新欧洲大片免费在线| 国产JIZZ中国JIZZ免费看| 伊人久久亚洲综合影院首页| 成人黄动漫画免费网站视频| 99精品免费视品| 免费大片av手机看片高清| www.亚洲成在线| 亚洲AV日韩精品久久久久久久 | 国产精品成人观看视频免费 | 免费jjzz在在线播放国产| 日本最新免费网站| 岛国精品一区免费视频在线观看| 亚洲av第一网站久章草| 亚洲激情电影在线| 亚洲第一AV网站| 在线播放亚洲第一字幕| 又黄又爽无遮挡免费视频| 亚洲成在人线aⅴ免费毛片| 久久永久免费人妻精品下载| 中文字幕免费在线播放| 人碰人碰人成人免费视频| 亚洲精品久久无码| 亚洲欧洲无码AV不卡在线|