1. 程式人生 > >(轉)web.xml中的contextConfigLocation在spring中的作用

(轉)web.xml中的contextConfigLocation在spring中的作用

param 不存在 new xml配置 prope alt 圖片 變量 -s

一、Spring如何使用多個xml配置文件

  1、在web.xml中定義contextConfigLocation參數,Spring會使用這個參數去加載所有逗號分隔的xml文件,如果沒有這個參數,Spring默認加載web-inf/applicationContext.xml文件。

  例如:

技術分享圖片
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:conf/spring/applicationContext_core*.xml,
        classpath*:conf/spring/applicationContext_dict*.xml,
        classpath*:conf/spring/applicationContext_hibernate.xml,
    </param-value>
</context-param> 
技術分享圖片

  contextConfigLocation 參數的<param-value>定義了要裝入的 Spring 配置文件。

原理:利用ServletContextListener 實現

  Spring 提供ServletContextListener 的一個實現類ContextLoaderListener ,該類可以作為listener 使用,它會在創建時自動查找WEB-INF/ 下的applicationContext.xrnl 文件。因此,如果只有一個配置文件,並且文件名為applicationContext.xml ,則只需在web.xml文件中增加如下代碼即可:

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

  如果有多個配置文件需要載入,則考慮使用<context-param>元素來確定配置文件的文件名。由於ContextLoaderListener加載時,會查找名為contextConfigLocation的參數。因此,配置<context-param>時參數名字應該是contextConfigLocation。

  帶多個配置文件的web.xml 文件如下:

技術分享圖片
<1-- XML 文件的文件頭二〉
<web-app>
  <!一確定多個配置文件>
  <context-param>
    <1-- 參數名為contextConfigLocation…〉
    <param-name>contextConfigLocation</param-name>
    <!一多個配置文件之間以,隔開二〉
    <param-value>/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <!-- 采用listener創建ApplicationContext 實例-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>
技術分享圖片

  如果沒有contextConfigLocation 指定配置文件,則Spring 自動查找applicationContext.xml 配置文件。如果有contextConfigLocation,則利用該參數確定的配置文件。該參數指定的一個字符串,Spring 的ContextLoaderListener 負責將該字符串分解成多個配置文件,逗號","、空格" "及分號";"都可作為字符串的分割符。如果既沒有applicationContext.xml 文件,也沒有使用contextConfigLocation參數確定配置文件,或者contextConfigLocation確定的配置文件不存在。都將導致Spring 無法加載配置文件或無法正常創建ApplicationContext 實例

配置一個spring為加載而設置的servlet可以達到同樣效果.

  采用load-on-startup Servlet 實現。
  Spring 提供了一個特殊的Servllet 類: ContextLoaderServlet。該Servlet 在啟動時,會自動查找WEB-IN日下的applicationContext. xml 文件。當然,為了讓ContextLoaderServlet 隨應用啟動而啟動,應將此Servlet 配置成load-on-startup 的Servleto load-on-startup 的值小一點比較合適,因為要保證ApplicationContext 優先創建。如果只有一個配置文件,並且文件名為applicationContext. xml ,則在web.xml 文件中增加如下代碼即可:

<servlet>
     <servlet-name>context</servlet-arne>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
     <load-on-startup>l</load-o 口-startup>
</servlet>

  帶多個配置文件的web.xml 文件如下:

技術分享圖片
<web-app>
  <‘一確定多個配置文件一>
  <context-param>
    <!-- 參數名為contextConfigLocation-->
    <param-name>contextConfigLocation</param-name>
    <!-- 多個配置文件之間以,隔開一〉
    <param-value>/WEB-工NF/daoContext.xml,/WEB-工NF/applicationContext.xml</param-value>
  </context-param>
  <!一采用load-on-startup Servlet 創建Applicat工onContext 實例一〉
  <servlet>
    <servlet-narne>context</servlet-narne>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <!一下面值小一點比較合適,會優先加載一〉
    <load-on-startup>l</load-on-startup>
  </servlet>
</web-app>
技術分享圖片

二、使用匹配符

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

  比如說用到Hibernate,則把hibernate相關的配置放在applicationContext-hibernate.xml這一個文件,而一些全局相關的信息則放在applicationContext.xml,其他的配置類似.這樣就可以加載了,不必寫用空格或是逗號分開!

三、如果使用Struts加載多個Spring配置文件

  下面這個配置的其實也是contextConfigLocation變量.struts-config.xml裏面加這個

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,,,,,,,"/>

四、如果是非j2ee應用程序加載

技術分享圖片
ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf = (BeanFactory)reg;

(轉)web.xml中的contextConfigLocation在spring中的作用