隨筆-9  評論-168  文章-266  trackbacks-0

          一.AOP(常用)

          <?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-2.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
          <!-- 這是我們將要配置并使它具有事務性的Service對象 -->

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

          <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->

          <tx:advice id="txAdvice" transaction-manager="txManager">
          <!-- the transactional semantics... -->

          <tx:attributes>
          <!-- all methods starting with 'get' are read-only -->

          <tx:method name="get*" read-only="true"/>
          <!-- other methods use the default transaction settings (see below) -->

          <tx:method name="*"/>
          </tx:attributes>
          </tx:advice>
          <!-- ensure that the above transactional advice runs for any executionof an operation defined by the FooService interface -->

          <aop:config>
          <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
          </aop:config>
          <!-- ******************************************************************-->
          <!-- don't forget the DataSource -->

          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
          <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
          <property name="username" value="scott"/>
          <property name="password" value="tiger"/>
          </bean>

          <!-- similarly, don't forget the (particular) PlatformTransactionManager -->

          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
          </bean>
          <!-- other <bean/> definitions here -->

          </beans>

          我們來分析一下上面的配置。我們要把一個服務對象('fooService' bean)做成事務性的。我們想施加的事務語義封裝在<tx:advice/>定義中。<tx:advice/>把所有以 'get' 開頭的方法看做執行在只讀事務上下文中,其余的方法執行在默認語義的事務上下文中”。 其中的 'transaction-manager' 屬性被設置為一個指向 PlatformTransactionManager bean的名字(這里指 'txManager'),該bean將實際上實施事務管理。

          配置中最后一段是 <aop:config/> 的定義,它確保由 'txAdvice' bean定義的事務通知在應用中合適的點被執行。首先我們定義了 一個切面,它匹配 FooService 接口定義的所有操作,我們把該切面叫做 'fooServiceOperation'。然后我們用一個通知器(advisor)把這個切面與 'txAdvice' 綁定在一起,表示當 'fooServiceOperation' 執行時,'txAdvice' 定義的通知邏輯將被執行。

          一個普遍性的需求是讓整個服務層成為事務性的。滿足該需求的最好方式是讓切面表達式匹配服務層的所有操作方法。例如:

          <aop:config>
              <aop:pointcut id="fooServiceMethods" expression="execution(* x.y.service.*.*(..))"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
            </aop:config>
          posted on 2009-03-30 17:37 紫蝶∏飛揚↗ 閱讀(2499) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 定远县| 峨眉山市| 顺昌县| 巴马| 新和县| 托克托县| 蒙山县| 余庆县| 云浮市| 贵德县| 汉源县| 娱乐| 新安县| 华池县| 隆尧县| 南涧| 安达市| 黄浦区| 墨竹工卡县| 临武县| 永福县| 南漳县| 灌云县| 库车县| 盐边县| 丰顺县| 凤台县| 永德县| 贺州市| 苍南县| 沙雅县| 禹州市| 侯马市| 万州区| 牙克石市| 揭阳市| 白朗县| 临西县| 舒兰市| 兴海县| 娱乐|