简体   繁体   中英

Spring Boot application to start application initializing servlets in web.xml

@Given Web.xml with all servlet mappings and contextConfigLocations to load spring beans.

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

How to load this web.xml and corresponding servlets and contextConfig's using SpringBoot application?

Web.xml resides in module A, the calling application B has module A as dependency.

My springBootapp is

@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 normally prefers annotation based configurations over xml based configurations. By default the Spring web.xml will usually live in src/main/webapp/WEB-INF and it will be automatically taken up by spring.

Please refer this post for more details:

DispatcherServlet and web.xml in Spring Boot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM