<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=2258763691017137&amp;ev=PageView&amp;noscript=1">
Simplex Software

Benefits of Spring Boot based applications in a practical Hello World example

1. Introduction

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. 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.

2. 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.

2.1 Previous consideration

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

2.2 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.


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

    CREATE SCHEMA spring_boot_example;

    CREATE TABLE `spring_boot_example`.`people` (
      `name` VARCHAR(45) NULL,
        PRIMARY KEY (`id`));

      spring_boot_example.people (name) 
    ('James'), ('John'), ('Robert'), ('Michael'),('William'),
    ('David'), ('Richard'), ('Charles'), ('Joseph'), ('Thomas'),
    ('Christopher'), ('Daniel'), ('Paul'), ('Mark'), ('Donald');


    curl -X PUT -d     'jdbc:mysql://localhost:3306/spring_boot_example'

    curl -X PUT -d 'user'

    curl -X PUT -d 'user654'

Java Maven Project

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  

2.3 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:

    <project xmlns="http://maven.apache.org/POM/4.0.0"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Person class

    public class Person{  
        private long id;
        private String name;
        public long getId(){  
            return id;
        public void setId(long id){  
            this.id = id;
        public String getName(){  
            return this.name;
        public void setName(String name){  
            this.name = name;

PersonDAO class

    public interface PersonDAO extends CrudRepository<person, long="">{}  

Bootstrap.yml file

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

          host: localhost
          port: 8500
            enabled: true
            prefix: '/'
            defaultContext: '/'
            format: KEY_VALUE
        url: ${configuration.db.url}
        username: ${configuration.db.user}
        password: ${configuration.db.password}
        testWhileIdle: true
        validationQuery: SELECT 1
          naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
          hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

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

    public class Application implements CommandLineRunner {  
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(false).run(args);
        private PersonDAO personDao;
        public void run(String... strings) throws Exception {
            personDao.findAll().forEach(person -> 
            System.out.println("Person id: " + person.getId() + " - Name: " + person.getName()));

3. 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 others 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


15 Questions You Should Ask a Software Development Company

Follow Us

Subscribe To Our Blog

Nuevo llamado a la acción

Let Us Know What You Thought about this Post.

Put your Comment Below.