0

@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);
    }

}
preetham
  • 586
  • 5
  • 12
  • You don't. A `web.xml` has to be in a specific location to be picked up when the application is deployed as a war. That simply isn't going to work in a Spring Boot application that is run with the embedded container. – M. Deinum Apr 16 '19 at 06:12

1 Answers1

0

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

Ananthapadmanabhan
  • 4,089
  • 5
  • 17
  • 31