3 Core Features of Spring Boot: All You Need To Know

3 Core Features of Spring Boot: All You Need To Know

Introduction

To build a complete web application a lot of frameworks are required. For example:

  • REST APIs: Spring, Spring MVC, Tomcat, JSON conversion etc

  • Unit Testing: Spring Test, JUnit, Mockito etc

Spring Boot provides following three core features to quickly build applications.

1. Simplified Dependency Management


If you're providing a RESTful web API there should be a way to expose endpoints over HTTP, listen for requests, bind those endpoints to methods that process those requests, build and return appropriate responses. Each dependency incorporates several other dependencies to fulfil the entire functionality.

Also, there is a question of version incompatibility. One version of a particular dependency might be compatible with only specific version of some other incorporated dependency resulting in hours of wasted manual work!

Spring Boot Starter Dependencies

The Design-Development-Usage pattern doesn't vary much across web applications. It is an industry-wide approach with a few customisations. Adding a spring-boot-starter-web provides all functionalities in a single application dependency which are version compatible too. Although you do have an option to override the tested version with some other version of dependency which provides you the expected functionality - at your own risk and testing!

pom.xml

Spring Boot provides a variety of starter projects:

  • Web Application: spring-boot-starter-web, spring-boot-starter-tomcat, spring-boot-starter-json etc.

  • Unit Testing: spring-boot-starter-test

  • JPA: spring-boot-starter-data-jpa

  • JDBC: spring-boot-starter-jdbc

  • Security: spring-boot-starter-security

2.Simplified Deployment


JAR Deployment

In early days, deploying a Java application involved complex tasks such as:

  1. Installing and configuring the Application server

  2. Installing database drivers

  3. Creating a database connection

  4. Build and test your application

  5. Deployment of application and its dependencies to the Application Server.

When you build a Spring Boot application, it packages the application along with an embedded server into a single executable JAR file.

  • To build the project
mvn clean package
  • Locate the jar file
target/springbootapplication-0.0.1-SNAPSHOT.jar
  • Run the jar file
java -jar target/springbootapplication-0.0.1-SNAPSHOT.jar
  • After building the project with Maven, the directory structure will be as follows
springbootapplication/
├── target/
│   └── springbootapplication-0.0.1-SNAPSHOT.jar
├── src/
│   └── main/
│       └── java/
│           └── com/
│               └── example/
│                   └── SpringBootApplication.java
├── pom.xml

Application Properties

To simplify the setup process and centralize the configuration, a file named application.properties is used. For example:

application.properties

server.port=8080
spring.database.url=jdbc:mysql://localhost:3306/mydb
spring.database.username=root
spring.database.password=pwd

# Specify the active profile
spring.profiles.active=dev

In real world applications there are usually multiple environments such as DEV, QA, UAT, PPD and PRD wherein environment specific properties need to be configured. Spring Boot allows you to define profiles. For example:

application-dev.properties

spring.database.url=jdbc:mysql://localhost:3306/devdb
spring.database.username=devuser
spring.database.password=devpwd
logging.level.org.springframework=trace

application-prod.properties

spring.database.url=jdbc:mysql://prod-server:3306/proddb
spring.database.username=produser
spring.database.password=prodpassword
logging.level.org.springframework=info

3.Auto Configuration


To build a typical Spring application a lot of configuration such as Component Scan, Data Sources, Dispatcher Servlet, JSON conversion need to be configured.

Every application has some common set of tasks that are performed repeatedly in a certain way on frequent basis. For example: Every time there is a need to access database, a connection needs to be established and a set of operations are performed to retrieve/store data and then the connection is closed.

Automated Configuration of an application can be decided based on what all frameworks are present in class path and the existing configuration. All the auto configuration logic is defined in spring-boot-autoconfigure-<version>.jar which contains org.springframework.boot.autoconfigure.web package. Using Spring Boot Starter Web below things are autoconfigured:

  • DispatcherServletAutoConfiguration: Dispatcher Servlet responsible for handling all incoming HTTP requests and dispatching them to the appropriate handlers (controllers).

  • EmbeddedWebServerFactoryCustomizerAutoConfiguration: Embedded Servlet Container Tomcat (default)

  • ErrorMvcAutoConfiguration: Default error pages (displaying white label errors)

  • JacksonHttpMessageConvertersConfiguration: Bean to JSON conversion

Spring Boot Dev Tools

To increase the developer productivity without requiring to restart the server manually for every code change, dev tools are used. It automatically restarts the server and picks up the code or property file changes. In pom.xml, add below dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

Note: Every time you make a change to pom.xml, you'll need to restart the server.

Summary

This article outlines three core features of Spring Boot that streamline the development of web applications. It highlights simplified dependency management through Spring Boot starter dependencies, which bundle multiple functionalities and ensure version compatibility. Simplified deployment reduces the complexity of traditional deployment methods. Lastly, the article explains Spring Boot's auto-configuration capabilities, which automatically configure common tasks and frameworks, and introduces Spring Boot Dev Tools for enhanced development productivity by enabling automatic server restarts upon code changes.