Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

Spring中,applicationContext.xml 配置文件在web.xml中的配置详解一、首先写一下代码结构。二、再看web.xml中的配置情况。<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:s…

大家好,又见面了,我是你们的朋友全栈君。

一、首先写一下代码结构。

Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

 

二、再看web.xml中的配置情况。

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
     <servlet> 
        <servlet-name>springmvc</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> -->
           <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup> 
    </servlet>  
    <servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
</web-app>

复制代码

 

三、下面是对配置文件的说明。

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

  <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>

这段配置是用于指定applicationContext.xml配置文件的位置,可通过context-param加以指定:

这里需要搞清楚classpath是什么,以及classpath:和classpath*有何区别:

1. 首先 classpath是指 WEB-INF文件夹下的classes目录

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

 

如果applicationContext.xml配置文件存放在src目录下,就好比上面的代码结构中的存放位置,那么在web.xml中的配置就如下所示:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

如果applicationContext.xml配置文件存放在WEB-INF下面,那么在web.xml中的配置就如下所示:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext*.xml</param-value>
    </context-param>

需要注意的是,部署到应用服务器后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下,spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml, 运行时使用的是web-info/classes目录下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目录下,还是存放在WEB-INF下面,都可以用下面这种方式来配置路径:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/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,
            ......
        </param-value>
    </context-param>

复制代码

也可以用下面的这种方式:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:**/applicationContext-*.xml</param-value>
    </context-param>

“**/”表示的是任意目录; 

“**/applicationContext-*.xml”表示任意目录下的以”applicationContext-“开头的XML文件。 

Spring配置文件最好以”applicationContext-“开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160375.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月16日 上午6:16
下一篇 2022年7月16日 上午6:16


相关推荐

  • linux常用命令杀死进程_结束进程的命令

    linux常用命令杀死进程_结束进程的命令原文网址:简介法1:ps+grep等用法ps-ef|grepprocedure_name|grep-vgrep|awk'{print$2}’|xargskill-9procedure_name为进程名。分析ps-ef 列出所有进程 grepprocedure_name 查找指定进程名的进程 awk'{print$2}’ 筛选出进程的ID xargskill 杀死指定进程 法2:killall用法

    2026年4月15日
    5
  • 未来之路作为创业者_如何看待读图时代

    未来之路作为创业者_如何看待读图时代距离4月11日-14日百度联盟峰会已经过去一个多月了,这一段与许多站长谈论最多的是百度创始人李彦宏在峰会上的演讲,其中创业者三大机会尤最。演讲更多的是从战略角度的高度概括,因此笔者主要想在大家的帮助下再深入分析一下读图时代的创业机会,主要是交流,通过交流更具体一些。在分析、交流之前,还是先引用一些媒体报道,以免失之毫厘,谬以千里。    4月12日上午消息,百度公司…

    2025年10月18日
    5
  • 卸载Symantec Endpoint Protection, 无需password的卸载方法

    卸载Symantec Endpoint Protection, 无需password的卸载方法

    2021年12月3日
    114
  • 简单分析RLP编码原理

    简单分析RLP编码原理RLP 编码是以太坊数据序列化的主要方法 本文介绍 RLP 编码的主要规则和原理分析 RLP 编码具有较好的数据处理效率 尤其是将长度和类型统一作为前缀 实际上 RLP 是基于 ASCII 编码的一种结构化扩充 既能表示长度还能表示类型 是一种非常紧凑的结构化编码方案 RLP RecursiveLen 递归长度前缀 是一种编码算法 用于编码任意的嵌套结构的二进制数据 它是以太坊中数据

    2026年3月17日
    2
  • Oracle数据库运维常用Sql

    Oracle数据库运维常用Sql1 查看可以收缩的数据文件 select fromdba free spacewherefi idin selectfile idfromdba free spacegroupby idhavingcoun 0 1 select fromdba data fileswherefi idin selectfile id

    2026年3月8日
    3
  • JavaFX Script语言教程「建议收藏」

    JavaFX Script语言教程「建议收藏」JavaFXScript™(下文中称为JavaFX)语言是一种声明式的静态类型编程语言。它具有第一级函数(first-classfunctions)、声明式的语法、列表推导(list-comprehensions)及基于依赖关系的增量式求值(incrementaldependency-basedevaluation)等特征。JavaFX脚本式语言特别适用于Java2Dswi…

    2022年6月21日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号