Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it is necessary to extend`SpringBootServletInitializer` while deploying it to an external tomcat

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?

like image 452
Mehraj Malik Avatar asked Jan 01 '18 07:01

Mehraj Malik


People also ask

Why do we need to extend SpringBootServletInitializer?

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.

What is the use of SpringBootServletInitializer in spring boot?

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.

Does spring boot support external application 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 .


2 Answers

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

like image 157
ManojP Avatar answered Oct 06 '22 17:10

ManojP


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.

like image 28
Mehraj Malik Avatar answered Oct 06 '22 17:10

Mehraj Malik