`
Sev7en_jun
  • 浏览: 1213028 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
84184fc0-d0b6-3f7f-a3f0-4202acb3caf5
Apache CXF使用s...
浏览量:109907
社区版块
存档分类
最新评论

web.xml 通过contextConfigLocation配spring 的方式

 
阅读更多

 web.xml 通过contextconfiglocation配置spring 的方式
ssi框架配置文件路径问题:

struts2的 1个+n个  路径:src+src(可配置)      名称: struts.xml  + n
spring 的 1个           路径: src                          名称: applicationcontext.xml
ibatis 的 1个+n个  路径: src+src(可配置)     名称: sqlmapconfig.xml + n


部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

spring的 配置文件在启动时,加载的是web-info目录下的applicationcontext.xml,
运行时使用的是web-info/classes目录下的applicationcontext.xml。

配置web.xml使这2个路径一致:


<context-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>/web-inf/classes/applicationcontext.xml</param-value>
</context-param>

多个配置文件的加载,以逗号隔开
<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,
            classpath*:conf/spring/applicationcontext_staff*.xml,
            classpath*:conf/spring/applicationcontext_security.xml
            classpath*:conf/spring/applicationcontext_modules*.xml,
            classpath*:conf/spring/applicationcontext_cti*.xml,
            classpath*:conf/spring/applicationcontext_apm*.xml
        </param-value>
    </context-param>

contextconfiglocation 参数定义了要装入的 spring 配置文件。



首先与spring相关的配置文件必须要以"applicationcontext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。
还有最好把所有spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。
在web.xml中的配置如下:
xml代码
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath*:**/applicationcontext-*.xml</param-value> 
</context-param>

"**/"表示的是任意目录;
"**/applicationcontext-*.xml"表示任意目录下的以"applicationcontext-"开头的xml文件。
你自己可以根据需要修改。最好把所有spring配置文件都放在一个统一的目录下,如:

<!-- spring 的配置 -->
<context-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>classpath:/spring/applicationcontext-*.xml</param-value>
</context-param>



web.xml中classpath:和classpath*:, 有什么区别?



classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

分享到:
评论
2 楼 changxianbest 2013-04-19  
总结得很好 
1 楼 wpf523 2012-07-31  
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans-*.xml</param-value>
</context-param>
约定优于配置,恩

相关推荐

    JAVA web.xml配置详解

    &lt;param-value&gt;/WEB-INF/spring-config.xml &lt;!-- 备注:此所设定的参数,在JSP网页中可以使用下列方法来取得:${initParam.param_name} 若在Servlet可以使用下列方法来获得:String param_name=getServletContext...

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

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

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

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

    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和hibernate整合

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

    springweb3.0MVC注解(附实例)

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

    MyContextLoaderPlugIn.jar

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

    基于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; ...

    基于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 入门实例

    如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用...

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

    struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml ...

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

    Spring MVC Demo

    本工程主要是一个练手,里面对web.xml和applicationContext-base.xml有一个详细的说明。 &lt;servlet&gt; &lt;servlet-name&gt;Dispatcher &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;!--...

    spring_MVC源码

    -- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller --&gt; 19. *.do&lt;/url-pattern&gt; 20. &lt;/servlet-mapping&gt; 21. &lt;welcome-file-list&gt; 22. &lt;welcome-file&gt;index.jsp...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    SSH第7章上机.zip ACCP8.0

    在web.xml配置spring &lt;!-- 配置spring的配置文件的位置 --&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-*.xml &lt;!-- 配置spring随web容器启动时就创建 --&gt; ...

    Spring MVC 框架应用实例

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

    ssh框架的搭建

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

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

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    DOS命令使用方法(超全).

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;!-- 利用spring监听 编码设置 --&gt; &lt;filter-name&gt;SpringCharacterEncodingFilter &lt;filter-class&gt;org.springframework.web....

Global site tag (gtag.js) - Google Analytics