Why should we extend SpringBootServletInitializer
in order to run a SpringBoot
application to a external tomcat
?
If without extending SpringBootServletInitializer
it runs on embedded tomcat then why it is necessary to extendSpringBootServletInitializer
while deploying it to an external tomcat?
This class binds Servlet, Filter and ServletContextInitializer beans from the application context to the server. Extending the SpringBootServletInitializer class also allows us to configure our application when it's run by the servlet container, by overriding the configure() method.
SpringBootServletInitializer is an interface to run SpringApplication from a traditional WAR deployment. It binds Servlet, Filter and ServletContextInitializer beans from the application context to the server.
it can be a common expectation for the application to run on an external tomcat server. usually, it happens because of the operational infrastructure. at this point, some problems come. your spring boot application won't start by simply deploying it to the web server .
Older Servlet containers don’t have support for the ServletContextInitializer bootstrap process used in Servlet 3.0. You can still use Spring and Spring Boot in these containers but you are going to need to add a web.xml to your application and configure it to load an ApplicationContext via a DispatcherServlet.
Inorder to create deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container. Typically, you update your application’s main class to extend SpringBootServletInitializer.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
You can refer below link
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html
Here is some more information :
In order to deploy a Servlet
based web application(like Spring
) you actually need to provide traditional web.xml
.
We can also do the same thing programatically using WebApplicationInitializer interface. As per the docs
Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.
As SpringBoot suggests to use JavaConfiguration
over xml configuration
.
It uses JavaConfiguration instead of web.xml
.
It has SpringBootServletInitializer
class which eventually implement the WebApplicationInitializer
interface and override its onStartup
to configure things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With