Spring Boot Complete Guide

This tutorial explains the basic concepts of Spring Boot and how to run a simple application using the Spring Boot framework. Also end of this tutorial, popular tutorials on Spring Boot has been collated and given for the readers reference.

Introduction

Spring is the most popular and innovative community for the Java developers. They have changed the way how to build the enterprise applications and how to manage them. Spring Boot is their latest innovation to keep up to date with the changing technology needs. The primary motivation behind developing Spring Boot is to simplify the process for configuring and deploying the spring applications.

If you are working on Spring Boot and looking for a support, please send me a mail to krishnas at javabeat.net.

Spring Boot will get rid of all the fuss involved on configuring the dependencies for building the enterprise applications. Here as we know that configurations are fully loaded with bunch of XML files (Spring was first introduced the full fledged XML configurations), now they don’t like it. They don’t want anything to be configured or maintained in a file which is extra work for the developers.

Future Spring projects would not have any XML configurations as part of it, everything will be handled by the project Spring Boot. This article provides basic insights on what is all about Spring Boot and how it will benefit the developers. Also this page will be regularly updated with latest information on Spring Boot.

WHY We Need Spring Boot?

Spring Boot is next generation attempt to easy spring setup.

Spring Boot’s main benefit is configuring the resources based on what it finds in the classpath.

If your Maven POM includes JPA dependencies and a MYSQL driver, then Spring Boot will setup a persistence unit based on MySQL. If you’ve added a web dependency, then you will get Spring MVC configured with defaults.

When we talk about defaults, Spring Boot has its own opinions. If you are not specifying the details, it will use its own default configurations. If you want persistence, but don’t specify anything else in your POM file, then Spring Boot configures Hibernate as a JPA provider with an HSQLDB database.

The primary goals of spring boot:

To provide a radically faster and widely accessible getting started development experience for all Spring development. Since spring community has evolved so big, it is time to re-invent the way how spring applications are deployed in much quicker turn around time.

To be get started so quickely using the default values which are supported out of the box in the Spring Boot configurations.

To provide bunch of non-functional features/solutions that are very much common to large scale projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).

What is Spring Boot?

Spring Boot is approach to develop Spring based application with very less configuration. It leverages existing Spring projects as well as Third party projects to develop production ready applications. It provides a set of Starter Pom’s or gradle build files which one can use to add required dependencies and also facilitate auto configuration.

Depending on the libraries on its classpath, Spring Boot automatically configures required classes. For example to interact with DB, if there is Spring Data libraries on class path then it automatically sets up connection to DB along with the Data Source class.

Spring Boot shipped with command line tool (Groovy) for building the applications and another one is Java implementation for Java developers. However, Groovy follows the Java syntax, which is easy for the Java developers to use either of the tool.

If you want to quick start with an example, using the Groovy’s CLI tool is the easy one.

Spring Boot CLI

It is the easiest and quickest way to start using the Spring Boot. It is a command line tool used for executing the groovy scripts. In summary, you can install this tool by following these steps:

Download the binary distributions for this project from here. Spring Boot CLI requires Java JDK v1.6 or above in order to run. Groovy v2.1 is packaged as part of this distribution, and therefore does not need to be installed (any existing Groovy installation is ignored)

If you unpack the the zip file, you will find spring.bat which will check the all the settings. This script can be found under the directory /bin.

The summary of what is provided in the installation instructions file inside the package.

SPRING BOOT CLI - INSTALLATION
==============================
Thank you for downloading the Spring Boot CLI tool. Please follow these instructions
in order to complete your installation.
Prerequisites
-------------
Spring Boot CLI requires Java JDK v1.6 or above in order to run. Groovy v2.1 is packaged
as part of this distribution, and therefore does not need to be installed (any existing
Groovy installation is ignored).
The CLI will use whatever JDK it finds on your path, to check that you have an appropriate
version you should run:
java -version
Alternatively, you can set the JAVA_HOME environment variable to point an suitable JDK.
Environment Variables
---------------------
No specific environment variables are required to run the CLI, however, you may want to
set SPRING_HOME to point to a specific installation. You should also add SPRING_HOME/bin
to your PATH environment variable.
Checking Your Installation
--------------------------
To test if you have successfully install the CLI you can run the following command:
spring --version

