`

web.xml中通过contextConfigLocation的读取spring的配置文件

阅读更多
公司的考勤系统程序,有5个spring配置文件:bean-edu.xml,bean-pub.xml,db-edu.xml,db-pub.xml,timer-system.xml,均放置于src目录下,在web.xml中配置这些文件的代码如下:
<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/db-pub.xml,
                     classpath:db-edu.xml,
                     classpath:bean*.xml,
                     classpath*:timer-system.xml
        </param-value>
</context-param>

注意:部署程序启动tomcat之后,log4j显示出
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-pub.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-edu.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-edu.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-pub.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  -  Loading XML bean definitions from URL [file:/E:/apache-tomcat-6.0.33-windows-x86/apache-tomcat-6.0.33/webapps/DigitalCampus/WEB-INF/classes/timer-system.xml]




<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/db-pub.xml,
                     classpath:db-edu.xml,
                     classpath*:bean*.xml,
                     /WEB-INF/classes/timer-system.xml
                     <!--  classpath*:timer-system.xml-->
        </param-value>
    </context-param>



[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-pub.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-edu.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-edu.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-pub.xml]
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/timer-system.xml]


根据以上两个例子:
1 classpath和classpath*的区别是:前者from class path resource,后者from URL。classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
2 带不带有/,没有区别。
3 bean*.xml查找的是以bean开头的配置文件,from file
4 classpath*:bean*.xml 为from file.
5   /WEB-INF/classes/timer-system.xml 为from ServletContext resource。



另外:
"**/" 表示的是任意目录;
"**/applicationContext-*.xml" 表示任意目录下的以"applicationContext-"开头的XML文件。
程序部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 WEB-INF/classes目录下

分享到:
评论

相关推荐

    springmvc、spring、mybatis的resources配置文件和web.xml

    Spring框架的核心在于IoC(Inversion of Control)容器,它通过读取XML配置文件来管理对象的生命周期和依赖关系。在Spring的资源配置文件(通常命名为`beans.xml`)中,我们可以定义Bean的实例化、初始化方法、属性...

    SSH和SSI等框架常用基础配置web.xml

    通过`contextConfigLocation`参数指定多个XML配置文件路径,其中`classpath*:`表示从类路径下的指定目录中加载所有符合通配符规则的XML文件。这样,Spring容器在启动时会自动加载这些配置文件并初始化。 #### 三、...

    web.xml中的listen

    - **加载Spring配置文件**:这主要是通过`org.springframework.web.context.ContextLoaderListener`类实现的。该类是Spring框架提供的一个监听器类,用于在Web应用启动时加载Spring的应用上下文(ApplicationContext...

    web.xml中如何设置配置文件的加载路径实例详解

    在web.xml文件中,开发者可以通过"context-param"元素来指定配置文件的路径。 第一种方式是将配置文件放置于类路径(classpath)下,这通常意味着你需要将配置文件放置于src目录下或者在构建过程中打包到类路径指定...

    spring web.xml指定配置文件过程解析

    通过配置web.xml文件,我们可以轻松地配置Spring Web应用程序的核心组件,从而快速搭建Web应用程序。 ContextLoaderListener ContextLoaderListener 是 Spring 框架中的一个核心组件,它负责读取上下文配置文件,...

    Spring 加载多个配置文件

    在Web应用环境中,可以通过配置 `ContextLoaderListener` 来加载多个配置文件。这通常在web.xml中完成,通过 `&lt;context-param&gt;` 元素指定配置文件的位置。例如: ```xml &lt;param-name&gt;contextConfigLocation ...

    ssh框架在application.xml中配置数据源所需jar

    - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

    spring mvc的配置文件详解

    DispatcherServlet 加载时,Spring 会尝试读取相关的配置文件。默认情况下,这些配置文件位于 web.xml 相同的路径下,并且文件名与注册的 Servlet 名相关联,即 `&lt;servlet-name&gt;-servlet.xml`。例如,在本例中,默认...

    struts加载spring的方法

    在Web应用中集成Spring,主要是通过配置`web.xml`文件来完成的。具体步骤如下: 1. **引入Spring的依赖**:首先需要在项目的pom.xml文件或build.gradle文件中添加Spring框架的相关依赖。 2. **配置Spring监听器**...

    spring,struts1.x ,hibernate整合

    1. **在 `web.xml` 中配置监听器**:使用 `ContextLoaderListener` 来初始化 Spring 容器,并指定配置文件的位置。 ```xml &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ...

    配置spring

    这通常通过`&lt;servlet&gt;`标签来完成,其中`servlet-class`是`org.springframework.web.context.ContextLoaderServlet`,并设置`load-on-startup`参数为1,确保在应用启动时加载Spring上下文。 ```xml ...

    spring与axis的整合

    - **Axis配置**:同样在`Web.xml`文件中,通过配置`AxisServlet`实现了Axis Web服务的部署。这里的`load-on-startup`参数指定了Servlet的启动顺序,而`&lt;url-pattern&gt;/services/*&lt;/url-pattern&gt;`则指定了Axis服务的...

    基于Maven 搭建Spring + SpringMVC源码.rar

    Maven是一个项目管理工具,它通过读取项目配置文件(pom.xml)来管理项目的构建、报告和文档。Maven通过定义构建生命周期和插件,使得构建过程标准化,减少了项目间的配置差异。 2. Maven的pom.xml: pom.xml文件...

    S2SH xml配置文件

    - `/WEB-INF/applicationContext*.xml`表示在`WEB-INF`目录下所有以`applicationContext`开头的XML文件,这些文件将被Spring容器读取。 ```xml &lt;listener-class&gt;org.springframework.web.context....

    怎么建立一个资源(配置文件)夹

    - 这里的`*-context.xml`表示所有名称中包含`-context`的XML文件都会被加载为Spring的配置文件。 2. **注册Spring监听器**: - 为了让Spring能够读取到配置文件中的内容,还需要在`web.xml`中注册一个监听器: `...

    struts2+spring3+mybatis3_配置.doc

    2. `&lt;listener&gt;`:声明一个监听器`org.springframework.web.context.ContextLoaderListener`,它会在Web应用启动时初始化Spring的ApplicationContext,读取并加载`contextConfigLocation`指定的配置文件。...

    spring中的所有配置

    这通常在`web.xml`文件中通过`&lt;listener&gt;`标签进行配置: ```xml &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ``` 为了指定Spring配置文件的位置,还需要在`web.xml`中添加一个`...

    Spring框架web项目实战全代码分享

    - `&lt;listener&gt;`:定义了一个`ContextLoaderListener`监听器,当Web应用启动时,它会读取`context-param`中指定的配置文件,初始化Spring应用上下文。 4. **创建`applicationContext.xml`**: 这是Spring的配置...

    Struts+Spring+Ibatis整合框架搭建配置文档

    在`web.xml`中引入Spring配置文件,并通过`ContextLoaderServlet`加载Spring的上下文。这是Spring在Web应用中的核心配置之一。 ```xml &lt;param-name&gt;contextConfigLocation /WEB-INF/conf/applicationContext....

    Spring Web项目spring配置文件随服务器启动时自动加载

    其次,需要在 web.xml 中添加一个上下文参数,以便指定 spring 配置文件的位置。代码如下: ``` &lt;!--配置spring配置文件--&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:spring/spring-context....

Global site tag (gtag.js) - Google Analytics