`

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目录下

分享到:
评论

相关推荐

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

    web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者...

    JAVA web.xml配置详解

    --small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或.jpg. --&gt; 路径/small.gif &lt;!--large-icon元素应指向web站台中某个大图表路径,大小...

    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" ...

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: &lt;?xml...

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

    这个简单的例子演示了在 web 应用程序中使用的 Spring 容器,即org.springframework.web.context.WebApplicationContext Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context...

    spring和hibernate整合

    在web.Xml中 &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.spring...

    MyContextLoaderPlugIn.jar

    和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-config.xml中使用MyContextLoaderPlugIn.jar包中...

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

    在使用时,只需将其配置在项目的web.xml文件中,其配置代码如下: &lt;servlet&gt; &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt; &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt; ...

    Spring MVC 入门实例

    上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. applicationContext.xml: 1 &lt;?xml version="1.0" encoding=...

    SSH第7章上机.zip ACCP8.0

    在web.xml中配置struts &lt;filter-name&gt;struts2 &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter &lt;filter-name&gt;struts2 &lt;url-pattern&gt;/* 在web.xml配置spring &lt;!-- ...

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

    Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; ...

    企业人力资源管理项目SSH+EXT+MySQL+MD5

    list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:...

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

    2.web工程的web.xml中配置CXFServlet &lt;!-- 设置Spring容器加载配置文件路径 --&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-server.xml &lt;listener-class&gt;org....

    springmybatis

    1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...

    Spring MVC Demo

    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;!--servlet的参数配置,查找controller位置的xml文件配置,此参数指定了spring配置文件的位置 ,如果你不指定的话,默认会查找 */WEB-INF...

    springmvc小项目

    springmvc比较全面的教程,聚集了一般项目中会用到的注释控制之流。在web.xml中切换contextConfigLocation文件可以访问不同控制器。

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

     本文选用的数据库为Oracle 9i,当然你可以在不改动代码的情况下,通过配置文件的调整将其移植到任何具有Blob字段类型的数据库上,如MySQL,SQLServer等。  总体实现  上传文件保存到T_FILE表中,T_FILE表结构...

    Spring的web配置

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

    spring_MVC源码

    09. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 10. &lt;/listener&gt; 11. 12. &lt;servlet&gt; 13. &lt;servlet-name&gt;spring&lt;/servlet-name&gt; 14. &lt;servlet-class&gt;org.spring...

    Spring整合SpringMVC(毕业设计)

    – 第一步:编写springmvc-servlet.xml文件 – 第二步:编写web.xml文件 ...org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml

Global site tag (gtag.js) - Google Analytics