Introduction to Spring Boot

Introduction to Spring Boot





Spring Boot is an open-source Java-based framework used to create a micro Service. It is developed by the Pivotal Team and is used to build stand-alone and production-ready spring applications. This chapter will give you an introduction to Spring Boot and familiarizes you with its basic concepts.

What is MicroService?

Micro Service is an architecture that allows the developers to develop and deploy services independently. Each service running has its own process and this achieves the lightweight model to support business applications.

Advantages

Microservices offers the following advantages to its developers ?

Easy deployment

Simple scalability

Compatible with Containers

Minimum configuration

Lesser production time

What is Spring Boot?

Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring application that you can just run. You can get started with minimum configurations without the need for an entire Spring configuration setup.

Advantages

Spring Boot offers the following advantages to its developers ?

Easy to understand and develop spring applications

Increases productivity

Reduces the development time

Goals

Spring Boot is designed with the following goals ?

To avoid complex XML configuration in Spring

To develop a production ready Spring applications in an easier way

To reduce the development time and run the application independently

Offer an easier way of getting started with the application. For More Additional Information On Spring Boot, Go Through Spring Boot Online Training

Why Spring Boot?

You can choose Spring Boot because of the features and benefits it offers as given here ?

It provides a flexible way to configure Java Beans, XML configurations, and Database Transactions.

It provides a powerful batch processing and manages REST endpoints.

In Spring Boot, everything is auto configured; no manual configurations are needed.

It offers annotation-based spring application

Eases dependency management

It includes Embedded Servlet Container

How does it work?

Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.

The entry point of the spring boot application is the class contains @SpringBootApplication annotation and the main method.

Spring Boot automatically scans all the components included in the project by using @ComponentScan annotation.

Spring Boot Starters

Handling dependency management is a difficult task for big projects. Spring Boot resolves this problem by providing a set of dependencies for developers convenience.

For example, if you want to use Spring and JPA for database access, it is sufficient if you include spring-boot-starter-data-jpa dependency in your project.

Note that all Spring Boot starters follow the same naming pattern spring-boot-starter- *, where * indicates that it is a type of the application.

Examples

Look at the following Spring Boot starters explained below for a better understanding ?

Spring Boot Starter Actuator dependency is used to monitor and manage your application. Its code is shown below ?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

Spring Boot Starter Security dependency is used for Spring Security. Its code is shown below ?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

Spring Boot Starter web dependency is used to write a Rest Endpoints. Its code is shown below ?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

Spring Boot Starter Thyme Leaf dependency is used to create a web application. Its code is shown below ?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

Spring Boot Starter Test dependency is used for writing Test cases. Its code is shown below ?

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test<artifactId>

</dependency>

Auto Configuration

Spring Boot Auto Configuration automatically configures your Spring application based on the JAR dependencies you added in the project. For example, if MySQL database is on your class path, but you have not configured any database connection, then Spring Boot auto configures an in-memory database.

For this purpose, you need to add @EnableAutoConfiguration annotation or @SpringBootApplication annotation to your main class file. Then, your Spring Boot application will be automatically configured.

Observe the following code for a better understanding ?

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@EnableAutoConfiguration

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

Spring Boot Application

The entry point of the Spring Boot Application is the class contains @SpringBootApplication annotation. This class should have the main method to run the Spring Boot application. @SpringBootApplication annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.

If you added @SpringBootApplication annotation to the class, you do not need to add the @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration annotation. The @SpringBootApplication annotation includes all other annotations.

Observe the following code for a better understanding ?

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

Component Scan

Spring Boot application scans all the beans and package declarations when the application initializes. You need to add the @ComponentScan annotation for your class file to scan your components added in your project.

To get in-depth knowledge on Spring Boot, enroll for live free demo on Spring Boot Online Course

Observe the following code for a better understanding ?

import org.springframework.boot.SpringApplication;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

Spring Boot is a project that is built on top of the Spring Framework. It provides an easier and faster way to set up, configure, and run both simple and web-based applications.

