<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
    分多個模塊,多寫幾個例子就好了。  回復  更多評論
      

    主站蜘蛛池模板: 免费精品国产自产拍观看| 在线观看日本免费a∨视频| 久久亚洲欧洲国产综合| 青青久久精品国产免费看| 啊v在线免费观看| 人体大胆做受免费视频| 久久精品国产亚洲精品| 97国免费在线视频| 亚洲综合在线视频| 国产高清免费视频| 亚洲一区二区三区高清在线观看| 免费A级毛片无码免费视| 亚洲欧洲专线一区| 亚洲国产成人精品久久久国产成人一区二区三区综 | 男女猛烈xx00免费视频试看| 亚洲?V乱码久久精品蜜桃| 巨胸喷奶水www永久免费| 亚洲成色999久久网站| 91香蕉成人免费网站| 亚洲色一区二区三区四区| 亚洲av无码天堂一区二区三区| 久久嫩草影院免费看夜色| 亚洲丝袜美腿视频| 大学生一级特黄的免费大片视频| 美女视频免费看一区二区| 亚洲精品美女久久777777| 无码国产精品一区二区免费式影视 | 亚洲精品视频在线观看免费 | 无码国产精品一区二区免费| 久久亚洲中文字幕无码| 亚洲老妈激情一区二区三区| 国产福利在线免费| jizz免费在线影视观看网站| 1区1区3区4区产品亚洲| 国产大片51精品免费观看| 爱丫爱丫影院在线观看免费| 国产成人精品日本亚洲专| 国产精品亚洲w码日韩中文| www视频在线观看免费| 国产精品亚洲а∨天堂2021| 久久久亚洲欧洲日产国码二区 |