2009-03-01 102 views
55

將Spring的配置拆分爲多個xml文件的正確方法是什麼?將applicationContext拆分爲多個文件

目前,我有

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

web.xml有以下幾點:

<servlet> 
    <description>Spring MVC Dispatcher Servlet</description> 
    <servlet-name>intrafest</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/foo-*.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
</servlet> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
      /WEB-INF/foo-*.xml 
    </param-value> 
</context-param> 


<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

個實際的問題:

  • 是這種方法正確/最佳
  • 我真的需要指定DispatcherServletcontext-param部分中的配置位置嗎?

我需要記住什麼才能引用foo-servlet.xml中定義的豆類從foo-service.xml?這是否與在web.xml中指定contextConfigLocation有關?

更新1:

我使用框架3.0。這是我的理解,我不需要做這樣的資源導入:

<import resource="foo-services.xml"/> 

這是一個正確的假設?

回答

47

我發現下面的設置最簡單。

使用的DispatcherServlet默認的配置文件加載機制:

該框架將,在DispatcherServlet的初始化過程 ,尋找一個 文件名爲[servlet的名稱] -servlet.xml後綴 在WEB -INF目錄中,並創建在此定義的bean (覆蓋全局範圍內使用 定義的所有bean的定義)。

在你的情況下,簡單地創建在WEB-INF目錄文件intrafest-servlet.xml,不需要在web.xml指定任何特定的信息。您可以使用import來組成您的XML配置。

<beans> 
    <bean id="bean1" class="..."/> 
    <bean id="bean2" class="..."/> 

    <import resource="foo-services.xml"/> 
    <import resource="foo-persistence.xml"/> 
</beans> 

請注意,Spring團隊實際上更喜歡在創建(Web)ApplicationContext時加載多個配置文件。如果您仍想這樣做,我認爲您不需要同時指定上下文參數(context-param servlet初始化參數(init-param)。其中一個會做。您還可以使用逗號來指定多個配置位置。

+0

+1 - 我的設置看起來就像這樣。雖然我認爲這個設置與在web.xml中指定多個配置文件沒有任何實際的優勢/劣勢 - 它看起來像語義 – 2009-03-04 04:48:02

+1

我絕對認爲默認配置是有利的:約定優於配置。而不是用* extra config *指定多個配置文件,你只有一個* default *「頂級」配置文件,它將導入那些你必須指定的相同文件。 – eljenso 2009-03-04 08:12:04

+1

當你使用導入時要小心,不要多次導入同一個文件,(導入導入等等),因爲它會導致創建多個bean,並且會導致很難發現錯誤。 – 2011-01-07 10:30:57

6

@eljenso:intrafest-servlet.xml如果應用程序使用SPRING WEB MVC,將使用web應用程序上下文xml。

否則@kosoant配置沒問題。

簡單的例子,如果你不使用Spring Web MVC框架,但要utitlize SPRING IOC:

在web.xml:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:application-context.xml</param-value> 
</context-param> 

然後,您的應用程序的context.xml將包含:<import resource="foo-services.xml"/> 這些導入語句加載各種應用程序上下文文件並放入主應用程序context.xml中。

謝謝,並希望這有助於。

25

邁克Nereson有這個在他的博客上說:

http://blog.codehangover.com/load-multiple-contexts-into-spring/

有幾個方法可以做到這一點。

1. web.xml中的contextConfigLocation

你的第一個選項是通過contextConfigLocation的元素將它們全部加載到你的Web應用程序 上下文。假設您正在編寫一個Web應用程序 ,那麼您已經開始 的主要應用程序上下文了。您只需在 聲明下一個上下文之間放置一些空白區域即可。

<context-param> 
     <param-name> contextConfigLocation </param-name> 
     <param-value> 
      applicationContext1.xml 
      applicationContext2.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

以上使用回車。或者,喲可以放在 的空間。

<context-param> 
     <param-name> contextConfigLocation </param-name> 
     <param-value> applicationContext1.xml applicationContext2.xml </param-value> 
    </context-param> 

    <listener> 
     <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener> 

2. applicationContext.xml的進口來源

你的其他選擇是僅主的applicationContext.xml 添加到web.xml,然後在主環境使用import語句。

applicationContext.xml你可能有...

<!-- hibernate configuration and mappings --> 
    <import resource="applicationContext-hibernate.xml"/> 

    <!-- ldap --> 
    <import resource="applicationContext-ldap.xml"/> 

    <!-- aspects --> 
    <import resource="applicationContext-aspects.xml"/> 

你應該使用哪種策略?

我總是喜歡通過web.xml中加載了。

因爲,這允許我保持與其他每個 隔離的所有上下文。通過測試,我們可以只加載我們需要運行這些測試的上下文。這使得開發更加模塊化,因爲組件 保持爲loosely coupled,所以將來我可以提取包 或垂直層並將其移至其自己的模塊。

2.如果您正在將上下文加載到non-web application中,我將使用import資源。

11

有兩種類型的上下文我們正在處理:

:(父上下文典型地包括所有JDBC(ORM,休眠)初始化和其它彈簧安全相關的配置)根上下文

:單獨的servlet上下文(子上下文)。通常調度器Servlet上下文並初始化與spring-mvc(控制器,URL映射等)相關的所有bean)。

