Master java skills

What is Spring Boot?

Spring Boot is an open source framework built on Spring framework and maintained by a company called Pivotal. This framework is used to create microservices. Spring Boot provides developers a platform where they can build auto-configurable, production grade Spring applications. It saves a lot of time when it comes to make production ready applications quickly.

Spring Boot framework comes with many predefiend starter kits which includes configurations required to build and run that module. Using this feature, developers can focus on their business logic completely with minimal/or no configuration at all. Before taking a deep dive in Spring Boot, let’s understand the difference between Spring and Spring Boot.

1. Difference between Spring and Spring Boot

Spring Spring Boot
A framework that supports comprehensive infrastructure support to develop java applications.An extension of spring framework with embedded server and many other features.
Prefers convention over configurationTakes convention over configuration to the next level by registering the default configurations automatically on the basis of classpath libraries.
Does not provide production like features.Provides production like features healthchecks, metrics and externalized configurations.
We have to maintain dependencies with compatible versions ourselves in pom.
Provides starter dependencies which makes easier to maintain dependent libraries and their compatible versions.
Does not have an embedded server. We have deal with the deployment complexities ourselves.Comes with an embedded server which makes it easy to deploy and test applications fast.
We have to write a lot of boilerplate configuration code.A lot of configuration is handled automatically. So, manual configuration is significantly reduced.
Doesn’t take an opinionated approach. We have to define a lot of configuration code to make the application work. It takes an opinionated approach meaning it has its own templated method of working of a particular type of application. We only have to fit our business logic into it. Thus it is prone to very less configuration mistakes when compared to spring framework.

2. Features of Spring Boot

There are many features of Spring Boot, but we will talk about few important ones here.

2.1 Auto Configuration

This is the most powerful feature of Spring Boot. This means it does a lot of configuration required on its own intelligence. For example, if we add spring-boot-starter-web dependency in our pom.xml, Spring Boot automatically configures Spring MVC DispatcherServlet and Tomcat server. Similarly, if we add spring-boot-starter-data-jpa and h2 dependency, it automatically configures an in memory database h2 and JDBCTemplate. Similary if we add spring-boot-starter-security, then it will add basic security to the service.

Note -> Spring Boot can make more than 200 such decisions based on jar in its classpath.

Note -> Autoconfiguration is disabled by default. You have to enable it either by using @EnableAutoConfiguration or by @SpringBootApplication.

2.2 Starter POMs

Just as autoconfiguration solves the problem of doing common configurations, starter poms solves the problem of maintaining common dependencies and their compatible versions. Let’s take an example: we want to create one spring based REST application, then we will require many dependencies like : spring-core, spring-web, spring-webmvc, and many more depending on the specific requirement. But if we want to do the same thing using spring boot, then we only need to have one dependency spring-boot-starter-web. It will add all the related dependencies.

There are several starter kits available to use in springboot. Few of them are as follows:

  1. spring-boot-starter-web
  2. spring-boot-starter-test
  3. spring-boot-starter-actuator
  4. spring-boot-starter-data-jpa
  5. spring-boot-starter-mail and so on

2.3 Actuator

Actuator is an excellent feature that helps developers create applications with facilities like analysing all the metadata of a running service. We can get information about various metrics like memory usage, cpu usage, heapdump, health of service, threaddump, environment variables, system properties etc. Following are some of the examples of predefined endpoints in actuator:

Note -> this is how we can use actuator in the url :

http://localhost:8080/actuator/info

http://localhost/8080/actuator/beans

  • /info – returns general information about the service
  • /health – returns the health status of the service. Whether service is up and down.
  • /beans – gives info about all the beans registered with application context.
  • /auditevents – audit events like login/logout etc.
  • /conditions – builds a report of conditions about autoconfiguration
  • /env – gives current environment properties
  • /configprops – all configuration properties beans
  • /logfile – appliation logs
  • /loggers – enables to query or modify logging level of our application
  • /heapdump – returns heap dump information
  • /threaddump – thread information
  • /metrics – detailed metrics of our application including memory and cpu usage
  • /scheduledtasks – information about any scheduled tasks within the service
  • /shutdown – shuts down the service
  • /sessions – lists HttpSession objects

2.4 Spring Boot CLI

CLI is a command line interface provided by Spring Boot. This can be used to create spring based web applications using groovy programming language. Using the benefits of Spring Boot and Groovy, we can compltely focus on business logic. CLI knows which starter dependencies to configure based on the annotations used. CLI takes autoconfiguration to alltogether new level.

For example, when CLI encounters @Controller and @RequestMapping annotations, it knows it has to configure spring-boot-starter-web dependency.

2.5 Spring Boot Initializer

Spring Boot Initializer is a web application which helps developers to create the basic structure of their project using maven or gradle. The language can be chosen as Java, Kotlin, Groovy or Spring Boot. The developers can also specify which dependencies they want to use. Refer the below screenshots.

The Spring Initializer project can be accessed using below url.

https://start.spring.io/

in the above project, we have added Spring Web dependency