Spring Boot Initilizr With IDEs or IDE Plugins

Filed Under: Spring

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:

  1. Spring Boot Initilizr With Web Interface
  2. Spring Boot Initilizr With IDEs/IDE Plugins
  3. Spring Boot Initilizr With Spring Boot CLI
  4. 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.

    spring-boot-initilizer-ide-example1

  • We will get the following “New Spring Stater Project” Wizard to provide our project related information
  • spring-boot-initilizer-ide-example2

  • Please provide our Spring MVC Maven WebApplication Project details as shown below and Click on “Next” Button
  • spring-boot-initilizer-ide-example3

    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
  • spring-boot-initilizer-ide-example4

    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“.
  • spring-boot-initilizer-ide-example5

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 as shown below
  • spring-boot-initilizer-ide-execute1

    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:
    1. Creates a Spring MVC application
    2. Starts Embedded Tomcat Server with default port number: 8080
    3. Deploy our Spring MVC application into Tomcat Sever

    spring-boot-initilizer-ide-execute2

  • Access our Spring MVC application with “http://localhost:8080/SpringMVCMavenIDEProject” or “http://localhost:8080/*” and observe the results
  • Here * means any URI or Context Path

    spring-boot-initilizer-ide-execute3

    As we don’t provide any mappings or View Pages(ex:- JSPs), we are seeing default error page.

NOTE:-

  1. Use this base Spring Boot WebApplication project and enhance it with your project requirements.
  2. We can also use Spring STE IDE generated “SpringMvcMavenIdeProjectApplicationTests.java” class to execute and test our Spring Boot Application
  3. 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?

  • “SpringApplication” is one of the Spring Boot API classes.
  • SpringApplication class is used to bootstrap a Spring application that will be started from a “main()” method
  • Example:-
    Spring STS IDE Generated class: “SpringMvcMavenIdeProjectApplication”

    
    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:

    1. Because of @Configuration annotation, It scans for @Bean methods to create beans.
    2. Because of @ComponentScan annotation, It does component scanning (Components means Beans annotated with @Component,@Service,@Repository,@Controller etc).
    3. Because of @EnableAutoConfiguration annotation, It triggers Spring Boot Auto-Configuration.
  • By default, SpringApplication class creates “AnnotationConfigEmbeddedWebApplicationContext” instance
  • spring-boot-api1

  • “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
  • spring-boot-api3

    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.
  • spring-boot-api2

    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

    1. This call is used to create “AnnotationConfigEmbeddedWebApplicationContext”.
    2. This “AnnotationConfigEmbeddedWebApplicationContext” instance is used to create an instance of “TomcatEmbeddedServletContainerFactory” class.
    3. This “TomcatEmbeddedServletContainerFactory” is used to create an instance of “TomcatEmbeddedServletContainer” class.
    4. “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.

Comments

  1. Neha says:

    v nice and simple explanation of Spring boot.this is how things should be explained.

  2. Himanshubhusan Rath says:

    Hello, Thank you so much for such an in-depth explanation.

  3. Madhu says:

    Thanks for the nice Information.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages