This is continuation to my previous post. Before reading this post, please go through my previous post at “Spring Boot Initilizr Web Interface“.
Brief Post Content
- Spring Boot Initilizr With IDEs/IDE Plugins
- Execute Spring Boot Application With IDEs/IDE Plugins
- Internals and Importance of “SpringApplication” class
Spring Boot Initilizr is used to quick start new Spring Boot Maven/Gradle projects within no time. It generates initial project structure and build scripts to reduce Development time.
As we discussed in my previous post, Spring Boot Initilizr is available in the following forms:
- Spring Boot Initilizr With Web Interface
- Spring Boot Initilizr With IDEs/IDE Plugins
- Spring Boot Initilizr With Spring Boot CLI
- Spring Boot Initilizr With ThirdParty Tools
We have already discussed Spring Boot Initilizr With Web Interface in my previous post. Now we are going to discuss on “How to use Spring Boot Initilizr with IDEs/IDE Plugins”. Almost all Java IDEs provides Spring Boot plugins.
We are going to use Spring STS Suite to develop all our Spring Boot programs. By default, Spring STS Suite comes with Spring Boot Plugins, no need to install/configure it.
Spring Boot Initilizr With IDEs/IDE Plugins
Please follow the following steps to create a Spring MVC Maven project with Spring STS Suite.(Almost similar steps with other IDEs too.)
-
- Please select “Spring Stater Project” Menu option from “File >> New” Menu as shown below.
If you don’t fine “Spring Stater Project” option directly in “New” menu, then select this option from “File >> New >> Other” under “Spring” category.
-
- We will get the following “New Spring Stater Project” Wizard to provide our project related information
-
- Please provide our Spring MVC Maven WebApplication Project details as shown below and Click on “Next” Button
Here we can select Project Type: Maven Project or Gradle Project, Language: Java or Groovy, Java Version, Packaging: Jar or War etc.
-
- Select our Project related Technology Stack as shown below and Click on “Finish” button to create our new Spring Boot Project
Here we can select Spring Boot Framework Version: 1.2.5 and all our Project required Technology Stack.
-
- Now Spring STS Suite creates a Maven Project and downloads all required Jars to our Maven Local Repository.
- If we observe this project structure, pom.xml and Java Source files, they all are similar to the files we have created by using “Spring Boot Initilizr With Web Interface“.
NOTE:-
In the same way, we can create JARs, Gradle projects, Spring Web Services, Spring REST API, Spring Cloud etc.
Execute Spring Boot Application
We have created Spring Boot WebApplication using Spring STS IDE. It’s time to run this application and observe the results.
-
- Mouse Right click on “SpringMvcMavenIdeProjectApplication.java” file and select “Spring Boot App” option from “Run As” Menu to run this Program as Spring Boot Application.
NOTE:- Why we need to run “SpringMvcMavenIdeProjectApplication.java” Program?
Because “SpringMvcMavenIdeProjectApplication.java” program has Java’s main() method. We will see the importance of “SpringApplication.run()” method soon.
- When we execute “SpringMvcMavenIdeProjectApplication.java” program, Spring Boot automatically does the following things:
- Creates a Spring MVC application
- Starts Embedded Tomcat Server with default port number: 8080
- Deploy our Spring MVC application into Tomcat Sever
- Access our Spring MVC application with “https://localhost:8080/SpringMVCMavenIDEProject” or “https://localhost:8080/*” and observe the results
As we don’t provide any mappings or View Pages(ex:- JSPs), we are seeing default error page.
NOTE:-
- Use this base Spring Boot WebApplication project and enhance it with your project requirements.
- We can also use Spring STE IDE generated “SpringMvcMavenIdeProjectApplicationTests.java” class to execute and test our Spring Boot Application
- Mouse Right click on “SpringMvcMavenIdeProjectApplicationTests.java” class and Select “Run As >> JUnit Test” option.
Internals and Importance of “SpringApplication” class
Now, We will discuss about What is SpringApplication? Why we need this? The importance of SpringApplication? The internals of SpringApplication?
1 2 3 4 5 6 7 8 9 10 11 |
package com.journaldev.springboot.sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringMvcMavenIdeProjectApplication { public static void main(String[] args) { SpringApplication.run(SpringMvcMavenIdeProjectApplication.class, args); } } |
We are making a call to SpringApplication.run() from the main() method of SpringMvcMavenIdeProjectApplication class.
- SpringMvcMavenIdeProjectApplication class is annotated with @SpringBootApplication annotation
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication does the following things:
- Because of @Configuration annotation, It scans for @Bean methods to create beans.
- Because of @ComponentScan annotation, It does component scanning (Components means Beans annotated with @Component,@Service,@Repository,@Controller etc).
- Because of @EnableAutoConfiguration annotation, It triggers Spring Boot Auto-Configuration.
- By default, SpringApplication class creates “AnnotationConfigEmbeddedWebApplicationContext” instance
- “AnnotationConfigEmbeddedWebApplicationContext” instance searches JAR files in our Project Classpath. Based on JARs found at Classpath, it creates an instance of one of the implemented class of “EmbeddedServletContainerFactory” interface
Example:-
By default, Spring STS IDE adds all required Tomcat Container JARs to our newly created Spring Boot Project Classpath. That’s why “AnnotationConfigEmbeddedWebApplicationContext” instance creates an instance of “TomcatEmbeddedServletContainerFactory” class.
- “EmbeddedServletContainerFactory” (“JettyEmbeddedServletContainerFactory” or “TomcatEmbeddedServletContainerFactory”) is used to create an instance of a “EmbeddedServletContainer” implementations.
Example:-
By default, Spring STS IDE creates an instance of “TomcatEmbeddedServletContainerFactory” class. This factory is used to create an instance of “TomcatEmbeddedServletContainer” class.
- When we run SpringMvcMavenIdeProjectApplication class main() method, it make a calls to “SpringApplication.run()” method.
“SpringApplication.run()” method in Spring STS IDE
- This call is used to create “AnnotationConfigEmbeddedWebApplicationContext”.
- This “AnnotationConfigEmbeddedWebApplicationContext” instance is used to create an instance of “TomcatEmbeddedServletContainerFactory” class.
- This “TomcatEmbeddedServletContainerFactory” is used to create an instance of “TomcatEmbeddedServletContainer” class.
- “TomcatEmbeddedServletContainer” instance starts a Tomcat Container at default port number: 8080 and deploys our Spring Boot WebApplication.
That’s it about Spring Boot Initilizr With IDEs/IDE Plugins and Importance of “SpringApplication” class.
We will discuss about “Spring Boot Initilizr With Spring Boot CLI“ and “Spring Boot Initilizr With ThirdParty Tools” in my coming post.
Please drop me a comment if you have any issues/suggestions.