Spring contextConfigLocation

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 參數定義了要裝入的 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-para即元素來確定配置文件的文件名。由於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/application
Context.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.ContextLoader
Servlet</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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章