0

I am using Spring boot 2.6.3 and I have the following web.xml in my project. What would be the ideal way to move this to Java config?

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>My Web Application</display-name>
    <servlet>
        <servlet-name>my</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.xyz.my.service.custom.config.CustomServiceConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>my</servlet-name>
        <url-pattern>/v1/</url-pattern>
    </servlet-mapping>
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Saif
  • 2,530
  • 3
  • 27
  • 45
  • 1
    Ditch it. Put `@Import(CustomServiceConfig.class)` on your `@SpringBootApplication` annotated class, or make sure your `@SpringBootApplication` annotated class is in the `com.xyz.my.service` package (then you don't need to do anything). – M. Deinum Mar 09 '22 at 07:07
  • 1
    You don't need them with Spring Boot. The `AnnotationConfigWebApplicationContext` is the default and your configuration class just can be imported or automatically detected. – M. Deinum Mar 09 '22 at 07:15
  • You are too fast. XD – Saif Mar 09 '22 at 07:16

1 Answers1

1

You don't need all of that. Just ditch your web.xml. The AnnotationConfigWebApplicationContext (actually Spring Boot has a specialized sub-class for it) is the default and your configuration class just can be imported or automatically detected

If your @SpringBootApplication annotated class is in the com.xyz.my.service you don't need to do anything. Your CustomServiceConfig will be automatically detected. If it isn't you can add an @Import(CustomServiceConfig.class) to your @SpringBootApplication annotated class.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224