Java Spring Boot – Getting Started

In this tutorial, you will learn to work with Java Spring Boot and build microservices simply and effectively. Let’s start with the introduction.

Spring Framework in Java

It’s an open-source, enterprise-level framework for creating standalone, production-grade applications that run on the Java Virtual Machine (JVM).

One of the biggest advantages of Spring is that it allows developers to create modular applications consisting of loosely coupled components that are ideal for microservices.

Microservices architecture is an approach in which a single application is composed of many loosely coupled and independently deployable services called microservices.


A fundamental aspect of the Spring framework is Dependency Injection, through which the Spring container “injects” objects into other objects as dependencies.

Have a look at these two classes:

MyAppConfig.class

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAppConfig {

  @Bean
  public ObjectMapper defaultObjectMapper() {
    return new ObjectMapper();
  }
}

HomeController.class

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

  private final ObjectMapper objectMapper;

  public HomeController(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }

  @GetMapping("/")
  public String getJsonResponse() {
    // do some logic and return a response as a JSON String
    return objectMapper.createObjectNode().toPrettyString();
  }
}


The class marked with the @Configuration annotation will be instantiated very early in the application lifecycle, during its startup. Also, Spring will execute each method marked with an @Bean annotation and save the result of the method in a registry of objects called the Application Context.

In this case, the Application Context contains a singleton object called objectMapper. The HomeController class is a REST controller or another class that Spring automatically instantiates for us. Note that the construction of the HomeController object requires the presence of the objectMapper.

Here, Spring will search the Application Context, find the objectMapper object, and use it to instantiate the HomeController class. As we can see, Spring has instantiated individual classes and resolved their interdependencies. 

The developer can write the application logic and leave all the instantiating, ordering, connecting, and destroying objects to the framework.

Another thing that Spring does extremely well is the abstraction of programming concepts. Suppose we want to implement the logic of caching the data we read from the database. We can use the Guava library for this purpose. If, after some time, for any reason, we want to use another solution, say EhCache, we will have to change the code and use a completely new software API.

On the other hand, Spring provides a generic API for caching, which can use Guava, EhCache, Caffeine, Gemfire or similar technology in the background. In this case, the choice of a specific technology is a matter of configuration.

This approach ensures easy replacement of the caching solution and makes the whole approach to writing caching much faster and simpler than multiple APIs. We only need to know one.

Perhaps the most apparent advantage of the Spring framework, from the developer’s point of view, is reflected in its ecosystem, i.e. a large number of modules and projects that provide great opportunities for developing enterprise applications.

This modularity ensures that an application, manually or using dependency management tools such as Maven, imports only the modules it needs. So, if we want to develop a web application, we need spring-mvc, to develop messaging functionality, we can use spring-integration, for databases spring-data-JPA, etc.

Spring allows us to define methods that will be called automatically after the object gets constructed or methods that will be called before it is destroyed.

Although the development of applications in Java has been much improved by the introduction of Spring, there is still a lot of configuration that must be done for the microservices to behave properly. This is where Spring Boot enters the scene.

What is a Spring Boot?

Spring Boot is a tool that enables faster and easier creation of microservices by bringing autoconfiguration and starter packages for easier preparation and setting up the Java applications.

It scans classpath applications and examines imported (Spring and third-party) libraries. Then, it tries to determine the application type and create appropriate bean objects that the developer typically has to create according to application requirements.

For example, if Boot detects the H2 driver and spring-jdbc libraries on the classpath, it will automatically create JdbcTemplate and DataSource objects.

If it detects Hibernate libraries, it will automatically create more objects, e.g. EntityManagetFactoryBean, or TransactionManager object, by instantiating the HibernateTransactionManager class. This approach is called autoconfiguration.

Autoconfiguration means that applications get initialized with pre-set dependencies that we don’t have to configure manually.

Spring Boot organizes libraries in the so-called starters, and each starter contains a set of libraries necessary for specific application needs. Thus, spring-boot-starter-jdbc contains all the libraries needed to work with the database, and spring-boot-starter-web includes many libraries for working with web applications.

In this way, the developer doesn’t need to worry about what libraries are needed for the specific type of application and the compatibility of versions of those libraries.

Another great advantage of Spring Boot is that it comes with embedded web servers such as Tomcat and Jetty.

All applications in Spring Boot are standalone services that can be run by executing a spring boot run command. No need to deploy it on an external web server. 

To learn more about Spring Boot, look at Spring Boot tutorials and Spring Boot Microservices pages.