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.
Table of Contents
- Introduction
- Why we need spring boot?
- Spring boot releases
- What is spring boot?
- Spring boot CLI
- Hello World example using spring boot
- Servlet containers support
- Starter POMs
- Template Engines Support
- Caching Support
- Spring boot vs Spring MVC
- Frequently Asked Questions (FAQs)
- Spring boot tutorials
- Videos
- Adoption
- Resources
- Recommended Books
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.
- Also Read : Spring Tutorials | Spring 4 Tutorials
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).
Spring Boot Releases
Latest Release : Spring Boot 1.3.3. You require minimum Spring Framework 4.2.2 for this version.
- Features Added in Spring Boot 1.3.0 are:
- HTTP session persistence
- JMS and AMQP configuration improvements
- Logback and Log4j2 improvements
- Excluding auto configuration classes through configuration
- Auto configuration support for embedded MongoDB
- Auto configuration support for H2 web console
- The latest stable release is Spring Boot 1.3.2.
- Spring Boot 1.0.0 Release
- Spring Boot 1.2.7 Released
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.
Create the app.groovy with the following lines of code.
@Controller class ThisWillActuallyRun { @RequestMapping("/") @ResponseBody String home() { return "Hello World!" } }
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.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>0.5.0.M4</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable JAR --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Allow access to Spring milestones and snapshots --> <!-- (you don't need this if you are using anything after 0.5.0.M2) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.springsource.org/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.springsource.org/milestone</url> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.springsource.org/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.springsource.org/milestone</url> </pluginRepository> </pluginRepositories> </project>
Once maven is in the class path, it is very easy to create a maven project and run the example. Just create a Java file with the below code snippet:
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
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.
[table id=6 /]Template Engines Support
Spring Boot provides auto-configuration support for the following template engines:
- FreeMarker
- Groovy
- Thymeleaf
- Velocity
- Mustache
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:
- EhCache
- Hazelcast
- Infinispan
- Any JCache (JSR 107) implementation
- Redis
- Guava
- Simple Map based in-memory cache also supported in the auto configuration
You can read how to use caching with spring boot application for more details. Prior to 1.3.0, there is no auto-configuration for the caching technologies. Also actuator endpoint is enabled for the caching.
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
- 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:
- 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 Tutorials
- Spring Boot : RESTful API using Spring Boot and MongoDB : This tutorial explains how to implement RESTful web service using SpringBoot and MongoDB. This tutorials uses already implemented REST services using Node.js and ExpressJS frameworks.
- 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.
- Spring Data + MongoDB + REST Shell Integration : This tutorial guides you through an example for understanding the integration between Spring Data, Spring REST and MongoDB. As you are going to use the rest-shell for achieving different operations against database.
- 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.
- MySQL Configurations : This tutorial explains how to configure MySQL database for your spring boot applications.
- 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 Security Article : This tutorial focuses more about the core module of spring security and one simple example that demonstrates the core functionality.
Spring Boot Videos
Here is the one good video on Spring Book introduction by Josh Long.
Spring Boot Adoption
Spring Boot Adoption on March 2016:
Eugen has published his original report here.
Spring Boot Adoption on 2015:
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.
Spring Boot Articles
- HOW-TO: SPRING BOOT AND THYMELEAF WITH MAVEN
- What you can build for free in 2 hours with Spring Boot, Twitter and Facebook
- How to use spring boot on Open Shift
- OpenUI5 boilerplate based on OLingo, JPA and Spring Boot
Why Spring Boot
- 6 Simple Reasons Why Spring Boot Rocks
- Java Bootstrap: Dropwizard vs. Spring Boot
- Should You Use Spring Boot in Your Next Project?
- Here is a nice forum post about Spring Boot vs Other Spring Projects by Craig Walls
- Reasons to consider spring-boot for your next Spring based application!
- Spring Boot is still a gem…waiting to be discovered
Micro Services
Performance
Eclipse & NetBeans
Deployment
Recommended Books
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.