If you are working with Linux distributions like Ubuntu, then run the following command to install through GVM

$ gvm install springboot
$ spring --version
Spring Boot v0.5.0.M4

Hello Word Example Using Spring Boot

I have used the same example provided in the spring boot’s official documentation.

Once the above file is created, you can run the application by using the following command:

$spring run app.grrovy

You can invoke the http://localhost:8080 in your browser and you will see the result “Hello World!”.The above command can invoke the application and run it in the web server.

If any dependencies are required like web server, etc. can be resolved by the Spring Boot itself.

There is another implementation of the same project for the Java developers. If you don’t want to use the above implementation, you can choose the Java implementation with Maven build (Read : How to Install Maven on Windows / Ubuntu ?). As a first step, you have to create create the pom.xml with the following artifacts.

Once you have created the above Java file and the pom.xml in the same directory, please run the following command:

$ mvn package
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

First statement invokes the pom.xml and builds the complete package by downloading all the dependencies. The second statement makes the spring application and bundle with the web server. As said in the above groovy example, you can invoke the application in the browser.

I hope this article have provided the basic steps to build the applications using Spring Boot. Keep visiting this page for my update on the Spring Boot releases and list of features in the each release. Below section you can browse the list of Spring Boot tutorials published in JavaBeat.

Servlet Containers Support

The following are the list of servlet containers supported out of the box in spring boot applications.

Name

Servlet Version

Java Version

Tomcat 8

3.1

Java 7+

Tomcat 7

3.0

Java 6+

Jetty 9

3.1

Java 7+

Jetty 8

3.0

Java 6+

Undertow 1.1

3.1

Java 7+

Starter POMs

Spring Boot has set of pre-defined starter POMs for the developer’s convenience. The starter POMs are convenient dependency descriptors that can be added to your application’s Maven. In simple words, if you are developing a project that uses Spring Batch for batch processing, you just have to include spring-boot-starter-batch that will import all the required dependencies for the Spring Batch application. This reduces the burden of searching and configuring all the dependencies required for a framework.

Spring offers wide range of started POMs that can be used in your application. Here is the list of started POMs.

Template Engines Support

If you are using any of the above template engines, spring boot will automatically pick the templates from src/main/resources/templates. It is good practice to avoid using JSP for the templates as it has lot of limitations using with embedded servlet containers.

Caching Support

Since the release of Spring Boot 1.3.0, auto-configuration for the following technologies has been added to the spring boot:

Spring Boot vs Spring MVC

First of all it doesn’t make sense to compare spring boot and spring MVC framework. But, lot of beginners have seems to be misunderstand the purpose of spring boot project. In simple words:

Spring MVC is a framework for building a web application. It is similar to a Struts framework.

Spring Boot project helps you to package the spring applications and deploy to server with minimal effort. For example, you could develop your web site using the Spring MVC framework, but you will be using the spring boot for preparing the deployment to production. I hope this clarifies your question.

Frequently Asked Questions (FAQs)

How to tell Spring Boot to use Jetty server instead of Tomcat server?

You can achieve that by adding the jetty server dependency ‘spring-boot-starter-jetty‘ in the pom.xml file.

What are the different ways to create Spring Boot project?

Using Maven

via start.spring.io

Using Sprig Boot CLI

Spring Boot IDE

Can we deploy the Spring Boot applications into application servers like WildFly (formerly known as JBoss) and WebLogic?

One of the most common questions comes into mind is that, spring boot applications are built as JAR file with embedded servlet containers that runs as the standalone services. How to deploy them in the existing servers installations like Tomcat, WebLogic, WildFly, etc. It is possible and very simple to do with the following steps:

Instead of making the JAR file, change the Maven build to a WAR file

Remove the declaration of spring-boot-maven-plugin plugin in the pom.xml file

Change the Maven packaging type to WAR

Finally you will have to add the web entry point into your spring boot application. The existing spring boot application entry point has to configure by using the SpringBootServletInitializer.

