Spring20简明手册系列之五 事务管理.docx

上传人:b****2 文档编号:2605305 上传时间:2023-05-04 格式:DOCX 页数:16 大小:19.34KB
下载 相关 举报
Spring20简明手册系列之五 事务管理.docx_第1页
第1页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第2页
第2页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第3页
第3页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第4页
第4页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第5页
第5页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第6页
第6页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第7页
第7页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第8页
第8页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第9页
第9页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第10页
第10页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第11页
第11页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第12页
第12页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第13页
第13页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第14页
第14页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第15页
第15页 / 共16页
Spring20简明手册系列之五 事务管理.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Spring20简明手册系列之五 事务管理.docx

《Spring20简明手册系列之五 事务管理.docx》由会员分享,可在线阅读,更多相关《Spring20简明手册系列之五 事务管理.docx(16页珍藏版)》请在冰点文库上搜索。

Spring20简明手册系列之五 事务管理.docx

Spring20简明手册系列之五事务管理

Spring2.0简明手册(系列之五事务管理)

2008-07-1621:

26:

45

标签:

Spring简明手册

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。

否则将追究法律责任。

1.声明式事务管理

   Spring2.0及以后的版本中声明式事务的配置与之前的版本有相当大的不同。

主要差异在于不再需要配置TransactionProxyFactoryBean了。

1.1基于XMLSchema

//我们想做成事务性的服务接口

packagex.y.service;

publicinterfaceFooService{

   FoogetFoo(StringfooName);

   FoogetFoo(StringfooName,StringbarName);

   voidinsertFoo(Foofoo);

   voidupdateFoo(Foofoo);

}

//上述接口的一个实现

packagex.y.service;

publicclassDefaultFooServiceimplementsFooService{

   publicFoogetFoo(StringfooName){

       thrownewUnsupportedOperationException();

   }

   publicFoogetFoo(StringfooName,StringbarName){

       thrownewUnsupportedOperationException();

   }

   publicvoidinsertFoo(Foofoo){

       thrownewUnsupportedOperationException();

   }

   publicvoidupdateFoo(Foofoo){

       thrownewUnsupportedOperationException();

   }

}

    

xmlversion="1.0"encoding="UTF-8"?

>

    

//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="

          [url]http:

//www.springframework.org/schema/beans[/url][url]http:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]

          [url]http:

//www.springframework.org/schema/tx[/url][url]http:

//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]

          [url]http:

//www.springframework.org/schema/aop[/url][url]http:

//www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]">

        

--===================================-->

        

--                        加载属性文件                        -->

        

--===================================-->

        

            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            

                

                    classpath:

jdbc-config.properties

                

            

        

        

--===================================-->

        

--                        配置数据源                          -->

        

--===================================-->

      

        

        

        

        

      

      

        

      

        

--===================================-->

        

--                        配置事务                            -->

        

--===================================-->

        

config>

            

pointcutid="defaultServiceOperation"expression="execution(*x.y.service.*Service.*(..))"/>

            

advisorpointcut-ref="defaultServiceOperation"advice-ref="defaultTxAdvice"/>

            

pointcutid="noTxServiceOperation"expression="execution(*x.y.service.ddl.DefaultDdlManager.*(..))"/>

            

advisorpointcut-ref="noTxServiceOperation"advice-ref="noTxAdvice"/>

        

config>

        

adviceid="defaultTxAdvice"transaction-manager="txManager">

            

attributes>

                

methodname="get*"read-only="true"/>

                

methodname="*"/>

            

attributes>

        

advice>

        

adviceid="noTxAdvice"transaction-manager="txManager">

            

attributes>

                

methodname="*"propagation="NEVER"/>

            

attributes>

        

advice>

        

--===================================-->

        

--                        配置业务Bean                        -->

        

--===================================-->

        

--

      thisbeanwillbetransactional(c.f.the'defaultServiceOperation'pointcut)

      -->

        

        

--thisbeanwillalsobetransactional,

          butwithtotallydifferenttransactionalsettings-->

        

    

   更进一步:

参照Spring2.0参考手册--9.5.5.

advice/>有关的设置

1.2使用@Transactional

   //theserviceclassthatwewanttomaketransactional

   @Transactional(readOnly=true)

   publicclassDefaultFooServiceimplementsFooService{

      publicFoogetFoo(StringfooName){

          //dosomething

      }

      //thesesettingshaveprecedenceforthismethod

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

      publicvoidupdateFoo(Foofoo){

          //dosomething

      }

   }

    

xmlversion="1.0"encoding="UTF-8"?

>

    

//www.springframework.org/schema/beans"

          xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

          xmlns:

tx="http:

//www.springframework.org/schema/tx"

          xsi:

schemaLocation="

          [url]http:

//www.springframework.org/schema/beans[/url][url]http:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]

          [url]http:

//www.springframework.org/schema/tx[/url][url]http:

//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]">

        

--===================================-->

        

--                        加载属性文件                        -->

        

--===================================-->

        

            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            

                

                    classpath:

jdbc-config.properties

                

            

        

        

--===================================-->

        

--                        配置数据源                          -->

        

--===================================-->

      

        

        

        

        

      

      

        

      

        

--===================================-->

        

--                        配置事务                            -->

        

--===================================-->

      

--enabletheconfigurationoftransactionalbehaviorbasedonannotations-->

      

annotation-driventransaction-manager="txManager"/>

        

--===================================-->

        

--                        配置业务Bean                        -->

        

--===================================-->

      

--thisistheserviceobjectthatwewanttomaketransactional-->

      

    

   更进一步:

参照Spring2.0参考手册--9.5.6.1.@Transactional有关的设置

1.3插入事务操作

   1.3.1使用@Transactional

   

--theserviceclassthatwewanttomaketransactional-->

   @Transactional(readOnly=true)

   publicclassDefaultFooServiceimplementsFooService{

      publicFoogetFoo(StringfooName){

          //dosomething

      }

      //thesesettingshaveprecedenceforthismethod

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

      publicvoidupdateFoo(Foofoo){

          //dosomething

      }

   }

packagex.y;

importorg.aspectj.lang.ProceedingJoinPoint;

importorg.springframework.util.StopWatch;

importorg.springframework.core.Ordered;

publicclassSimpleProfilerimplementsOrdered{

   privateintorder;

   //allowsustocontroltheorderingofadvice

   publicintgetOrder(){

       returnthis.order;

   }

   publicvoidsetOrder(intorder){

       this.order=order;

   }

   //thismethodisthearoundadvice

   publicObjectprofile(ProceedingJoinPointcall)throwsThrowable{

       ObjectreturnValue;

       StopWatchclock=newStopWatch(getClass().getName());

       try{

           clock.start(call.toShortString());

           returnValue=call.proceed();

       }finally{

           clock.stop();

           System.out.println(clock.prettyPrint());

       }

       returnreturnValue;

   }

}

xmlversion="1.0"encoding="UTF-8"?

>

//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="

  [url]http:

//www.springframework.org/schema/beans[/url][url]http:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]

  [url]http:

//www.springframework.org/schema/tx[/url][url]http:

//www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]

  [url]http:

//www.springframework.org/schema/aop[/url][url]http:

//www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]">

    

    

--thi

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2