Spring Boot is a tricky framework to understand. In this tutorial for beginnners with Spring Boot, we will look at the basics of Spring Boot and help you understand the important concepts — Starter Projects, Auto Configuration, and Starter Parents.

Understanding Spring Boot's Architecture

The building blocks of Spring Boot are:

  • Spring Boot Starter Projects
  • Spring Boot Starter Parent
  • Auto Configuration

We will start with understanding what Spring Boot wants to achieve by comparing it with Spring and Spring MVC. Once you understand that, you should be in a good position to start with the building blocks of Spring Boot.

Spring Boot vs. Spring MVC vs. Spring

The most important thing to understand is:

Spring Boot does not compete with Spring or Spring MVC. It makes it easy to use them.

Spring Framework

The most important feature of Spring Framework is Dependency Injection. At the core of all Spring Modules is Dependency Injection or IOC — Inversion of Control.

When DI or IOC is used properly, we can develop loosely coupled applications. And loosely coupled applications can be easily unit tested.

Spring MVC

Spring MVC provides a decoupled way of developing web applications. With simple concepts like Dispatcher Servlet, ModelAndView, and View Resolver, it makes it easy to develop web applications.

Spring Boot

The problem with Spring and Spring MVC is the amount of configuration that is needed:

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<mvc:resources mapping="/webjars/**" location="/webjars/"/>

Spring Boot solves this problem through a combination of Auto Configuration and Starter Projects. Spring Boot also provides a few features to make building production-ready applications faster.

The following article digs deeper and gives you a full-blown comparison between Spring, Spring MVC, and Spring Boot.

Title Category URL Github
Spring Boot vs. Spring MVC vs. Spring - How do they compare? Spring Boot Basics URL  

Spring Boot Auto Configuration

Spring and Spring MVC applications have a lot of XML or Java Bean Configuration.

Spring Boot brings in a new thought process around this.

Can we bring more intelligence into this? When a Spring MVC JAR is added into an application, can we auto configure some beans?

  • How about auto configuring a Data Source if a Hibernate JAR is on the classpath?
  • How about auto configuring a Dispatcher Servlet if a Spring MVC JAR is on the classpath?

There would be provisions to override the default auto configuration.

Spring Boot looks at a) Frameworks available on the CLASSPATH b) Existing configurations for the application. Based on these, Spring Boot provides the basic configuration needed to configure the application with these frameworks. This is called Auto Configuration.

The following article explores Auto Configuration in depth.

Title Category URL Github
What is Spring Boot Auto Configuration? Spring Boot Basics URL  

Spring Boot Starter Projects

Here’s what the Spring Boot documentation says about starters.

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.

Let’s consider an example starter — Spring Boot Starter Web.

If you want to develop a web application or an application to expose RESTful services, Spring Boot Start Web is the starter to pick.

The following screenshot shows the different dependencies that are added into our application when you add Spring Boot Start Web into the dependencies of your project.

Image

This is a wide variety of components that are typically used to develop web applications. Spring Boot Starter Web brings them together and provides a simple approach to use them.

  • Spring — core, beans, context, AOP
  • Web MVC — (Spring MVC)
  • Jackson — for JSON Binding
  • Validation — Hibernate Validator, Validation API
  • Embedded Servlet Container — Tomcat
  • Logging — logback, slf4j

Any typical web application would use all these dependencies. Spring Boot Starter Web comes pre packaged with these. As a developer, I would not need to worry about these dependencies or their compatible versions.

Spring Boot provides a wide range of starter projects. Spring Initializr suppports all of them and more. Among the varied range of starter projects and options supported are:

  • spring-boot-starter-web-services: Build applications exposing SOAP web services
  • spring-boot-starter-web: Build Web applications and RESTful applications
  • spring-boot-starter-test: Write great unit and integration tests
  • spring-boot-starter-jdbc: Traditional JDBC applications
  • spring-boot-starter-hateoas: Make your services more RESTful by adding HATEOAS features
  • spring-boot-starter-security: Authentication and authorization using Spring Security
  • spring-boot-starter-data-jpa: Spring Data JPA with Hibernate
  • spring-boot-starter-cache: Enabling Spring Framework’s caching support
  • spring-boot-starter-data-rest: Expose simple REST services using Spring Data REST

You can explore starter projects in depth with the article below.

Title Category URL Github
Initializing Projects with Spring Boot Starters - Web and JPA Spring Boot Basics URL  

Spring Boot Starter Parent

All Spring Boot projects typically use spring-boot-starter-parent as the parent in the pom.xml.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

Parent POMs allow you to manage the following things for multiple child projects and modules:

  • Configuration: Java version and other properties
  • Depedency Management: Version of dependencies
  • Default plugin configuration

You can read more about Spring Boot Starter Parent at the article below.

Title Category URL Github
Introduction to Spring Boot Starter Parent Spring Boot Basics URL  

Spring Initializr: Create Spring Boot Projects at F1 Speed

Spring Initializr is great tool to bootstrap your Spring Boot projects.

It allows you to create a varied range of Spring Boot-based applications from a very simple UI. Some of the types of applications you can bootstrap are:

  • Web applications
  • Restful applications
  • Batch applications

Let’s take the example of creating a web application with Spring Initializr.

Image

As shown in the image above, the following steps have to be taken:

  • Launch Spring Initializr (http://start.spring.io/) and choose the following 
    • Choose com.in28minutes.springboot as the Group
    • Choose student-services as the Artifact
    • Choose following dependency 
      • Web
  • Click the Generate Project button at the bottom of the page.
  • Import the project into Eclipse.

The following article delves more into Spring Initializr.

Title Category URL Github
Spring Initializr - Bootstrap Your Spring Boot Applications at F1 speed! Spring Boot Basics URL  

Spring Boot and Embedded Servers

When we create a deployable application, we can embed the server (for example, Tomcat) inside the deployable.

For example, for a Spring Boot Application, you can generate an application JAR that contains Embedded Tomcat. You can run a web application as a normal Java application!

An embedded server implies that our deployable unit contains the binaries for the server (for example, tomcat.jar).

Let’s take a quick look at the dependencies for spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.0.0.M6</version>
    <scope>compile</scope>
</dependency>

You can see that, by default, Starter Web includes a dependency on the Starter Tomcat.

  • Tomcat is the default embedded server for Spring Boot.
  • Spring Boot also supports Jetty and Undertow.

The following article explores more about embedded servers:

Title Category URL Github
Spring Boot and Embedded Servers - Tomcat, Jetty, and Undertow Spring Boot Basics URL Project Code on Github

Spring Data

From http://projects.spring.io/spring-data/:

Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services.

To make it simpler, Spring Data provides abstractions (interfaces) you can use irrespective of underlying data source.

Some of the sub modules in Spring Data are:

  • Spring Data JPA — relational databases
  • Spring Data MongoDB
  • Spring Data REST — Expose awesome REST APIs around Spring Data Repositories

The following articles digs deeper into Spring Data.

Title Category URL Github
Introduction to Spring Data - Spring Data JPA, Spring Data REST, and MongoDB Spring Data URL  

Other References