這裏是web.xml中的一個例子,它包括多個應用程序上下文文件

<?xml version="1.0" encoding="UTF-8"?> 
 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
 

 
    <display-name>Spring Web Application example</display-name> 
 

 
    <!-- Configurations for the root application context (parent context) --> 
 
    <listener> 
 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 
    </listener> 
 
    <context-param> 
 
     <param-name>contextConfigLocation</param-name> 
 
     <param-value> 
 
      /WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context --> 
 
      /WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context --> 
 
     </param-value> 
 
    </context-param> 
 

 
    <!-- Configurations for the DispatcherServlet application context (child context) --> 
 
    <servlet> 
 
     <servlet-name>spring-mvc</servlet-name> 
 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 
     <init-param> 
 
      <param-name>contextConfigLocation</param-name> 
 
      <param-value> 
 
       /WEB-INF/spring/mvc/spring-mvc-servlet.xml 
 
      </param-value> 
 
     </init-param> 
 
    </servlet> 
 
    <servlet-mapping> 
 
     <servlet-name>spring-mvc</servlet-name> 
 
     <url-pattern>/admin/*</url-pattern> 
 
    </servlet-mapping> 
 

 
</web-app>

1

我的modular-spring-contexts作者。

這是一個小型實用程序庫,它允許使用比使用Composing XML-based configuration metadata時獲得的更多模塊化彈簧環境組織。 modular-spring-contexts通過定義模塊工作,這些模塊基本上是獨立的應用程序上下文,並允許模塊從其他模塊導入Bean,這些模塊將在其原始模塊中導出。

要點然後超過

  • 控制模塊之間的依賴關係

    • 控制哪些豆出口,並且其中它們被用於命名豆碰撞的
    • 可能性減小

    一個簡單的例子看起來像這樣:

    文件moduleDefinitions.xml

    <?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:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd 
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
    
        <context:annotation-config /> 
    
        <module:module id="serverModule"> 
         <module:config location="/serverModule.xml" /> 
        </module:module> 
    
        <module:module id="clientModule"> 
         <module:config location="/clientModule.xml" /> 
         <module:requires module="serverModule" /> 
        </module:module> 
    
    </beans> 
    

    文件serverModule.xml

    <?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:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd 
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
    
        <context:annotation-config /> 
    
        <bean id="serverSingleton" class="java.math.BigDecimal" scope="singleton"> 
         <constructor-arg index="0" value="123.45" /> 
         <meta key="exported" value="true"/> 
        </bean> 
    
    </beans> 
    

    文件clientModule.xml

    <?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:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd 
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
    
        <context:annotation-config /> 
    
        <module:import id="importedSingleton" sourceModule="serverModule" sourceBean="serverSingleton" /> 
    
    </beans>