Exclude Tomcat dependency from the pom.xml file

Can we debug spring boot applications?

Yes. Like normal Java application, you can right click on the main() method and debug as the Java application. It will work fine. If you are facing any issues with the debugging, please reach out me (krishnas at javabeat.net) for the help.

Spring Boot and Spring MVC : This tutorial for beginners who are interested to learn basics of Spring Boot and Spring MVC working together. At the end of this tutorial, you could run a simple SPring MVC application using Spring Boot.

Integration Testing REST API in Spring Boot : This tutorial explains how to write the integration testing REST API in Spring Boot. This tutorial uses the examples written in the previous tutorial. This tutorial intends to create the automated testing using JUnit and runs with Spring Boot application.

Spring Data REST Exporter : This tutorial provides the detailed instruction on how to use Spring Data REST project in your Spring MVC applications to export your JPA entities. The greatest advantage of using the Spring Data REST is that, it easily exports the entities and made available through the HTTP protocol.

Spring Data Neo4j 3 REST Exporter : This guide walks you through the process of converting a runnable JAR application that was built with Spring Boot into a WAR file that you can run in any standard servlet container. You’ll take a simple Spring MVC web app and build a WAR file using Spring Boot.

External Configurations for Spring Boot Applications : This tutorials explains you the different ways how you can do external configurations for Spring Boot applications. When you work with the real time environments, External Configurations for Spring Boot would become important for the flexibility. There are multiple sources from where the configuration can be read from and the order in which the configuration properties are overridden is determined by Spring Boot.

Logging Configuration in Spring Boot :Spring Boot provides great support for logging and provides lot of hooks to configure the same. In this article we are going to see the default support for logging in Spring Boot, then use the hooks i.e the spring properties to configure the logging. At the end of this article, you will be familiar with the logging configuration in spring boot applications.

Working with SQL Databases and Spring Boot :SQL Databases are an integral part of any application being development. They help in persisting application data. SQL Databases provide advanced support for querying data using the Structured Query Language(SQL). Spring Boot provides great support for interacting with SQL databases with minimal or no XML configurations.

Spring Data JPA Tutorial using Spring Boot : This tutorial guides you through on building simple REST APIs using Spring Data JPA and Spring Boot. At the end of this tutorial, you would be able to write the applications with Spring Data JPA using Spring Boot.

Complete Guide for Spring Boot Actuator : Spring Boot Actuator is a sub-project / feature that is part of the spring boot framework. It is not a separate framework to be added to your applications. Main purpose of this feature is to provide various useful metrics about the applications. It is very helpful in the production environment to check the various metrics like health of your application, configurations, error page, version details, etc.

Spring Cache Tutorial : This tutorial explains how to configure spring cache with the latest version of spring (spring 4) and also it highlights the implementation architecture of spring cache framework. It uses spring boot for configuring and running the application.

Spring Boot Adoption

Eugen has published a report about the Spring Boot adoption here. This chart shows that there is more developers started using Spring Boot for their project development.

Here is the chart that shows number is tags created on stack over flow forums for spring boot topic. This chart shows up to the month of July 2015. This shows how fast spring boot is adopted by most developers for the real time implementation.

Spring Boot Resources

If you come across any interesting facts or tutorials for Spring Boot, please send me the details. I will update this section with more useful resources.

Performance

Eclipse & NetBeans

Deployment

Recommended Books

Spring Boot in Action – Click on the image to buy this book

I hope this tutorial provide enough details about Spring Boot. This page would be regularly updated and maintained with latest information about Spring Boot framework. If you are looking for any specific information, please write it in the comments section.

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Spring Boot is not a framework to develop applications. It is just framework to package and deploy existing spring applications without writing the configurations. So, you would use spring boot for deploying your spring mvc applications. I hope this clears your questions.

Hi Krishna,
I have the following configuration problem: I have module A which uses spring boot dependency management. And it depends on module B, so I’am adding module B as to module A.
Then I package my application into executable jar with spring-boot-maven-plugin and repackage goal. But when starting it with java -jar .. -I get error that classes from my module B are not found. How should I define a dependency to custom module when using spring boot dependency management?