🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this quick article, we will discuss the usage and importance of SpringBootServletInitializer class in Spring boot. This SpringBootServletInitializer class belongs org.springframework.boot.web.servlet.support package.
SpringBootServletInitializer Class
SpringBootServletInitializer class is an extension of WebApplicationInitializer which runs a SpringApplication from a traditional WAR archive deployed on a web container. 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 Class Diagram
Below class diagram shows a list of methods SpringBootServletInitializer class provides:
SpringBootServletInitializer Class Methods
- protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) - Configure the application.
- protected WebApplicationContext createRootApplicationContext(ServletContext servletContext)
- protected SpringApplicationBuilder createSpringApplicationBuilder() - Returns the SpringApplicationBuilder that is used to configure and create the SpringApplication.
- void onStartup(ServletContext servletContext)
- protected WebApplicationContext run(SpringApplication application) - Called to run a fully configured SpringApplication.
- protected void setRegisterErrorPageFilter(boolean registerErrorPageFilter) - Set if the ErrorPageFilter should be registered.
SpringBootServletInitializer Class Usage (Example)
In order to create a deployable war file and deploy on external tomcat, let's consider we have Application or Main class which extends the SpringBootServletInitializer and overrides the configure() method. That method uses SpringApplicationBuilder to simply register our class as a configuration class of the application:
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 Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Springboot2WebappJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(Springboot2WebappJspApplication.class, args);
}
}
If it is a maven project and to create a war file using the following command:
mvn clean install
Once maven builds success then WAR file is generated under a target folder. Just copy war file to external tomcat webapps folder and start the tomcat server.
If we want to package it as a JAR file, then we’ll need to add the same logic to the main() method so that the embedded container can pick it up as well.
Note that a WebApplicationInitializer interface(SpringBootServletInitializer implementation) is only needed if you are building a war file and deploying it. If you prefer to run an embedded web server then you won't need this at all.
Let's develop complete step by step Spring Boot 2 Web application, generate a WAR file and deploy on external Tomcat server at http://www.javaguides.net/2018/09/spring-boot-deploy-war-file-to-external-tomcat.html
In the next article, you will learn how to develop CRUD RESTFul API using Spring boot 2, Hibernate 5, JPA, Maven, and MySQL database.
Check out all spring boot articles, guides, and tutorials at Top Spring Boot Tutorials
Reference
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment