Spring Boot tutorial

This tutorial will teach you how to build a Hello World program with Spring Boot and create a REST service.

Approx Time to finish this tutorial: 20min.

This tutorial describes the main functionalities of Spring Boot 2.0 and the underlying concepts such as starter projects and auto configurations.

You will understand how Spring Boot develops Spring applications with agility, providing a prepared menu for creating a REST service. Also, you will learn how to solve common problems at the configuration level of an enterprise application by using auto-configure.

This tutorial covers the following topics:

  • Introduction.
  • The Hello World program, using Spring Boot.
  • The core components
    • Spring Boot Starter,
    • Spring Boot Auto-configuration,
    • Spring Boot CLI,
    • Spring Boot Actuactor.
Introduction

The Spring Boot was released in 2013. Since then, the Spring team is focused on making software development easy.

Spring Boot is not a separate framework. It is a collection of ready-made things to pick up and use without worrying about the configuration.

Spring BootSpring Boot provides a new strategy for application development with Spring Framework and with minimal effort. It enables you to focus only on the functionality rather than the configurations. According to the Spring Boot documentation:

Spring Boot makes it easy to create stand-alone, production-grade  Spring based applications that you can “just run”.

 

In this tutorial, we will see how Spring Boot simplifies Spring application development.

The Hello World program, using Spring Boot

The very first application is a Hello World web application.

The Hello World Application, in Groovy-based controller class, is as follows:

@RestController
class HelloWorldController {
  @GetMapping ("/")
  String hello ( ) {
    "Hello World!!"
  }
}

 

The class HelloWorldController is declared as a @RestController. This enables the Spring MVC to handle web requests. @GetMapping maps / to the hello( ) method. The method hello ( ) returns a string when it is called from a browser or by using curl on the command line.

This controller is a complete Spring web application. Notice that we don’t need any web.xml file to configure it or any application server. We can run this first application using Spring Boot CLI, typing the command:

$Spring run HelloWorldController.groovy

 

Notice that this process was straightforward.

If you choose to develop it in Spring Framework, you need to do these configurations:

  • Create a project in Maven or Gradle.
  • Define the dependencies such as Spring MCV and Servlet API.
  • Write the web.xml.
  • Implement the WebApplicationInitializer class that declares the DispatcherServlet.
  • Write the Spring MVC configuration class to enable the Spring MVC module for the HelloWorld application.
  • Implement a controller class that will respond to the request.
  • Configure a web application server, for example, Tomcat.

As a conclusion of this part of the tutorial, notice that:

Spring Boot simplifies Spring application development. It does not compete with the Spring or Spring MVC Framework.

We have already created a Controller class. Now, let’s see the tools that Spring Boot uses to reduce our developing efforts.

The core components

Spring Boot brings the magic to Spring application development. The key components of this technology are :

  • Spring Boot Starter
  • Auto-configuration
  • Spring Boot CLI
  • The Actuactor

These four components are the tools behind the Spring Boot’s magic. Let’s see each of them in more detail.

Spring Boot Starters

A Starter is a small Spring project, which includes modules MVC, JDBC, and so on. When building a Spring application, you add, in the classpath, a starter of the module. The framework will import the necessary libraries to the project in Maven or Gradle.

You don’t need to worry about the module libraries and dependent versions of the libraries. The Starters makes all the build configurations for you.

For example, if you want to create a web application or an application to expose RESTful services using the Spring web MVC module, you include the starter spring-boot-starter-web dependency in your project.

The code in the Spring application in Maven looks like this:

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

 

In the Gradle structure, the code you have to add is :

implementation 'org.springframework.boot:spring-boot-starter-web'

 

The starter dependency resolves these transitive dependencies:

spring-web-*.jar
spring-webmvc-*.jar
tomcat-*.jar
jackson-databind-*.jar

 

The spring-boot-starter adds functionalities to your project. In this example, you add the web starter to your Spring application. This starter provides all the web functionalities that your application needs. Similarly, if you add the security starter to your project, this starter will add all the security functionalities to your application.

Spring Boot provides a wide range of Starter projects. The following
application Starters are under the org.springframework.boot group:

  • spring-boot-starter-web-services: For building applications exposing SOAP web services.
  • spring-boot-starter-web: Build web applications and RESTful applications.
  • spring-boot-starter-test: Write great unit and integration tests
  • spring-boot-starter-jdbc: Traditional JDBC applications
  • spring-boot-starter-hateoas: Make your services more RESTful by adding HATEOAS features.
  • spring-boot-starter-security: Authentication and authorization using Spring Security.
  • spring-boot-starter-data-jpa: Spring Data JPA with Hibernate
    spring-boot-starter-cache: Enabling the Spring Framework’s caching support.
  • spring-boot-starter-data-rest: Expose simple REST services using Spring Data REST.
Spring Boot Auto-configuration

Auto-configuration works by analyzing the classpath of your project.  Notice that Spring Boot works with Maven, Gradle, and Ant/Ivy. Also, Spring Boot can’t configure if you forget a dependency.

Spring Boot offers auto-configuration of those modules in your Spring application based on the JAR dependencies you have added. First, Spring Boot looks for frameworks available on the classpath. After that, it checks the existing configuration for the application.

Spring Boot provides the @EnableAutoConfiguration annotation that is responsible for enabling the auto-configuration feature. This annotation is used in the main application file of the Spring Boot application. The @EnableAutoConfiguration annotation on a Spring
Java configuration class causes Spring Boot to create beans automatically it thinks you need, usually based on classpath contents, that it can easily override.

@Configuration
@EnableAutoConfiguration
public class AppDemo {
  public static void main(String[] args) {
    Application.run(AppDemo.class, args);
  }
}

 

Spring Boot provides a shortcut for this configuration file by using another annotation, @SpringBootApplication.
With this annotation, we can introduce all these three configurations :

  • @EnableAutoConfiguration,
  • @Configuration,
  • @ComponentScan.

Let’s see the following updated code:

@SpringBootApplication
public class MyAppConfig {
  public static void main(String[] args) {
    SpringApplication.run(MyAppConfig.class, args);
  }
}

 

The annotation @SpringBootApplication is available since Spring Boot 1.2.

Spring Boot Starter reduces a build’s dependencies, and Spring Boot autoconfiguration reduces the Spring configuration.

Spring Boot CLI

Spring Boot also provides a command-line tool that you can use to write Spring applications quickly.

Spring Boot’s CLI gives you more free time from adding starter dependencies and auto-configuration to let you focus only on writing your application-specific code.

CLI is an optional feature of Spring Boot; it allows you to write a complete application with your application code only as it doesn’t need to build a traditional project. CLI provides tremendous power and simplicity for Spring development.

The Actuator

The Actuator is the final key component. The previous components simplify Spring development. Meanwhile, the Actuator offers the ability to inspect the internals of your application at runtime.

The Actuator provides data on auditing, metrics, and the health of your application using HTTP endpoints or with JMX. It helps you manage your application when it’s pushed to production.

The actuator also provides details about auto-configuration.
It also ensures all environment variables, system properties, configuration properties, and command-line arguments are available to your application. It provides a trace of recent HTTP requests handled by your application. It also gives information about the current state of the threads in the application.

The Actuator provides the listed information by using web endpoints or via a shell interface.

>>Click here for more Code in Java solutions…