It is a Spring module that provides the RAD (Rapid Application Development) feature to the Spring Framework used to create a stand-alone Spring-based application that you can just run because it needs minimal Spring configuration.

As summarized in the below figure, Spring Boot is the combination of Spring Framework and Embedded Servers.

No alt text provided for this image


How did Spring Boot originate?

The Spring framework has gained the interest of the Java developer’s community as a lightweight alternative to EJB and became one of the most popular application development frameworks.

It consists of a large number of modules providing a range of services including a component container, ASP support, security framework, data access framework, web application framework, support classes for testing components and etc.

Collecting all the required spring components together, setting up library dependencies in gradle/maven, and then configuring the required spring beans using xml, annotations, or java code requires quite an effort within the Spring framework.

This is where the Spring Boot Project comes into play!!!

Spring boot is already a launchable app. Depending on what Spring launcher class you extend, it might be a cli app or a web app. To add more stuff to the app you just have to add dependencies to the project. That’s it…

Spring Boot Architecture

In Spring Boot, there is no requirement for XML configuration (deployment descriptor). It uses?convention over configuration?software design paradigm which means that it decreases the effort of the developer.

The main goal of Spring Boot is to reduce development, unit test, and integration test time and leveraging the following features:

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty, or Undertow directly (no need to deploy WAR files)
  • Provide opinionated ‘starter’ POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks, and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

Spring Boot follows a layered architecture in which each layer communicates with the layer directly below or above (hierarchical structure) it. The below diagram demonstrates that every layer of a Spring Boot application is directly communicating with the layer just below or above it is due to the workflow.

No alt text provided for this image


Presentation Layer:?The presentation layer handles the HTTP requests, translates the JSON parameter to object, and authenticates the request, and transfer it to the business layer. In short, it consists of views.

Business Layer:?The business layer handles all the business logic. It consists of service classes and uses services provided by data access layers. It also performs authorization and validation.

Persistence Layer:?The persistence layer contains all the storage logic and translates business objects from and to database rows.

Database Layer:?In the database layer, CRUD (create, retrieve, update, delete) operations are performed.

Spring Boot uses all the modules of Spring-like Spring MVC, Spring Data, etc. The architecture of Spring Boot is the same as the architecture of Spring MVC, except for one thing: there is no need for DAO and DAOImpl classes in Spring boot. As a summary, in a simple spring boot flow:

  • Data access layer gets created and CRUD operations are performed.
  • The client makes the HTTP requests.
  • The request goes to the controller, and the controller maps that request and handles it. After that, it calls the service logic if required.
  • In the service layer, all the business logic performs. It performs the logic on the data that is mapped to JPA with model classes.
  • A response page is returned to the user if no error occurs.


No alt text provided for this image



Spring Boot Annotations

Spring Boot Annotations is a form of metadata that provides data about a program. Spring Boot Annotations are mostly placed in the following packages:

  • org.springframework.boot.autoconfigure
  • org.springframework.boot.autoconfigure.condition

Detailed information is given on the follow-up blog →?Basic Spring & Spring Boot Annotations

Spring Boot Components

The Spring Boot Project provides four key features to begin with and these can be considered as the reason behind Spring Boot’s magic.

  • Spring Boot Starters
  • Automatic configuration
  • Spring Boot CLI
  • Spring Boot Actuator

Detailed information is given on the follow-up blog →?Spring Boot Components

Spring Boot CLI

Spring Boot also provides a command-line tool that can be used to quickly write Spring applications. You can run Groovy scripts with Spring Boot CLI. Groovy code has almost zero boilerplate code compared with Java.

