Spring Boot Tutorial for Beginners

1- What is Spring Boot?

Spring Boot is a Project lying on  the IO Execution layer of Spring IO Framework.

Spring Boot is the next step of Spring to make Spring easier in setting up and developing applications. For Spring Boot, Spring configuration are minimized. Spring Boot supports embedded containers in allowing web applications to be able to run independently without deploying on Web Server.

You can use spring boot to create Java Web application running via command line 'java -jar' or export to war file to deploy on Web Server as usual. Spring Boot supply you a "CLI Tool" to run spring scripts.
Spring Boot can be explained simply by the illustration below:

Below is the benefits of Spring Boot:

  1. It is very easy to develop Spring Based applications with Java or Groovy.
  2. It reduces lots of development time and increases productivity.
  3. It avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  4. It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
  5. It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort
  6. It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  7. It provides CLI (Command Line Interface) tool to develop and test Spring Boot(Java or Groovy) Applications from command prompt very easily and quickly.
  8. It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle
  9. It provides lots of plugins to work with embedded and in-memory Databases very easily

2- The objective of the document

This post is based on:
  • Eclipse 4.7 (Oxygen)

  • Spring Boot 2.x

In the post, I will create a Hello Spring Boot application, it is a simple Web application using Spring Boot, and run this application independently without deploying on Web Server.

Spring Tool Suite (STS)

To build Spring Boot applications on Eclipse IDE, you need to install Spring Tool Suite, this is an extended Plugin supporting Spring programming on Eclipse.

If you have not installed it, let's see the guideline here:

3- Create Spring Boot Project

On Eclipse, creating a project by using Spring Tool Suite (STS):
  • File/New/Other...
  • Name: HelloSpringBoot
  • Group: org.o7planning
  • Artifact: HelloSpringBoot
  • Package: org.o7planning.hellospringboot
Project has already been created, below is the default structure of the Project.

4- Some explanation of Spring Boot

In the above step, you have just created a Spring Boot Project, let's see pom.xml content:

pom.xml

There are 3 key points in pom.xml (1),(2), (3) as shown below, Spring Boot supports you in simplifing the declaration of Spring libraries.

spring-boot-starter-parent

spring-boot-starter-parent is a available project in Spring Boot. The dependent libraries is declared in spring-boot-starter-parent, your project only inherit it. You only need to declare <parent> in file pom.xml of your project.
** spring-boot-starter-parent **
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
You can overwrite the default dependence of Spring Boot. The topic will be cleared here:
  • TODO

spring-boot-starter-web

Other “Starters” simply provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we will add a spring-boot-starter-web dependency
<dependencies>

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>

</dependencies>

spring-boot-maven-plugin

Spring-boot-maven-plugin is the plugin providing necessary libraries that helps your project to be able to run directly without deploying on a Web Server. It helps to create a jar file that can be executable.
<plugins>

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

        <!-- ... -->

</plugins>

5- Running Spring Boot project

In the step above, you have created HelloSpringBoot project by Eclipse & Spring Tool Suite and there is no any changes. All things are created by default. Now you need some steps to run the application.
Your HelloSpringBoot application is running, you can try this link:
An error message "404 Not found" will appear when you run this link, but there are no any serious problems because your project has not any pages yet.
Creating style.css file in the static folder:
style.css
h1 {
    color: red;
}
And re-run the application with the link:

Run Configuration...

You can change the parameters for Server such as port, contextPath,... These problems are mentioned in the appendix at the end of the post.

6- How Spring Boot run?

When you create a "Spring Boot Web Appp", there is 1 class generated by the tool "Spring Tool Suite":
  • HelloSpringBootApplication

Your application is started by executing class HelloSpringBootApplication. The class is annotated by @SpringBootApplication.
HelloSpringBootApplication.java
package org.o7planning.hellospringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloSpringBootApplication.class, args);
    }
}
 
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes:
Therefore, @SpringBootApplication supports you in automatically configuring Spring, and automatically scanning entire project in order to find out Spring components (Controller, Bean, Service,...)

7- Technologies for View Layer

Next tutorial:

8- Appendix: Config Server

You are running the Spring Boot application above, you can change the server configurations such as Port, contextPath.....
Changing Server HTTP Port:
You can change other properties if you want, Eclipse will suggest the changeble properties and their meaning.