Spring Boot is one of the most powerful Java EE frameworks that aims to facilitate the development of new application. It brings all the power of the Spring stack (Spring dependency injection, Spring data JPA, Spring ORM, Spring MVC…) combined with an embedded Tomcat/Jetty server avoiding you heavy deployment.

One  of the strong point of Spring Boot is the automatic configuration. In fact, you have just to respect some naming conventions and Spring Boot will do the job for you all by giving you the flexibility to override the defaults  configurations if you want.

This tutorial will present a basic “hello world !” application using Spring Boot.


The tutorial source code is available on Github. Download the source

Creating the project

Using Spring Tool Suite (STS) or Spring IDE

If you are using Eclipse IDE, you can install Spring IDE or Spring Tool Suite (STS) from the Eclipse Market place to work easily with Spring projects. Below steps to follow to create a Spring Boot project :

  • Open Eclipse and go to File -> New -> Other -> Spring -> Spring Starter Project
  • Name your project. Example :

Creating Spring Boot project

  • Click Next and Finish to end the creation

Basic Eclipse

If you don’t have Spring IDE or Spring Tool Suite (STS) installed on your Eclipse, you can  create a Spring Boot structure manually by creating a Maven project. Below steps to follow :

  • Open Eclipse and go to File -> New -> Other -> Maven-> Maven Project and click Next >
  • On the next window, choose Create a simple project (skip archetype selection) and click Next >
  • Fill the Maven project form as below

Spring Boot Maven project creation

  • Click Finish to create the project.

Project structure

Create all files you miss to make your project corresponds to the following :

Spring Boot project structure

Pom.xml

Update your pom file to make it corresponds to the following :

Spring Boot main class : Application.java

The  Application.java file would declare the main method that  uses SpringApplication.run() method to launch the application. It’s annotated with @SpringBootApplication which is  equivalent to using  @Configuration@EnableAutoConfiguration and  @ComponentScan all together.

Consider to put your Application.java in the root package of your application. Why ? Because Spring Boot implicitly defines its package as the search package to search you spring component (@Component), your JPA entities (@Entity), your class configuration (@Configuration) etc. Like this, you don’t have to specify a @ComponentScan and Spring Boot will do the job for you as you will see in the next section.

Spring component : HelloWorldService.java

Let’s consider the following basic Spring component containing the method sayHelloWorld() that returns a String.

The HelloWorldService is annotated with @Component in order to tell Spring to register this class as Spring Bean.

Spring MVC Controller : HelloWorldController.java

Below a simple Spring MVC controller containing a method that will be called on the root context of our application. This method calls the helloWorldService created in the previous section that we inject with the annotation @Autowired

That’s all, the application is ready now to use !

Running the application

  • Right-Click on your project -> Run As -> Spring Boot App or Right-Click on Application.java -> Run As -> Java Application

You must see Spring Boot starting logs on the console as below :

Spring Boot starting logs

Wait for the finish and go to :  http://localhost:8080/

You must see, “Hello World !” as below :

Spring Boot hello world

Notice that there wasn’t a single line of XML nor a web.xml file.


The tutorial source code is available on Github. Download the source

References