Issue
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>
Solution
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.
Answered By - M. Deinum
Answer Checked By - Robin (JavaFixing Admin)