Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解

简介: Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解sworkorg.

Spring MVC中,applicationContext.xml [ServletName]-servlet.xml配置文件在web.xml中的配置详解

<!-- spring 配置 --><servlet><servlet-name>swork</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>swork</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>

AI 代码解读

这样配置,SpringMVC会自动按照约定去找<servlet-name>swork</servlet-name>的swork-servlet.xml配置文件作为其servlet配置文件.

如果是显式指定Springmvc-servlet.xml, 加上

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

AI 代码解读

如下:

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

AI 代码解读

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

 
AI 代码解读

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

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

AI 代码解读

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>

AI 代码解读

这段配置是用于指定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>

AI 代码解读

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

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

AI 代码解读

需要注意的是,部署到应用服务器后,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>

AI 代码解读

当有多个配置文件加载时,可采用下面代码来配置:

![复制代码](http://upload-images.jianshu.io/upload_images/1233356-61a1170cf5642d48.gif?imageMogr2/auto-orient/strip)
<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>
[图片上传中。。。(4)]

AI 代码解读

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

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

AI 代码解读

"/"表示的是任意目录;
"
/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。
Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。

目录
打赏
0
0
0
0
78
分享
相关文章
|
12天前
|
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
50 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
微服务——SpringBoot使用归纳——Spring Boot事务配置管理——Spring Boot 事务配置
本文介绍了 Spring Boot 中的事务配置与使用方法。首先需要导入 MySQL 依赖,Spring Boot 会自动注入 `DataSourceTransactionManager`,无需额外配置即可通过 `@Transactional` 注解实现事务管理。接着通过创建一个用户插入功能的示例,展示了如何在 Service 层手动抛出异常以测试事务回滚机制。测试结果表明,数据库中未新增记录,证明事务已成功回滚。此过程简单高效,适合日常开发需求。
45 0
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——MyBatis 介绍和配置
本文介绍了Spring Boot集成MyBatis的方法,重点讲解基于注解的方式。首先简述MyBatis作为持久层框架的特点,接着说明集成时的依赖导入,包括`mybatis-spring-boot-starter`和MySQL连接器。随后详细展示了`properties.yml`配置文件的内容,涵盖数据库连接、驼峰命名规范及Mapper文件路径等关键设置,帮助开发者快速上手Spring Boot与MyBatis的整合开发。
61 0
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——依赖导入和Thymeleaf相关配置
在Spring Boot中使用Thymeleaf模板,需引入依赖`spring-boot-starter-thymeleaf`,并在HTML页面标签中声明`xmlns:th=&quot;http://www.thymeleaf.org&quot;`。此外,Thymeleaf默认开启页面缓存,开发时建议关闭缓存以实时查看更新效果,配置方式为`spring.thymeleaf.cache: false`。这可避免因缓存导致页面未及时刷新的问题。
32 0
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
271 0
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
95 0
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
196 0
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
267 0