CONAN ZONE

          你越掙扎我就越興奮

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks

          作者簡介:

           

          Craig Walls Texas-based 公司的軟件開發(fā)人員,有著超過 13 年的開發(fā)經驗,涉及的領域有通信,金融,零售,教育以及軟件業(yè)等。他是 Spring Framework 的狂熱擁護者,頻繁的在當地 local user groups 討論組和相關會議上演講 Spring ,并且他的 Blog 上也有很多關于 Spring 的內容。

           

          出版的著作有:

          l          Spring in Action, 2nd Edition, 2007

          l          XDoclet in Action, 2003

           

          他的 Blog 是:

          l          http://www.springinaction.com

           

          所參與的項目:

          l          Committer to XDoclet project;

          l          Originator of Portlet and Spring modules for XDoclet

           

           

           

          本手冊主要是將分布于文檔中的那些零散的配置文件部分統(tǒng)一成一個比較系統(tǒng)的整體。結合 Spring 文檔一起查閱也許能節(jié)省你一些時間。不過,并不推薦你全部掌握;很多陌生的元素或標簽只應用于特定場合。本手冊英文版本可以在: http://java.dzone.com 下載。

           

           

           

           

           

           

           

          Spring 配置全書

           

           

          關于 Spring 的配置

           

          Spring Framework 總是不斷的改變著 Java 企業(yè)開發(fā)的方向,它用一種松耦合的方式來配置和組裝應用程序對象和業(yè)務對象,比以往的 Java 企業(yè)開發(fā)來的更加簡潔。一旦你開發(fā)了基于 Spring 的應用程序,在 Spring 上下文配置的那些資源簡直就是唾手可得。

           

           

           

          依賴注入是 Spring 容器的核心

           

           

          盡管 Spring Framework 可以做很多事,但依賴注入卻是 Spring 容器提供的最基本的功能。

           

          任何稍微復雜一點的應用程序都至少由兩個或兩個以上的對象協(xié)作在一起,共同完成一些業(yè)務邏輯。以往的 Java 企業(yè)開發(fā),每個對象都要自己去主動獲得他們所引用(或依賴)的對象,才可正常運作。這將導致代碼之間的緊耦合,難以測試。

           

           

           

           

           

           

           

          有了依賴注入后,對象所依賴的資源則可通過外部來獲得。換句話說,對象所依賴的資源是按照它們的需要給注入進去的。對于基于 Spring 的應用程序來說,是 Spring 容器將這些對象所依賴的資源幫助實現(xiàn)注入依賴的。

           

           

           

           

           

           

           

          XML 來配置 Spring

           

           

          到了 Spring2.0 Spring 鼓勵你使用基于 XML Scheme 的配置方式來應用于你的系統(tǒng),這比起過去基于 DTD 的方式要更加靈活。一個典型的 Spring2.5 配置文件至少擁有以下結構:

           

           

           

           

           

           

           

          Xml代碼 復制代碼
          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. xsi:schemaLocation=”http://www.springframework.org/schema/beans    
          5. <A href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</A>>    
          6.   
          7. <!-- place configuration details here -->  
          8.   
          9. </beans>  
           

           

           

           

           

           

           

          Beans 命名空間簡介

           

           

          Schema URI

          www.springframework.org/schema/beans

           

          Schema XSD

          www.springframework.org/schema/beans/spring-beans-2.5.xsd

           

          beans 命名空間是Spring 命名空間的核心,也是你配置Spring 時使用最多的一個。根元素是<beans> ,它不僅可以包含一個或多個<bean> 子元素,而且還可以包含其它命名空間的元素,甚至在<beans> 下你可以不配置任何<bean> 子元素。

           

           

          Spring XML 圖表的一些約定

           

          Spring XML 圖通常使用以下符號來表示哪些元素是必選的,可選的以及它們之間的包含關系。

           

           

           

           

           

           

           

           

           

           

          Bean 命名空間下的元素簡介

          <bean> 元素揭密

           

          雖然有很多 XML 元素可以用來配置 Spring context ,但也許你用的最多的可能還是 <bean> 元素。因此,讓你深入了解 <bean> 標簽是十分必要的。

           

           

           

          Bean 命名空間實例

           

          下面的 Spring XML 配置文件配置了兩個 beans ,其中一個注入到另一個中去:

           

           

           

          Xml代碼 復制代碼
          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. xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>    
          5.   
          6. <bean id=”pirate” class=”Pirate”>    
          7. <constructor-arg value=”Long John Silver” />    
          8. <property name=”map” ref=”treasureMap” />    
          9. </bean>    
          10.   
          11. <bean id=”treasureMap” class=”TreasureMap” />  
          12.   
          13. </beans>  

           

           

           

          第一個 bean ID 為“ pirate ”,類型為“ Pirate ”。它使用了構造函數注入,該構造函數帶有一個 String 參數,在這個例子中參數的值為“ Long John Silver ”。另外,它的“ map ”屬性引用了另一個叫“ treasureMap ”的 bean ,該 bean TreasureMap 的一個實例。

           

           

           

           

          溫馨提示:

           

          不要把你所有的 beans 都定義在一個 XML 文件中。一旦你的應用程序變得越來越復雜,在 Spring XML 配置文件中定義的 beans 的數量一定讓你印象深刻。也沒有什么理由要把所有的 beans 都定義在一個 XML 配置文件中去。通過將所有的 beans 分別放在多個 XML 文件中,有助于你的 Spring 配置文件更易于管理。當應用程序上下文 (application context) 建立的時候,可以使用 <import> 元素將它們全部組裝起來:

           

           

          Xml代碼 復制代碼
          1. <import resource=”service-layer-config.xml” />    
          2. <import resource=”data-layer-config.xml” />    
          3. <import resource=”transaction-config.xml” />  

           

           

           

           

           

           

          Context 命名空間簡介

           

           

          Schema URI

          www.springframework.org/schema/context

           

          Schema XSD

          www.springframework.org/schema/context/spring-context-2.5.xsd

           

           

          Spring2.5 中, context 命名空間主要用來提供多種 application context 特定的配置。它包括:支持基于 annotation 的配置方式, JMX 以及領域對象 (domain object) 的注入。

           

           

           

           

           

           

           

          Context 命名空間元素簡介

           

           

           

           

           

           

          Bean 命名空間實例

           

           

          下面的 Spring 配置文件使用了 <context:component-scan> 用來自動注冊“ com.springinactin.service ”包下的 beans

           

           

          Xml代碼 復制代碼
          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:context=”http://www.springframework.org/schema/context”    
          5. xsi:schemaLocation=”http://www.springframework.org/schema/beans    
          6. <A href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</A>    
          7. <A href="http://www.springframework.org/schema/context" target=_blank>http://www.springframework.org/schema/context</A>    
          8. <A href="http://www.springframework.org/schema/context/spring-context-2.5.xsd" target=_blank>http://www.springframework.org/schema/context/spring-context-2.5.xsd</A>>    
          9.   
          10. <context:component-scan base-package=”com.springinac¬tion.service” />  
          11.   
          12. </beans>  
           

           

           

           

          在上面的配置文件中, <context:component-scan> 元素會自動掃描“ com.springinac­tion.service ”包下的類,并自動將那些標記有 @Component, @Controller, @Repository, @Service @Aspect. 的類全部注冊到 Spring 容器中。

           

           

           

           

           

          溫馨提示:

           

          盡量為你的最終用戶提供外部配置文件。

          將所有的配置都定義在 Spring 配置文件中并不推薦。你根本別指望應用程序的管理員或最終用戶會去研究 Spring XML 然后搞懂數據庫的配置和其它特定布署細節(jié),你不會真打算靠他們吧?相反,使用外部配置文件,我們可以使用 <context:property-placeholder> 這么來做:

           

          Xml代碼 復制代碼
          1. <context:property-placeholder location=”file:////etc/pirate.properties”  
           

           

          定義在 /etc/pirate.properties 屬性文件中的那些鍵值對現(xiàn)在可以在 Spring 配置文件中大顯身手啦:

           

          Xml代碼 復制代碼
          1. <bean id=”pirate” class=”Pirate”>    
          2. <constructor-arg value=”${pirate.name}” />  
          3. </bean>  
           

           

           

           

           

          AOP 命名空間簡介

           

           

          Schema URI

          www.springframework.org/schema/aop

           

          Schema XSD

          www.springframework.org/schema/aop/spring-context-2.5.xsd

           

           

           

          aop 命名空間是用來在 Spring context 中聲明 aspects, pointcuts advice 的。同樣,使用 @Aspectj

          annotation 的話,也可以基于 annotation 方式來配置你的 aop 。使用 aspects 的話,可以定義被你多個程序切點使用(或織入)的功能。

           

           

           

           

           

          AOP 命名空間元素簡介

           

           

           

           

           

           

          AOP 命名空間實例

           

          下面的 Spring 配置文件使用了 aop 命名空間來建一個 aspect

           

           

          Xml代碼 復制代碼
          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.5.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</A>    
          7. <A href="http://www.springframework.org/schema/aop" target=_blank>http://www.springframework.org/schema/aop</A>    
          8. <A href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" target=_blank>http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</A>>    
          9. <bean id=”pirateTalker” class=”PirateTalker” />    
          10.     <aop:config>    
          11.     <aop:pointcut id=”plunderPointcut” expression=”execution(* *.plunder(..))” />    
          12.     <aop:aspect ref=”pirateTalker”>    
          13.     <aop:before pointcut-ref=”plunderPointcut” method=”sayAvastMeHearties” />    
          14.     <aop:after-returning pointcut-ref=”plunderPointcut” method=”sayYarr” />    
          15. </aop:aspect>    
          16. </aop:config>  
          17. </beans>  
           

           

           

           

           

          aspect 由一個 pointcut 和兩個 advice 組成。該 pointcut 用來定義所有對象 plunder() 方法的執(zhí)行。標有 <asp:before> advice 用來配置當 plunder() 執(zhí)行時,立即調用 pirateTalker bean sayAvastMeHearties() 方法;類似的,當 plunder() 方法調用成功時, sayYarr() 方法同樣也會觸發(fā)。

           

           

           

           

           

          JEE 命名空間簡介

           

           

          Schema URI

          www.springframework.org/schema/jee

           

          Schema XSD

          www.springframework.org/schema/jee/spring-context-2.5.xsd

           

           

           

           

           

          JEE 命名空間元素簡介

           

           

           

           

           

           

          JEE 命名空間實例

           

          下面的 Spring 配置文件使用了部分 jee 命名空間元素,用來獲取 Spring 容器外的對象,并且將這些對象作為 Spring beans

           

           

          Xml代碼 復制代碼
          1. <?xml version=”1.0” encoding=”UTF-8”?>  
          2.   
          3. <beans xmlns=”http://www.springframework.org/schema/beans”    
          4. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”    
          5. xmlns:jee=”http://www.springframework.org/schema/jee”    
          6. xsi:schemaLocation=”http://www.springframework.org/schema/beans    
          7. <A href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</A>    
          8. <A href="http://www.springframework.org/schema/jee" target=_blank>http://www.springframework.org/schema/jee</A>    
          9. <A href="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" target=_blank>http://www.springframework.org/schema/jee/spring-jee-2.5.xsd</A>>    
          10.   
          11. <jee:remote-slsb id=”hispaniola” jndi-name=”ejb/PirateShip” business-interface=”com.pirate.PirateShipEjb”  resource-ref=”true” />  
          12. <jee:jndi-lookup id=”parrot” jndi-name=”pirate/Parrot “ resource-ref=”false” />  
          13.   
          14. </beans>  
           

           

           

          第一個元素 <jee:remote-slsb> 配置了一個叫“ Hispaniola ”的 bean ,它實際上引用的是一個 EJB2 remote stateless session bean 。這里 EJB home interface 可以通過 JNDI 名稱“ java:comp/env/ejb/PirateShip ”找到。屬性“ resource-ref ”表示應該值會自動加上“ java:comp/env/ ”前綴。 EJB 的實現(xiàn)方法定義在 PirateShipEjb 業(yè)務接口中。

           

           

          另一個元素 <jee:jndi-lookup> 用于從 JNDI 獲取對象的引用(它可以是一個 EJB3 session bean 或一個普通 pojo )。對象會在這個叫“ pirate/Parrot ”的 JNDI 中獲得。注意這里我們將“ resource-ref ”屬性配置為“ false ”,所以應該 jndi-name 不會加上“ java:comp/env ”前綴。

          posted on 2008-07-08 23:03 CONAN 閱讀(1009) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 巢湖市| 潞城市| 临朐县| 连城县| 万源市| 宜宾市| 武强县| 禹州市| 依安县| 乐昌市| 安西县| 北海市| 武强县| 广宗县| 临汾市| 体育| 广西| 孙吴县| 沅江市| 师宗县| 水富县| 平舆县| 象州县| 如东县| 左权县| 马关县| 望江县| 河北省| 张家川| 独山县| 宜丰县| 吕梁市| 泉州市| 咸阳市| 保靖县| 玉门市| 达日县| 和龙市| 清原| 北宁市| 泌阳县|