The Spring Boot documentation says:
“You don’t need to use the CLI to work with Spring Boot but it’s definitely the quickest way to get a Spring application off the ground.”

  • Spring Boot’s CLI gives you more free time from having to add starter dependencies and auto-configuration to let you focus only on writing your application-specific code.
  • What about dependent libraries? We don’t have Maven or Gradle here. CLI is smart; it detects classes being used in your application and it also knows which Starter dependencies should be used for these classes; accordingly, Spring Boot CLI adds dependencies to the classpath to make it work.
  • As Spring Boot CLI adds dependencies, a series of auto-configuration kicks in and adds the required bean method configuration so that your application is able to respond to HTTP requests.
  • CLI is an optional feature of Spring Boot; it just allows you to write a complete application with your application code only as, it doesn’t need to build a traditional project. CLI provides tremendous power and simplicity for Spring development.

Spring Boot Actuator

There are a lot of frameworks that provide tools for application development. But Spring Boot doesn’t only provide application development-specific features; it also provides post-production features. This allows you to monitor your Spring application during production using HTTP endpoints or with JMX.

Other parts of Spring Boot’s building blocks simplify Spring development; the Actuator instead offers the ability to inspect the internals of your application at runtime.

The Actuator provides data on auditing, metrics, and the health of your Spring Boot application using HTTP endpoints or with JMX. It helps you to manage your application when it’s in production.

The Actuator installed in a Spring Boot application provides the following benefits:

  • It provides details of all beans configured in the Spring application context
  • It also ensures all environment variables, system properties, configuration properties, and command-line arguments are available to your application
  • The Actuator gives various metrics pertaining to memory usage, garbage collection, web requests, and data source usage
  • It provides a trace of recent HTTP requests handled by your application
  • It also gives information about the current state of the threads in the Spring Boot application

Now that we have gone through the very basics of spring boot let’s take a look at its advantages and disadvantages.

Advantages

  • Simplified & version conflict-free dependency management through the starter POMs.
  • We can quickly set up and run standalone, web applications, and microservices.
  • Reduces the time spent on development and increases the overall efficiency of the development team.
  • Helps to avoid all the manual work of writing boilerplate code, annotations, and complex XML configurations. The beans are initialized, configured, and wired automatically.
  • Facilitates the creation and testing of Java-based applications by providing a default setup for unit and integration tests.
  • You can just assemble the jar artifact which comes with an embedded Tomcat, Jetty, or Undertow application server and you are ready to go.
  • The Spring Initializer provides a project generator to make you productive with a certain technology stack from the beginning.
  • The integration of Spring Boot with the Spring ecosystem which includes Spring Data, Spring Security, Spring ORM, and Spring JDBC is easy.
  • Provides many plugins that developers can use to work with embedded and in-memory databases smoothly and readily.
  • Allows for easy connection with database and queue services like Oracle, PostgreSQL, MySQL, MongoDB, Redis, Solr, ElasticSearch, Rabbit MQ, ActiveMQ, and many more.
  • Offers easy access to Command Line Interface which makes the development and testing of Spring Boot apps built with Java or Groovy agile.

Disadvantages

  • Spring boot may unnecessarily increase the deployment binary size with unused dependencies.
  • If you are a control freak, I doubt Spring Boot would fit your needs.
  • Spring Boot sticks well with microservices. The Spring Boot artifacts can be deployed directly into Docker containers. In large and monolithic applications it is not encouraged much.
  • If you have never worked with Spring before and want to learn about proxies, dependency injection, and AOP programming, it is not recommended to start with Spring Boot because it will make you miss many concepts that you would grasp if you had started using them independently.
  • You really have to understand a lot of the underlying Spring systems (and a bit of Spring history too), along with some advanced topics in order to modify and troubleshoot it.

********Thank you for reading*********

References


#SpringBoot



Michael Ferrara

?????Trusted IT Solutions Consultant | Technology | Science | Life | Author, Tech Topics | My goal is to give, teach & share what I can. Featured on InformationWorth | Upwork | ITAdvice.io | Salarship.Com

10 个月

Sirajeddine, thanks for sharing!

回复

要查看或添加评论,请登录

Sirajeddine Bouasker的更多文章

社区洞察

其他会员也浏览了