Devlane Blog
>
Tutorials

Spring Boot Applications: Hello World Tutorial

Every, or almost every developer, knows Spring potential when thinking of how to resolve or develop a software solution, either for a web application or a background and/or scheduled process.

by
Nicolás Mosconi
|
May 13, 2024

One of the most recent tools of Spring is Spring Boot.

Its main idea is that the developer only focuses on resolving the business logic rather than on configuring the necessary environment to run their application, as Spring boot does this for them. 

Furthermore, Spring Boot can be used together with other Spring solutions like Spring Cloud

In fact, Spring Cloud builds on Spring Boot.

Let’s pick Spring Cloud Consul working with Spring Boot to describe how easy it is to manage an integration with another service in the cloud, in this case with a Key/Value Store feature of Consul.


Step by Step example


The idea of the example exercise is to list the current instances of people existing in a table in the database, in a console terminal. 

But its main purpose is to store the configuration to create the connection to the database in Consul as a key/value. 

Through the implementation of Spring Boot with Spring Cloud Consul to get this configuration and using such configuration in the bootstrap.yml properties file, Spring can take these values to establish the database connection when it creates the context. 

As a bonus, we use Spring Data JPA to get the example data from the database.

This is another little example of how easy it is to integrate one more of Spring.io solutions with Spring Boot.


Previous consideration


The MySQL database server and the Consul server are installed in the localhost environment and running.


Preparing the environment

To begin with, we need to create a database instance and a table with the example data. 

Besides, we have to create a java maven project and set the key/value in consul using the Consul HTTP API.

Database

Create a database schema called spring_boot_example, a table named people and populate this table with the data needed for the example:

Consul

Java Maven Project

Taking action

We need to edit the pom.xml file to include all the Spring dependencies, create the Person class and do the ORM mapping using JPA, create the Person DAO interface in order to access the data, and add the consul and database configuration properties into the bootstrap.yml file, so that Spring Boot takes these values when it starts the application context. 

Finally, we have to create the Main class to be able to run the application.

Pom.xml file

When the pom.xml file is edited, the resultant content should be:

Person Class

PersonDAO class

Bootstrap.yml file

We have to create this file in the resources folder and add the next content:

NOTE: The URL, username and password properties of the datasource are written as Spring placeholders annotation (${…}), which means that the value is taken from an external context, in this example, from Consul.

Main application class

Conclusion

Through this example we tried to prove how simple it is to create Spring Boot based Applications. It just requires very little configuration in order to be able to integrate cloud-bases and other services in the application.

The following are some other Spring Boot features that might come in handy:

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration\

More info