What is Spring Boot

Learn via video courses
Topics Covered

Overview

Spring framework is a popular open-source framework for creating enterprise production-grade applications.

Over the years, the spring framework has become the default choice for building enterprise applications. Spring framework provides dependency injection and easy integration with different technologies RDBMS, NoSQL, Middleware, etc.

What is Spring Boot?

So, what is spring boot, and why is so much buzz about it?

Definition of spring boot from official website:

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run".

SPRING BOOT

Let's break individual key characteristics from the definition.

  • Stand-alone - A stand-alone application can run and execute independently. Any class with the main method can run independently. It does not need any external software to run.
  • Production-grade - The application can be taken to production quickly without worrying about the observability part. The application can be integrated with varieties of other systems with minimal effort.
  • Just-run - Spring boot generates a runnable jar. It needs only JVM to run the application.

Advantages of using spring boot

  • The primary advantage is spring boot offers an effortless way to create spring-based applications using JAVA or Groovy.
  • Spring boot helps accelerate application development by reducing all the manual work of writing annotations, boilerplate codes, and XML configurations.
  • Following the 'Opinionated Defaults Configuration' reduces the developer's efforts.
  • Spring boot generates a runnable jar; therefore no need for a servlet container to deploy the application.
  • It provides one of the key principles of the 12 Factor app, Separate config from APP.

Spring Boot Features

Although Spring Boot provides many features, the three below are the most important.

  • Simplify Dependency Management A set of JARs is required for a Java application to function properly. If you have spent time developing software, you are probably familiar with the headaches of dependency management. This problem multiplies when we consider versions. One version of a particular dependency can work with only a specific version of other dependencies. To overcome dependency issues spring boot provides starters JAR, those are Bills of Material (BOM), which provide packaged dependency of JARs compatible with each other.

  • Simplify Deployment Spring Boot generates a self-executable fat JAR where all the dependencies are packaged inside the JAR (including the container).

    Generated spring boot JAR is called Fat JAR or Uber JAR.

    Once you have the fat JAR, it can run on any system where Java virtual machine is available, using the command Java –jar <SpringBootAppName.jar>.

  • Auto-configuration It is the most powerful feature of the spring boot. As the name suggests, Spring Boot auto-configuration is the framework's intelligence to automatically configure a Spring application based on the dependencies on the classpath.

    • Configure a DispatcherServlet, if the Spring MVC JAR is on the classpath.
    • An EntityManager, if JPA JAR is on the classpath.

    This intelligence is provided by spring boot as auto-configuration.

Spring Boot Starters

Spring boot provides starter jars to simplify dependency management. Let's see some of those and their purpose.

Spring Boot Starter Web Dependency

Dependency spring-boot-starter-web adds web capability to the project. It brings in all the required dependencies for the application to expose the REST endpoint.

SPRING FRAMEWORK

SPRING BOOT STARTER

Adding this dependency will bring all the above jars with it and make your application REST ready.

Spring MVC

Web starter inherently adds and configures MVC(Model-view-controller) to the application. No need to separately configure the application for the same.

Spring Boot Starter Security Dependency

Dependency spring-boot-starter-security is required to secure the application using authentication and authorization. Spring security is covered in a separate article.

Spring Boot Starter Actuator Dependency

Monitoring and observability are crucial to any application, and they rely on the application emitting effective health-checking metrics. Dependency spring-boot-starter-actuator adds observability to the application. Once the Spring Boot Actuator is added, the following metrics are enabled out of the box:

  • JVM metrics (related to GC and thread)
  • Application's overall health
  • Resource utilization metrics (CPU, threads, file descriptors, JVM heap, and garbage collection metrics)
  • Kafka consumer metrics
  • Logger metrics (Log4j2, Logback)
  • Availability metrics (process up-time)
  • Cache metrics (out of the box for Caffeine, EhCache2, Hazelcast, or any JSR-107-compliant cache) Tomcat metrics
  • Spring integration metrics.

Spring Boot Starter Test Dependency

Dependency spring-boot-starter-test brings in all the dependencies required to write good unit and integration tests. The scope of this dependency should be "test".

Spring boot starter JPA dependency

Dependency spring-boot-starter-data-JPA provides an easy way to access and query different datastore types. It handles most of the repeated work and complexity related to database integration by eliminating boilerplate code. It provides no code repository.

Extending this interface will immediately add basic CRUD capability to the Employee entity without a single line of code. Spring Data JPA provides three types of repositories.

  • CRUD repository - Add basic create, read, update and delete capabilities to the entity.
  • PagingAndSortingRepository - It extends the CrudRepository and adds specific methods to sort and retrieve the data in a paginated way.
  • JPA Repository - Extends both CRUD and PagingAndSortingRepository.

Great example of interface segregation principle.

Interface Segregation Principle

Spring Boot Auto-configuration

Spring Boot auto-configuration is the framework's intelligence to automatically configure a Spring application based on the dependencies on the classpath.

SPRING BOOT AUTOCONFIGRATION

How does it Works?

Spring boot functions with the help of the @SpringBootApplication annotation.

Understand @SpringBootApplication

Spring Boot has revolutionized how Java applications are created. Perhaps the most important annotation is @SpringBootApplication. Let’s dive in and find out why.

Let’s do one experiment: remove the @SpringBootApplication annotation from the application's main class and see what happens. We should see the below error.

This annotation has special significance, which is why it’s applied to the class with the main method. Underneath @SpringBootApplication is a combination of the following three annotations.

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

Out of the above, the most important one is @EnableAutoConfiguration.

Understand @EnableAutoConfiguration

This annotation enables the magical auto-configuration feature of Spring Boot. Now we know why the application failed during startup when we removed the @SpringBootApplication annotation. Even though we have a starter-web dependency available in the classpath, the auto-configuration was turned off due to the missing @SpringBootApplication annotation. As a result, the framework failed to configure the DispatcherServlet and other required beans.

Spring vs Spring Boot

SpringSpring boot
Generates runnable fat JarIt generates WAR to be deployed on web servers.
Application is to be deployed on the web server explicitly.Provides embedded web server.
Developers need to define and deal with dependency manually.Spring boot offers to simplify dependency management using starter jars.
Developers write a lot of code (boilerplate code) to do minimal tasks.Reduces boilerplate code.

Conclusion

  • In this article we understood what is spring boot and its advantages.
  • Spring boot simplifies application development and takes care of heavy lifting itself.
  • Spring boot allows developers to focus on business from day one without worrying about different integration points.
  • Spring boot has three foundation features
    • Simplify dependency management - Provide starter JARs to simplify dependency management.
    • Simplify deployment - Generates bootable jar to simplify deployment. Different web servers supported by spring boot are Tomcat, Jetty, and Undertow.
    • Autoconfiguration - This is the most important feature of the spring boot. The framework has the intelligence to configure system defaults based on JAR on the classpath.
  • Annotation @SpringBootApplication enables autoconfiguration with the help of @EnableAutoConfiguration annotation.