作者簡介:
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 version=”1.0” encoding=”UTF-8”?>
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- <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>>
- <!-- place configuration details here -->
- </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 version=”1.0” encoding=”UTF-8”?>
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>
- <bean id=”pirate” class=”Pirate”>
- <constructor-arg value=”Long John Silver” />
- <property name=”map” ref=”treasureMap” />
- </bean>
- <bean id=”treasureMap” class=”TreasureMap” />
- </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> 元素將它們全部組裝起來:
- <import resource=”service-layer-config.xml” />
- <import resource=”data-layer-config.xml” />
- <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 version=”1.0” encoding=”UTF-8”?>
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xmlns:context=”http://www.springframework.org/schema/context”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- <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>
- <A href="http://www.springframework.org/schema/context" target=_blank>http://www.springframework.org/schema/context</A>
- <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>”>
- <context:component-scan base-package=”com.springinac¬tion.service” />
- </beans>
在上面的配置文件中, <context:component-scan> 元素會自動掃描“ com.springinaction.service ”包下的類,并自動將那些標記有 @Component, @Controller, @Repository, @Service 或 @Aspect. 的類全部注冊到 Spring 容器中。
溫馨提示:
盡量為你的最終用戶提供外部配置文件。
將所有的配置都定義在 Spring 配置文件中并不推薦。你根本別指望應用程序的管理員或最終用戶會去研究 Spring XML 然后搞懂數據庫的配置和其它特定布署細節(jié),你不會真打算靠他們吧?相反,使用外部配置文件,我們可以使用 <context:property-placeholder> 這么來做:
- <context:property-placeholder location=”file:////etc/pirate.properties”
定義在 /etc/pirate.properties 屬性文件中的那些鍵值對現(xiàn)在可以在 Spring 配置文件中大顯身手啦:
- <bean id=”pirate” class=”Pirate”>
- <constructor-arg value=”${pirate.name}” />
- </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 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”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- <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>
- <A href="http://www.springframework.org/schema/aop" target=_blank>http://www.springframework.org/schema/aop</A>
- <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>”>
- <bean id=”pirateTalker” class=”PirateTalker” />
- <aop:config>
- <aop:pointcut id=”plunderPointcut” expression=”execution(* *.plunder(..))” />
- <aop:aspect ref=”pirateTalker”>
- <aop:before pointcut-ref=”plunderPointcut” method=”sayAvastMeHearties” />
- <aop:after-returning pointcut-ref=”plunderPointcut” method=”sayYarr” />
- </aop:aspect>
- </aop:config>
- </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 version=”1.0” encoding=”UTF-8”?>
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xmlns:jee=”http://www.springframework.org/schema/jee”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- <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>
- <A href="http://www.springframework.org/schema/jee" target=_blank>http://www.springframework.org/schema/jee</A>
- <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>”>
- <jee:remote-slsb id=”hispaniola” jndi-name=”ejb/PirateShip” business-interface=”com.pirate.PirateShipEjb” resource-ref=”true” />
- <jee:jndi-lookup id=”parrot” jndi-name=”pirate/Parrot “ resource-ref=”false” />
- </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 ”前綴。