Spring Boot – Introduction

Spring Boot - Introduction to Spring Boot

Spring Boot is a tool that have become very popular and useful. So what is Spring Boot anyway? Spring Boot is an open source frame work used to create microservices. It is java based. So if you are a Java developer, then you have a decent head-start.

With Spring Boot, you can build stand-alone application that are production ready. This is the first in the series of simple course on Spring-Boot. Also remember to watch the video lesson at the end of the each lesson (bottom of this page).

Watch the Video

  1. What are Microservices?
  2. Introduction to Spring Boot
  3. Features of Spring Boot
  4. How Spring Boot Work
  5. Spring Boot Starters
  6. Auto Configuration

 

1. What are Microservices?

Microservice is an application development architecture that enables you to build and deploy services that run independently. So in this an application is would consist of a number of independent microservices.

In this architecture, each service is lightweight and runs its own processes. At the end of the day each microservice, though independent work together with other microservices to achieve a complete business application.

 

Benefits of Microservices

  • require minimum configuration
  • ease of deployment
  • they are highly scalable
  • relatively lower development/deployment time
  • can be used with containers

 

2. Introduction to Spring Boot

As previously mentioned, Spring Boot provides you all the tools and platform needed to build stand-alone production-ready Spring application that you can just run. Now, notice that we mentioned ‘Spring application’. This means application based on the Spring framework.

With Spring Boot, you can boot strap your application with the initial configuration already in place for you.

 

Benefits of Spring Boot

In addition to the benefits provided by Microservices, Spring Boot also offers he following benefits:

  • Spring Boot is super easy to use especially for new developers
  • Helps you achieve more with less effort
  • Development time is very minimal
  • Helps you circumvent complex configuration steps typical of web applications
  • Less code

 

3. Features of Spring Boot

The features of Spring Boot include the following:

Auto-configuration: You have almost everything auto-configured in Spring Boot. Again, this means less coding effort.

Flexibility: Spring Boot allows for easy configuration of XML configuration, Java Beans and Database Transactions.

REST Endpoint: It provides for easy management of REST endpoints

Annotation-based: Spring application are annotation based

Dependency Management

Embedded Servlet Container

 

4. How Spring Boot Work

Once you start a Spring application using Spring Boot,  your application is automatically configured (autoconfiguration). This auto-configuration is based on the dependencies that you have specified for your project using @EnableAutoConfiguration annotation. For example, if you have MySQL database on the classpath but have not configured databases connection, then Spring Boot will automatically configure an in-memory database for the application.

All Spring Boot application must have an entry point. The entry point is the class that contains the main method and is annotated with the @SpringBootApplication annotation.

Another annotation is the @ComponentScan. This makes spring boot automatically scan all the components that are in the class path.

 

5. Spring Boot Starters

Spring Boot starters are what you add to your project that allows spring boot set up the initial dependencies and configuration. An example of spring boot starter is:

 

spring-boot-starter-data-jpa

 

So this is a starter you use to tell Spring that you want to use jpa(Java Persistence API) for data access in your project. To use this starter, simply add the code below :

 

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

 

Another example is the one below:

spring-boot-starter-web

this is used to configure that application for creating REST API endpoints. To use the web starter, you add the code below:

 

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

 

6. Auto Configuration

One thing you should always remember about spring boot is auto-configuration. This means that Spring Boot automatically configures your application. The auto-configuration is based on the JAR dependencies added to your project.

For this to work, you need to add the @EnableAutoConfiguration annotation or the @SpringBootApplication to the entry point (the class that has the main method).

Also note that if you use the Spring IDE (spring-sts) to create your project, then these annotations are automatically added. You don’t need to add them yourself.

This is what the main class file of the spring boot application looks like:

 

package com.kindsonthegenius.demo;

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

@SpringBootApplication
public class FirstAppApplication {

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

}

 

The @SpringBootApplication annotation encompasses @EnableAutoConfiguration, @ComponentScan and @SpringBoot Configuration.

The @ComponentScan annotation to tell Spring Boot to scan the components added to the project.

Watch the Video