Overview
This article will explain why class SpringBootServletInitializer is needed, its usage and why is it required to extend this class along with an example.Typically Spring boot applications are packaged as a jar and executed from a main class annotated with @SpringBootApplication as shown below.

@SpringBootApplication
public class SpringBootApp {

   public static void main(String[] args) {
      SpringApplication.run(SpringBootApp.class, args);
   }
}

When this main class is executed, Spring boot starts an embedded Tomcat(or configured server) at a default port or a port customized as per the application.

SpringBootServletInitializer purpose
Till now all ok.

Now, consider below scenarios

  • What if you do not want to package the application as a jar but as a war and this war should be deployed to a server. That is, it will not use embedded server.
  • You have a web application that needs to be converted to Spring boot?
  • You want to create a web application(packaged as a war file) but without web.xml?

Answer to all the above questions is SpringBootServletInitializer. This class belongs to org.springframework.boot.web.servlet.support package in Spring boot 2.x.

SpringBootServletInitializer provides following benefits

1. It lets a Spring boot application packaged as a war file be deployed in an external tomcat(or other) server and run successfully.
2. It lets you convert an existing web application to a Spring boot application by writing a main class that extends SpringBootServletInitializer.
3. Most of us know that if an application is a web app, then it would definitely have a web.xml file. With SpringBootServletInitializer, it is no longer necessary.

Remember that SpringBootServletInitializer is ONLY required when you want to package a Spring boot application as a war file, that will be deployed to an external server.
If the application is a jar and runs on an embedded server, this class is not required and you can survive with the approach defined in the last section.

SpringBootServletInitializer example
Suppose your Spring boot application will be bundled as a war, modify your main class to extend SpringBootServletInitializer and override its configure method as below.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootApp extends SpringBootServletInitializer{
  
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(SpringBootApp.class);
  }


  public static void main(String[] args) {
    SpringApplication.run(SpringBootApp.class, args);
  }
}

Inside configure method, you can add classes that contain configuration for the application. This method can accept any number of comma separated classes.
With the above configuration, your application can run both as a standalone jar or as a war deployed inside a servlet container or a server.
Note that configure is marked protected. This because it has a protected scope in SpringBootServletInitializer .
But you can also make it public since it is less restrictive access specifier than protected. as per method overriding rules in java.
That is all on SpringBootServletInitializer class.
Was the article helpful, do add a comment. You may also hit the clap if you liked it.

0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.