What is Spring Boot?

Codecademy Team
Learn some of the underlying technology powering the Spring framework

What We’ll Be Learning

In a previous article, we learned how components of a Spring application are configured as Spring beans and injected as dependencies into other components by the Inversion of Control (IoC) container. For example, one component of web applications is the controller, and we can mark a class as a controller using the @RestController annotation.

import org.springframework.web.bind.annotation.*;
@RequestMapping("/restaurants")
@RestController
public class RestaurantController {
}

When our app is run, this controller class will be registered as a bean and used to define routes available in our API.

If you run the application and look closely at the terminal output, you’ll notice that our app is doing a lot more than running the controller code we defined. For example:

  • A Spring Data JPA repository is bootstrapped
  • A Tomcat server is initialized
  • A WebApplicationContext is initialized
  • An H2Dialect is used
  • We see an option to Explicitly configure spring.jpa.open-in-view
  • The Tomcat server is started on port(s): 4000
$ ./mvnw spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

........ Starting CoffeeOrdersApplication using Java 11.0.10...
........ No active profile set, falling back to default profiles: default
........ Bootstrapping Spring Data JPA repositories in DEFAULT mode.
........ Finished Spring Data repository scanning in 42 ms. Found 1 JPA repository interfaces.
........ Tomcat initialized with port(s): 4000 (http)
........ Starting service [Tomcat]
........ Starting Servlet engine: [Apache Tomcat/9.0.46]
........ Initializing Spring embedded WebApplicationContext
........ Root WebApplicationContext: initialization completed in 1369 ms
........ HHH000204: Processing PersistenceUnitInfo [name: default]
........ HHH000412: Hibernate ORM core version 5.4.31.Final
........ HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
........ HikariPool-1 - Starting...
........ HikariPool-1 - Start completed.
........ HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
........ HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
........ Initialized JPA EntityManagerFactory for persistence unit 'default'
........ spring.jpa.open-in-view is enabled by default. 
........ Therefore, database queries may be performed during view rendering. 
........ Explicitly configure spring.jpa.open-in-view to disable this warning
........ Tomcat started on port(s): 4000 (http) with context path ''
........ Started SpringCapstoneApplication in 9.187 seconds (JVM running for 9.726)
........ Application availability state LivenessState changed to CORRECT
........ Application availability state ReadinessState changed to ACCEPTING_TRAFFIC

This raises a few questions: How was that controller converted into a bean? Where are Spring Data JPA, H2Dialect, and Tomcat coming from? Who told them to start up like that? Where do I “configure” things?

The reason is Spring Boot! Spring Boot is a collection of tools that extends the Spring framework and makes it easier to build applications quickly. There are too many Spring Boot tools to cover in one article, so we will focus on these four, which will answer our questions above:

  1. “Starters”
  2. Auto-configuration
  3. Custom configurations with application.properties
  4. Distribution of your application

By covering these four features, you’ll begin to see how Spring Boot is added to our Spring projects and how it makes building apps easier.

Downloads

Every Spring project generated by start.spring.io includes Spring Boot, so the code included in this article will look similar to most applications you write.

To keep things specific, however, we’ve provided an example application so that you can follow along throughout the article.

Download the example Spring Boot application here and double-click on the .zip file to uncompress it.

Starters

If you look at the pom.xml of our Spring Boot application, you’ll see the spring-boot-start-jpa and spring-boot-starter-web dependencies.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

These are called Spring Boot “starters”. Each one represents a set of dependencies with which to run our application. For example, the spring-boot-starter-web dependency instructs Spring Boot to configure Tomcat for running a server and Spring MVC (Model-View-Controller) for routing HTTP requests. spring-boot-starter-data-jpa sets up JPA and Hibernate for database access.

There are other starters like spring-boot-starter-test which includes common testing libraries such as JUnit Jupiter, Hamcrest, and Mockito, and spring-boot-starter-parent, which provides the spring-boot:run command.

Auto-Configuration

We understand where dependencies like Tomcat are defined, but how are they hooked into our application?

If we look at the two imports at the top of SpringCapstoneApplication.java, we’ll see the SpringApplication class and @SpringBootApplication annotation from the org.springframework.boot package:

package com.codecademy.springcap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringCapstoneApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCapstoneApplication.class, args);
}
}

SpringApplication

Remember when we said that the Spring IoC container configures beans for us? The SpringApplication class is responsible for starting up that container. We can see that in the line:

........ Initializing Spring embedded WebApplicationContext

In Java, the IoC container is represented by an application context. This is the thing that receives beans and enables Spring to inject them into other components. In Java, the application context appears as a ApplicationContext interface. In our specific case, this app is using the class WebApplicationContext, a web-specific implementation of ApplicationContext.

@SpringBootApplication

The @SpringBootApplication annotation is equivalent to @Configuration, @ComponentScan, and @EnableAutoConfiguration. The first two are from the Spring framework, but the third, @EnableAutoConfiguration, is from Spring Boot.

This annotation enables Spring Boot to review our dependencies (such as spring-boot-starter-jpa and h2) and assume the intended purpose of the application. It will configure and run the application to best fit the assumed purpose.

This explains the Spring Data JPA, Tomcat, and H2Dialect lines in our original code snippet: Spring Boot knows to bootstrap JPA repositories, start a Tomcat server, and use an H2 dialect because spring-boot-starter-jpa, spring-boot-starter-web, and h2 are listed as dependencies in the pom.xml.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Here’s another way to explain it, from the Spring documentation:

@EnableAutoConfiguration…tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

Webb, Phillip et al. “Spring Boot Reference Documentation“. Accessed June 29, 2021.

Custom Configuration with application.properties

In our original terminal output, we saw the lines:

........ Explicitly configure spring.jpa.open-in-view to disable this warning
........ Tomcat started on port(s): 4000 (http) with context path ''

When we start our application with ./mvnw spring-boot:run, Spring Boot does some tasks by default: it makes some assumptions, like using port 8080, but it also checks for additional configuration instructions in the application.properties file. This allows us to make high-level changes to our application without writing any Java code.

For example, you can override the default 8080 port using:

server.port=4000

You can disable JPA warnings:

spring.jpa.open-in-view=false

Or customize the level of logging in your application:

logging.level.org.springframework=DEBUG

In the case of the downloadable application we provided, the application.properties file customizes the database behavior, such as the local file to use for storage:

spring.datasource.url=jdbc:h2:file:~/springcap

A longer list of application properties are available in the Spring docs.

Distribution

So far we’ve solved the mysteries of that Spring Boot terminal output, which appeared on your own computer. But what about the output that appears on Codecademy when you run a Spring Boot app?

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.codecademy:ccapplication >--------------------
[INFO] Building codecademy-learning-environment-app 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ ccapplication ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ ccapplication ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /home/ccuser/workspace/spring-capstone/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.162 s
[INFO] Finished at: 2021-06-30T00:45:54Z
[INFO] ------------------------------------------------------------------------

Java applications are often packaged into an executable file called a jar, which you can see in the above code. Spring Boot provides an easy way to bundle your code and dependencies into a jar via the spring-boot-maven-plugin dependency. This is included by default when we download a project from start.spring.io.

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

When we run applications on our Codecademy platform–or when you deploy your next app to the cloud–you are using Spring Boot to bundle your code and dependencies into a jar.

Note that there are other Spring Boot deployment tools such as Spring Boot Actuator, but they are not necessary to understand this article.

Practice

You’ve made it to the end! We’ve provided some assessments here for you to check your understanding. If you don’t feel confident in answering these questions, that’s okay! Spend some more time with the article above and try again later.