首頁  >  文章  >  後端開發  >  詳細介紹Spring使用多個xml設定檔的程式碼實例

詳細介紹Spring使用多個xml設定檔的程式碼實例

黄舟
黄舟原創
2017-03-08 16:53:512528瀏覽

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,
classpath*:conf/spring/applicationContext_staff*.xml,classpath*:conf/spring/applicationContext_security.xml
classpath*:conf/spring/applicationContext_modules*.xmlclasspath*:conf/spring/applicationContext_cti*.xml
classpath*:conf/spring/applicationContext_apm*.xml</param-value>
</context-param>

contextConfigLocation 參數定義了要裝入的Spring 設定檔。原理說明如下:

1.利用ServletContextListener 實作。
Spring 提供ServletContextListener 的實作類別ContextLoaderListener ,該類別可以作

為listener 使用,它會在建立時自動尋找WEB-INF/ 下的applicationContext.xrnl 檔案。因
此,如果只有一個設定文件,且檔案名稱為applicationContext.xml ,則只需在web.xml

檔案中增加如下程式碼即可:

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

如果有多個設定檔需要載入,則考慮使用檔名。由於ContextLoaderListener載入時,會尋找名為contextConfigLocation的參數。

因此,設定context-param時參數名稱應該是contextConfigLocation。
帶有多個設定檔的web.xml 檔案如下:

<1-- XML 文件的文件头二〉<?xml version="l.O" encoding="工80-8859-1"?>
< 1-- web.xm1 文件的DTD 等信息一〉<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN"&#39;&#39;http://java.sun.com/dtd/web-app_2_3.dtd&#39;&#39;>
<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创建Applicat工onContext 实例-->
<listener><listener-class>org.spr工ngframework.web.context.ContextLoader
Listener</listener-class></listener></web-app>

如果沒有contextConfigLocation 指定設定文件,則Spring 自動尋找application
Context. xrnl 設定檔。如果有contextConfigLocation,則利用該參數決定的設定檔。

此參數指定的一個字串, Spring 的ContextLoaderListener 負責將該字串分解成多個
配置文件,逗號","、空格" "及分號";"都可作為字符串的分割符。

如果既沒有applicationContext. xrnl 文件,也沒有使用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 的值小一點比較合適,因為要保證Application
Context 優先創建。如果只有一個設定文件,且檔案名稱為applicationContext. xml ,則在

web.xml 檔案中增加如下程式碼即可:

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

。帶有多個設定檔的web且nl 檔如下:

<!-- XML 文件的文件头--><?xml version="1.0" encoding="工SO-8859-1"?>
<! -- web.xml 文件的DTD 等信息→<!DOCTYPE web-appPUBLIC "-//Sun Microsystems , 工口c.//DTD Web Application 2.3//EN"
&#39;&#39;http://java.sun.com/dtd/web-app_2_3.dtd&#39;&#39;><web-app>
<&#39;一确定多个配置文件一><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>

2, 使用匹配符

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

比如說用到Hibernate,則把hibernate相關的設定放在applicationContext- hibernate.xml這一個檔案,而一

些全域相關的資訊則放在applicationContext.xml,其他的設定類似.這樣就可以載入了,不必寫用空格或是逗號分開!

3, 如果使用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,,,,,,,"/>

4,如果是非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;

最後,解釋以下classpath*:與classpath的差異:

classpath*:的出現是為了從多個jar檔案載入相同的檔案.classpath:只能載入找到的第一個檔案.


以上是詳細介紹Spring使用多個xml設定檔的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn