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

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

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

    空間站

    北極心空

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks
    Spring 2.x在配置文件的簡化的方面做了很多工作,原來1.x中比較麻煩的配置都已經擁有了比較完美的解決方案。最近剛看完《精通Spring 2.x --企業應用開發精解》的書,結合自己的經驗整理一下簡化配置的內容。

    一、關于集合的配置
    1.List
    >1.x版本的
    Java代碼 復制代碼
    1. <bean id="parentBoss" abstract="true"class="com.baobaotao.attr.Boss"> <--父<bean>   
    2.     <property name="favorites">   
    3.         <set>   
    4.             <value>看報</value>   
    5.             <value>賽車</value>   
    6.             <value>高爾夫</value>   
    7.         </set>   
    8.     </property>   
    9. </bean>  


    >2.x版本的
    Java代碼 復制代碼
    1. <util:list id="favoriteList1" list-class="java.util.LinkedList">   
    2.     <value>看報</value>   
    3.     <value>賽車</value>   
    4.     <value>高爾夫</value>   
    5. </util:list>  


    2.Set
    > 1.x
    Java代碼 復制代碼
    1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
    2.     <property name="favorites">   
    3.        <set>    
    4.           <value>看報</value>   
    5.           <value>賽車</value>   
    6.           <value>高爾夫</value>   
    7.        </set>   
    8.     </property>   
    9. </bean>  

    > 2.x
    Java代碼 復制代碼
    1.     
    2. <util:set id="favoriteSet1">   
    3.  <value>看報</value>   
    4.  <value>賽車</value>   
    5.   <value>高爾夫</value>   
    6. </util:set>  

    3.Map
    > 1.x
    Java代碼 復制代碼
    1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
    2.     <property name="jobs">   
    3.     <map>   
    4.           <!--Map第一個元素-->   
    5.                <entry>    
    6.               <key><value>AM</value></key>   
    7.               <value>會見客戶</value>   
    8.            </entry>   
    9.           <!--Map第二個元素-->   
    10.                <entry>    
    11.               <key><value>PM</value></key>   
    12.               <value>公司內部會議</value>   
    13.           </entry>                 
    14.     </map>   
    15.     </property>   
    16. </bean>  

    > 2.x
    Java代碼 復制代碼
    1. <util:map id="emails1">   
    2.     <entry key="AM" value="會見客戶" />   
    3.     <entry key="PM" value="公司內部會議" />   
    4. </util:map>  


    4. Properties

    > 1.x
    Java代碼 復制代碼
    1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
    2.     <property name="mails">   
    3.         <props>   
    4.            <prop key="jobMail">john-office@baobaotao.com</prop>   
    5.            <prop key="lifeMail">john-life@baobaotao.com</prop>   
    6.         </props>   
    7.     </property>   
    8. </bean>  


    > 2.x
    Java代碼 復制代碼
    1. <util:properties id="emailProps1" location="classpath:com/baobaotao/fb/mails.properties"/>  

    可以在一個屬性文件中直接配置屬性,這比較符合一般的項目習慣。

    二、 關于事務配置


    1.1.x
    Java代碼 復制代碼
    1.  <bean id="bbtForum"    
    2. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
    3. <property name="transactionManager" ref="txManager" />    
    4. <property name="target" ref="bbtForumTarget"/>   
    5. <property name="transactionAttributes">    
    6.     <props>   
    7.         <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>    
    8.         <prop key="*">PROPAGATION_REQUIRED</prop>    
    9.     </props>   
    10. </property>   
    11.  </bean>  


    2.2.x

    有兩種新方法
    a)使用@Transactional注解
    在需要的服務類或服務方法處直接打上@Transactional注解,然后在Spring配置文件中啟用注解事務驅動就可以了:

    Java代碼 復制代碼
    1. @Transactional    
    2. public class BbtForumImpl implements BbtForum {   
    3.     @Transactional(readOnly=true)    
    4.     public Forum getForum(int forumId) {   
    5.         return forumDao.getForum(forumId);   
    6.     }   
    7.        。。。。   
    8. }  

    在Spring配置文件中相應添加上:
    Java代碼 復制代碼
    1. <bean id="txManager"  
    2.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    3.         <property name="dataSource" ref="dataSource" />   
    4. </bean>   
    5. <tx:annotation-driven transaction-manager="txManager"/>  

    這樣就OK了,簡單吧:)

    b)使用aop/tx

    如果你的Service服務類都很規范,我覺得使用aop/tx更方面,因為不用到處打注解,在一處集中配置就OK了,可謂運籌帷幄之中,決勝于千里之外:)
    Java代碼 復制代碼
    1. <aop:config>    
    2. t;aop:pointcut id="serviceMethod"    
    3.    expression="execution(* com.baobaotao.service.*Forum.*(..))" />   
    4. t;aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />    
    5.  </aop:config>   
    6. <tx:advice id="txAdvice" transaction-manager="txManager">    
    7.    <tx:attributes>    
    8.        <tx:method name="get*" read-only="false"/>   
    9.        <tx:method name="add*" rollback-for="Exception" />   
    10.        <tx:method name="update*"/>            
    11.    </tx:attributes>   
    12. lt;/tx:advice>   


    三、關于AOP配置

    原來1.x的AOP太麻煩了,提都不想提,直接說一下2.x的AOP。
    Spring 2.x使用@AspectJ來描述切面,由于@AspectJ的語法描述能力超強,因此在Spring 2.x中使用AOP真的非常方便。

    在使用@AspectJ之前,首先你得保證你所使用的JDK的版本是5.0及以上版本,否則無法使用注解技術。
    Spring在處理@Aspect注解表達式時,需要使用位于spring/lib/asm下asm關聯類庫,將該類庫的三個類包加入到類路徑中:asm-2.2.2.jar、asm-commons-2.2.2.jar和asm-util-2.2.2.jar。我們在第一章中了解了asm類庫的用途,它是輕量級的字節碼處理框架,因為Java的反射機制無法獲取入參名,Spring就利用asm處理@AspectJ中所描述的方法入參名。

    此外,Spring采用AspectJ提供的@AspectJ注解類庫及相應的解析類庫,它位于spring/lib/aspectj目錄下,將目錄下的aspectjrt.jar和aspectjweaver.jar類包加入類路徑中。
    在做好上節中所提到的前置工作后,我們就可以開始編寫一個基于@AspectJ的切面了,首先來看一個簡單的例子,以便對@AspectJ有一個切身的認識。

    @AspectJ采用不同的方式對AOP進行描述, 我們使用NaiveWaiter的例子來說明,這是一個希望引入切面的目標類:
    Java代碼 復制代碼
    1. package com.baobaotao;   
    2. public class NaiveWaiter implements Waiter {   
    3.     public void greetTo(String clientName) {   
    4.         System.out.println("NaiveWaiter:greet to "+clientName+"...");   
    5.     }   
    6.     public void serveTo(String clientName){   
    7.         System.out.println("NaiveWaiter:serving "+clientName+"...");   
    8.     }      
    9. }  


    下面使用@AspectJ來定義一下切面:
    Java代碼 復制代碼
    1. package com.baobaotao.aspectj.aspectj;   
    2. import org.aspectj.lang.annotation.Aspect;   
    3. import org.aspectj.lang.annotation.Before;   
    4. //通過該注解將PreGreetingAspect標識為一個切面   
    5. @Aspect    
    6. public class PreGreetingAspect{   
    7.     @Before("execution(* greetTo(..))"//<---定義切點和增強類型   
    8.     public void beforeGreeting(){ //<----增強的橫切邏輯   
    9.         System.out.println("How are you");   
    10.     }   
    11. }  


    然后啟動@AspectJ的注解切面驅動就可以了!
    Java代碼 復制代碼
    1. <?xml version="1.0" encoding="UTF-8" ?>   
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:aop="http://www.springframework.org/schema/aop"    
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    6. <A href="http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</A>   
    7.                         http://www.springframework.org/schema/aop    
    8. <A href="http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" target=_blank>http://www.springframework.org/schema/aop/spring-aop-2.0.xsd</A>">   
    9.     <!--基于@AspectJ切面的驅動器-->   
    10.         <aop:aspectj-autoproxy />    
    11.     <bean id="waiter" class="com.baobaotao.NaiveWaiter" />   
    12.     <bean class="com.baobaotao.aspectj.example.PreGreetingAspect" />   
    13. </beans>  


    四、關于Spring 2.1添加的新功能

    1.原來引入一個外面屬性配置文件需要使用以下的方式:
    Java代碼 復制代碼
    1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
    2.     <property name="locations">   
    3.        <list>   
    4.            <!--指定屬性文件地址,可以在這里定義多個屬性文件-->   
    5.           <value>classpath:com/baobaotao/place/car.properties</value>    
    6.        </list>   
    7.     </property>   
    8.     <property name="fileEncoding" value="utf-8"/>   
    9. </bean>   
    10.   
    11. <!--引用外部屬性的值,對car屬性進行配置-->   
    12. <bean id="car" class="com.baobaotao.place.Car">   
    13.     <property name="brand" value="${brand}" />   
    14.     <property name="maxSpeed" value="${maxSpeed}" />   
    15.     <property name="price" value="${price}" />   
    16. </bean>  


    使用Spring 2.1后,你只需要象下面這樣配置就可以了:
    Java代碼 復制代碼
    1. <context:property-placeholder location=" classpath:com/baobaotao/place/car.properties "/>  


    3.注解驅動的Bean注入

    大家看看這段E文就OK了,
    <context:component-scan>: scans classpath using one or more "include"/"exclude" filters, and automatically registers beans
    In essence, this is a third way to define beans (1: classic xml, 2:javaconfig (code), 3:<context:component-scan>, matching on annotations or types)
    Default naming strategy is based on short classname of discovered bean
    @Component and @Repository annotations, when used, can optionally specify a bean name to use
    For filter type "annotation", the value of "expression" attribute should resolve to a Java annotation type
    For filter type "assignable", the value of "expression" attribute should resolve to a Java type
    For filter type "aspectj", the value of "expression" should be an "type expression" (in Pointcut language, perhaps it could be injected?)
    Relevant documentation can be found in preliminary spring 2.1 manual, sections 3.10 and 3.11
    In addition, this last JIRA comment for http://www.jetbrains.net/jira/browse/IDEADEV-16886#action_163502 contains two links to articles showing example usage of <context:component-scan>
    <context:annotation-config> (described in 3.10 in spring 2.1 manual linked above): allows autowiring to be defined using @Resource or @Autowired annotations.


    五、關于Spring 2.5添加的新功能
    Spring 2.5繼續對context命名空間進行了擴充,添加了好用而強大的context:load-time-weaver,可以讓我們更方便地應用AspectJ。大家可以看TSS上的這篇文章,它全面講解了Spring 2.5的新特性。
    http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25

    注:以上大部分代碼來直接引用自 《精通Spring 2.x--企業應用開發精解》
    文章來源于:javaeye的stamen
    posted on 2008-03-14 09:44 蘆葦 閱讀(1007) 評論(1)  編輯  收藏 所屬分類: Spring

    Feedback

    # re: 談談Spring 2.x中簡化配置的問題 2009-09-25 14:07 hfliyy
    分多個模塊,多寫幾個例子就好了。  回復  更多評論
      

    主站蜘蛛池模板: 日韩电影免费在线观看视频| 中文字幕av无码无卡免费| 免费一级做a爰片久久毛片潮喷| 亚洲免费在线观看视频| 2021久久精品免费观看| 亚洲大尺码专区影院| 日韩在线免费视频| 亚洲国产成人91精品| 在线视频观看免费视频18| 99热亚洲色精品国产88| 蜜桃视频在线观看免费网址入口| 亚洲一区在线免费观看| 无码免费午夜福利片在线| 亚洲国产成人精品无码区花野真一| 国产精品美女自在线观看免费| 亚洲av永久无码一区二区三区| 亚洲AV无码一区二三区 | 亚洲中文字幕在线乱码| 免费国产在线精品一区| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲日本va一区二区三区| 午夜a级成人免费毛片| 色窝窝亚洲av网| 亚洲一区精品无码| 51在线视频免费观看视频| 亚洲一级黄色大片| 波多野结衣中文一区二区免费| 人碰人碰人成人免费视频| 国产国拍亚洲精品mv在线观看 | 亚洲日本中文字幕一区二区三区| 免费国产va在线观看| 久久亚洲免费视频| 色窝窝免费一区二区三区| 欧洲美女大片免费播放器视频| 激情综合色五月丁香六月亚洲| 最近最新高清免费中文字幕| 亚洲日本va一区二区三区| 久久精品亚洲男人的天堂| 18禁美女黄网站色大片免费观看| 亚洲一日韩欧美中文字幕在线 | 亚洲av无码一区二区三区网站 |