`
xitongyunwei
  • 浏览: 928507 次
文章分类
社区版块
存档分类
最新评论

Spring contextConfigLocation

 
阅读更多

Spring contextConfigLocation

2012-04-02 01:20:56|分类:Spring|标签:spring工作积累|字号订阅

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;

分享到:
评论

相关推荐

    spring和hibernate整合

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/WEB-INF/spring/*.bean.xml org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework....

    Spring MVC 入门实例

    9 &lt;param-name&gt;contextConfigLocation 10 11 /WEB-INF/database.xml 12 /WEB-INF/applicationContext.xml 13 14 15 16 17 &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener 18 19 20...

    springweb3.0MVC注解(附实例)

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:applicationContext.xml &lt;!-- Spring 容器启动监听器 --&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- ...

    CXF WebService整合Spring示例工程代码demo

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-server.xml &lt;listener-class&gt;org.springframework.web.util.IntrospectorCleanupListener &lt;servlet-name&gt;...

    Spring的web配置

    springmvc的资源配置:Spring 刷新Introspector防止内存泄露,Spring MVC配置;Character Encoding filter; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-*.xml ...

    Spring.html

    --全局初始化参数--&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt; 4.在Servlet中使用WebApplicationContextUtils获取容器...

    spring_MVC源码

    05. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; 06. &lt;param-value&gt;classpath:applicationContext*.xml&lt;/param-value&gt; 07. &lt;/context-param&gt; 08. &lt;listener&gt; 09. &lt;listener-class&gt;org.springframework.web....

    Spring MVC 框架应用实例

    &lt;param-name&gt;contextConfigLocation /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的默认值为WEB-INF/applicationContext.xml 。 我们已将此值定义为classpath:applicationContext.xml 。 这个文件代表了 Spring...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    基于java的企业级应用开发:Spring MVC的核心类和注解.ppt

    * * * * Spring MVC的核心类和...&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:springmvc-config.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;se

    基于java的企业级应用开发:Spring的核心容器.ppt

    spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; ApplicationContext * * * *...

    ssh(structs,spring,hibernate)框架中的上传下载

    Struts+Spring+Hibernate实现上传下载    本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文...

    MyContextLoaderPlugIn.jar

    使用SSH构建项目的时候遇到了contextConfigLocation 和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-...

    Spring MVC Demo

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:applicationContext-*.xml &lt;!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 &lt;param-value&gt;/WEB-INF/mvc.xml,/WEB-INF/mvc1.xml ...

    Spring+mybatis整合实例应用

    项目结构图(Spring3.0.2+mybatis3.0.4)应用数据库对应的实体类数据Dao接口在config目录下...节点配置contextConfigLocation属性是为了让ContextLoaderListener找到Spring上下文的位置并加载它,如果不指定contextConfig

    rural:基于spring的mvc框架,使用简单,URL映射零配置零注解

    ####1 Getting Started #####1.1 Introduction  Rural是一个基于spring的mvc框架,设计上类似spring mvc,相比于spring mvc,Rural使用更简便,无需配置和注解就可以... &lt;param&gt;contextConfigLocation &lt;param&gt;class

    xfire的使用详解

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:spring/app.xml,classpath:org/codehaus/xfire/spring/xfire.xml &lt;!--配置xfire ws--&gt; &lt;servlet-name&gt;XFireServlet &lt;servlet-class&gt;org....

    SSH第7章上机.zip ACCP8.0

    &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-*.xml &lt;!-- 配置spring随web容器启动时就创建 --&gt; &lt;listener-class&gt;org.springframework.web.context....

    ssh框架的搭建

    &lt;param-name&gt;contextConfigLocation /WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml &lt;param-name&gt;log4jConfigLocation &lt;param-value&gt;/WEB-INF/log4j.properties &lt;!-- Listener ...

Global site tag (gtag.js) - Google Analytics