Spring Boot – Overview

Table of Contents
Why Spring Boot?
What is Spring Boot?
Spring – Problems
Spring Boot – Solutions
Getting Started with Spring Boot
Insights
Packaging and Running Spring Boot Application
Actuator

1. Why Spring Boot?

Spring Boot lets you bootstrap the Spring based application from the scratch.

Official Definition: “Spring Boot makes it easy to create stand-alone, production-grade Spring based applications that you can just run

Stand-alone: Runs on its own
Production Grade: Production ready applications
Just Run: No much configuration required to start the application

Pre-requisites:
Basic understanding of Java 8
Basic understanding of Spring

2. What is Spring?

Spring is much more than Dependency Injection.

2.1 Application Framework

Spring provides complete solution to build enterprise applications. It also has programming and configuration model.
Spring solves most of the common problems
For example-
1. You need transaction services in you business layer – Spring provides the template for the same.
2. You need to connect with database – Spring provides JDBC template for the same.
3. You need some ORM framework to interact with databases – Spring provides integration for popular ORM frameworks like Hibernate.
4. To solve common cutting concerns – Spring AOP
and many more…

2.2 Programming and Configuration model

Spring lets you to just focus on business logic rather than worrying about lot many configuration related things. Spring provides simpler way to configure these.
1.2.1 Handling HTTP requests
1.2.2 Connecting to database
1.2.3 Annotations (e.g. just annotate with @Controller and Spring knows its a Controller so it provides whole lot of functionalities to it and manages the life cycle)

2.3 Infrastructure Support

1. Support for connecting to different databases

3. Spring – Problems

2.1 Huge Framework

2.2 Multiple Setup Steps

2.3 Multiple Configuration Steps

2.4 Multiple Build and Deployment Steps

4. Spring Boot – Solutions

4.1 Opinionated Framework

It resolves 80% of your problems with setup and configuration and allows you to tweak if necessary

4.2 Convention over Configuration

4.3 Stand-alone

4.4 Production Ready

5. Getting Started with Spring Boot

As mentioned above, Spring is opinionated framework and it provides convention over configuration. Spring achieved these by creating a separate project called spring-boot-starter-parent and put all of the configuration inside this project so it roughly covers 80% of configurations. Configurations are defined in parent project and you can inherit the configurations in your child project.

5.1 Creating Spring Boot Application

There are 4 ways to create Spring Boot application By using:
1. Maven Project (Manually)
2. Spring Intializr (Web Interface)
3. Spring Boot CLI
4. IDE – Through Wizard (STS is recommended over Eclipse IDE)

To create Spring Boot application by using Maven project (Manual way of creating Spring Boot application), follow the below steps:
Step 1: Create simple Maven project

Step 2: Add below parent project in your pom.xml

<!-- Below configuration tells that our project is child of this parent project -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.0.RELEASE</version>
</parent>

Step 3: Create a class with main method and annotate with @SpringBootApplication

package org.techlearnings;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemo {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo.class, args);

	}

}

Step 4: Add below code in main method

package org.techlearnings;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemo {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo.class, args);

	}

}

And there you go. That’s it for creating Spring Boot application!!

So when we start Spring Boot application by below line of code:

SpringApplication.run(SpringBootSecurityApplication.class, args);

What does it do?
1. Sets up default configuration
2. Starts Spring Application Context
3. Performs class path scan
4. Starts tomcat server

Spring boot lets you add the starter dependencies (Also known as meta dependencies) which in turn pulls all the related Jars.
For example:
In Spring Boot, to create web application you need to add below starter dependency

<!-- Meta dependency -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

5.2 Customizing Spring Boot Application

To customize Spring Boot application, you can modify application.properties file based on your needs to alter default behavior.

Example
Configuring Server HTTP Port

server.port=9090

To get the common list of properties you can set in your application.properties file, refer below URL.
https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/html/common-application-properties.html

This URL provides you properties you can set in application.properties as well as corresponding default values. As mentioned above, you can modify the default values based on your needs to tweak Spring Boot application.

6. Insights

When Spring application starts, it does the class path scanning and registers all the beans available in the class path. All the classes annotated with @Controller or @RESTController, Spring maintains a map of all the available URLs in the classes/methods. When an HTTP request is made, Spring checks in map if this URL pattern is mapped or not.

If available: Spring executes the specific method registered with URL.
If not available: Returns /error page (Also when error page is not defined you will see very common error page called “Whitelabel Error Page”)

Spring takes care of converting Java objects into Json automatically.

Spring downloads the compatible Jars based on the version you define of Spring Boot parent project in pom.xml. This pre-set list of possible combinations of Jars that work well without issues is called “Bill of Materials“.

6.1 Benefits of Embedded Tomcat Server

1. Convenience
2. Servlet Container configuration is now Application configuration
3. Standalone application

7. Packaging and Running Spring Boot Application

To package Spring Boot application we can leverage the Maven command and package our application in jar or war format. It packages the application based on the configuration provided in our pom.xml file.

<packaging>jar</packaging>

Maven command to generate jar/war file:

mvn clean install

Go inside your project directory and you can run your stand-alone application (jar file) using below command:

java -jar target/sample-application-1.0.jar

8. Actuator

Actuator is a group of binaries which gets added to the classpath and enables us with few additional features related to monitoring our application after deployment.

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.