这篇文章给大家分享的是有关弹簧使用多个xml配置文件的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
1,在网络。xml中定义contextConfigLocation参数.spring会使用这个参数加载。所有逗号分割的xml。如果没有这个参数,春天默认加载- inf/中文件。
& lt; context-param> & lt; param-name> contextConfigLocation & lt; 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 & lt;/context-param>
contextConfigLocation参数定义了要装入的春天配置文件。原理说明如下:
1。利用ServletContextListener实现。
春提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作
为侦听器使用,它会在创建时自动查找- inf/下的applicationContext。xrnl文件。因
此,如果只有一个配置文件,并且文件名为applicationContext。xml,则只需在网络。xml
文件中增加如下代码即可:
& lt; listener> & lt; listener-class> org.springframework.web.context.ContextLoaderListener & lt;/listener>
如果有多个配置文件需要载入,则考虑使用& lt; context-para即元素来确定配置文件的
文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。
因此,配置context-param时参数名字应该是contextConfigLocation。
带多个配置文件的网络。xml文件如下:
& lt; 1——, XML 文件的文件头二> & lt; ? XML version=發.O",编码=肮?0 - 8859 - 1 -“?比; & lt;时间1——大敌;;web.xm1 文件的DTD 等信息一> & lt; ! DOCTYPE web PUBLIC “-//Sun 微系统只工口c//DTD Web Application 2.3//EN" & # 39; & # 39; http://java.sun.com/dtd/web-app_2_3.dtd& # 39; & # 39;比; & lt; web-app> & lt; !一确定多个配置文件比; 1——& lt; context-param> & lt;参数名为contextConfigLocation…> & lt; param-name> contextConfigLocation & lt; !一多个配置文件之间以,隔开二> & lt; param-value>/WEB -工NF daoContext.xml./WEB - inf/applicationContext.xml & lt;/context-param> & lt; !——,采用听众创建众多工onContext 实例——比; & lt; listener> & lt; listener-class> org.spr工ngframework.web.context.ContextLoader Listener & lt;/listener> & lt;/web-app>
如果没有contextConfigLocation指定配置文件,则弹簧自动查找
应用程序上下文。xrnl配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。
该参数指定的一个字符串,春天的ContextLoaderListener负责将该字符串分解成多个
配置文件,逗号“、“空格“;“及分号“,“都可作为字符串的分割符。
如果既没有applicationContext。xrnl文件,也没有使用contextConfigLocation参数确
定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致春天无法
加载配置文件或无法正常创建ApplicationContext实例
配置一个春天为加载而设置的servlet可以达到同样效果。
采用load-on-startup Servlet实现。
春提供了一个特殊的Servllet类:ContextLoaderServlet。该Servlet在启动时,会
自动查找网络日下的applicationContext。xml文件。
当然,为了让ContextLoaderServlet随应用启动而启动,应将此Servlet配置成
load-on-startup的Servleto load-on-startup的值小一点比较合适,因为要保证应用
环境优先创建。如果只有一个配置文件,并且文件名为applicationContext。xml,则在
web。xml文件中增加如下代码即可:
& lt; servlet> & lt; servlet-name> context & lt; servlet-class> org.springframework.web.context.ContextLoaderServlet & lt; load-on-startup> l</load-on-startup> & lt;/servlet>
。带多个配置文件的网络且问文件如下:
& lt; !——, XML 文件的文件头——祝辞& lt; ? XML version=?.0“,编码=肮? 8859 - 1 -“?比; & lt; !, -, web.xml 文件的DTD 等信息→& lt; ! 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;祝辞& lt; web-app> & lt; & # 39;一确定多个配置文件一祝辞& lt; context-param> & lt; !——,参数名为contextConfigLocation——祝辞& lt; param-name> contextConfigLocation春天使用多个xml配置文件的方法