Show Topics
Hide Topics

Spring Boot with Spring Initializr Example

we are using Spring Initializer(https://start.spring.io) to create a template for spring boot application

 

1.Go to https://start.spring.io , Choose Dependencies & Generate Project. Here we are not selecting any dependencies because it is just a Hello world program

 

2.Open Eclipse, import→ Maven →Existing Maven Projects → Select Project →Finish

 

3.The Project structure will be as follows if we open eclipse Package explorer

 

4.If we open the pom.xml it contains only basic dependencies like spring-boot-starter which allows start spring boot application

 

 

5.Select Project, Right click Run asmaven install to download and install the dependencies.

 

 

 

6.Spring boot generates the default java class which contains main() method. The main method calls the run method of SpringApplication. SpringApplication.run(SpringBootHelloWorldApplication.class, args); This run method bootstraps the application starting spring which will run the embedded Tomcat Server.

Let’s add some helloworld message to print

 

 

7.Select the Java file and right click Run As Java Application

 

 

8.We can also run this application from the command line using the jar file that is generated. To get the jar file, select pom.xml right click Run As Maven Build (2nd one), goals=package, Apply & Run

 

 

9.Open command line and go to the folder where your project is located. Next, move to the target folder and then type java -jar <<filename>>.jar.

java -jar SpringBoot_HelloWorld-0.0.1-SNAPSHOT.jar

 

 

How it works internally

 

1.we place the all the required Spring boot starters in pom.xml which are requires for implementing Spring Boot application. on loading project, the all required starter dependencies are added automatically to the project

 

2.By running Spring Boot main class, at @SpringBootApplication line it will do the Auto configuration things, it will automatically add all required annotations to Java Class ByteCode.

 

3.On Executing main() method SpringApplication.run() used to bootstrap and launches Spring Boot application.