java - Spring Boot 应用程序在 web.xml 中启动应用程序初始化 servlet
问题描述
@Given Web.xml 带有所有 servlet 映射和 contextConfigLocations 以加载 spring bean。
<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>SpringBootAppWithWebxml</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/spring-context-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sampleServlet</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sampleServlet</servlet-name>
<url-pattern>/sample/*</url-pattern>
</servlet-mapping>
</web-app>
如何使用 SpringBoot 应用程序加载这个 web.xml 和相应的 servlet 和 contextConfig?
Web.xml 位于模块 A 中,调用应用程序 B 将模块 A 作为依赖项。
我的 springBootapp 是
@SpringBootApplication(scanBasePackages = {"com.test.package"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class StarterApp{
public static void main(String[] args) {
SpringApplication.run(StarterApp.class);
}
}
解决方案
Spring-boot 通常更喜欢基于注释的配置而不是基于 xml 的配置。默认情况下,Springweb.xml
通常会存在,src/main/webapp/WEB-INF
并且会被 spring 自动占用。
请参阅此帖子以获取更多详细信息:
推荐阅读
- git - 如何在某些特定提交之后划分一长串提交?
- python - 我可以从 python 'line by line' 登录到 mongoDB 吗?
- angular - 即使输入未更改,DOM 也会在更改检测周期期间使用 OnPush 策略更新视图
- c# - 根据模式从字符串中提取值
- google-cloud-platform - 谷歌云计算引擎随机变得无法访问
- iis - IIS 使用查询字符串的通配符重写映射
- java - 如何在我的程序中动态添加 TextFields?
- automake - 通过 Automake 创建数据文件
- google-cloud-platform - 存储桶网站前 GCP 负载平衡的每秒请求限制
- c - 在 C 中排序和分析数组