Spring Boot
Spring Boot
Spring Boot
Spring Boot Tutorial provides basic and advanced concepts of Spring Framework.
Our Spring Boot Tutorial is designed for beginners and professionals both.
Spring Boot is a Spring module that provides the RAD (Rapid Application
Development) feature to the Spring framework.
Our Spring Boot Tutorial includes all topics of Spring Boot such, as features, project,
maven project, starter project wizard, Spring Initializr, CLI, applications, annotations,
dependency management, properties, starters, Actuator, JPA, JDBC, etc.
We can use Spring STS IDE or Spring Initializr to develop Spring Boot Java
applications.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Along with the Spring Boot Framework, many other Spring sister projects help to
build applications addressing modern business needs. There are the following Spring
sister projects are as follows:
ADVERTISEMENT
o Java 1.8
o Maven 3.0+
o Spring Framework 5.0.0.BUILD-SNAPSHOT
o An IDE (Spring Tool Suite) is recommended.
Web Development
ADVERTISEMENT
ADVERTISEMENT
SpringApplication
ADVERTISEMENT
ADVERTISEMENT
Spring Boot uses events to handle the variety of tasks. It allows us to create factories
file that is used to add listeners. We can refer it to using the ApplicationListener
key.
ADVERTISEMENT
ADVERTISEMENT
Admin Support
Spring Boot provides the facility to enable admin-related features for the application.
It is used to access and manage applications remotely. We can enable it in the Spring
Boot application by using spring.application.admin.enabled property.
Externalized Configuration
Spring Boot allows us to externalize our configuration so that we can work with the
same application in different environments. The application uses YAML files to
externalize configuration.
Properties Files
Spring Boot provides a rich set of Application Properties. So, we can use that in the
properties file of our project. The properties file is used to set properties like server-
port =8082 and many others. It helps to organize application properties.
ADVERTISEMENT
ADVERTISEMENT
YAML Support
Type-safe Configuration
Logging
Spring Boot uses Common logging for all internal logging. Logging dependencies
are managed by default. We should not change logging dependencies if no
customization is needed.
Security
ADVERTISEMENT
ADVERTISEMENT
Spring Boot applications are spring bases web applications. So, it is secure by default
with basic authentication on all HTTP endpoints. A rich set of Endpoints is available
to develop a secure Spring Boot application.
ADVERTISEMENT
Prerequisite
Before learning Spring Boot, you must have the basic knowledge of Spring
Framework.
ADVERTISEMENT
Audience
Our Spring Boot Tutorial is designed to help beginners and professionals.
Problem
We assure you that you will not find any problem with the Spring Boot Tutorial. But if
there is any mistake, please post the problem in the contact form.
ADVERTISEMENT
ADVERTISEMENT
o What's New
Infrastructure Upgrade
Spring Framework 5
o What's Changed
Configuration Properties
Gradle Plugin
Actuators endpoints
o What's Evolving
Security
Metrics
The pivotal team has upgraded the infrastructure in which the following tools are
involved:
o Reactive Spring
i. Servlet stack
a. Servlet Container
b. Servlet API
c. Spring MVC
ii. Reactive Stack
The latest version of Spring Boot is 2.2.1. This release of Spring Boot includes 110
fixes, dependency upgrades, and improvements.
New Features
o Support constructor binding for property nested inside a JavaBean
o Add config property for CodecConfigurer.maxInMemorySize in WebFlux
o Make test slices' type exclude filters public
o Support amqps:// URIs in spring.rabbitmq.addresses
Dependency upgrades
Some dependencies have been upgraded in Spring Boot v2.2.1 are as follows:
o Mongodb 3.11.2
o Spring Security 5.2.1.RELEASE
o Slf4j 1.7.29
o Spring Hateoas 1.0.1.RELEASE
o Hibernate Validator 6.0.18.Final
o Hibernate 5.4.8.Final
o Jetty 9.4.22.v20191022
o Spring Framework 5.2.1
o Spring AMQP 2.2.1
o H2 1.4.200
o Spring Security 5.2
o Spring Batch 4.2
Some important and widely used third-party dependencies are upgraded in this
release are as follows:
o Micrometer 1.3.1
o Flyway 6.0.7
o Elasticsearch 6.8.4
o JUnit 5.5
o Jackson 2.10
Performance Improvements
In Spring Boot 2.2.1 the following performance has been improved:
Lazy Initialization
ADVERTISEMENT
ADVERTISEMENT
In Spring Boot 2.2.1, we can enable global lazy initialization by using the
property spring.main.lazy-initialization property. It reduces the application startup
time.
Java 13 Support
Spring Boot 2.2.1 now supports the latest version of Java that is Java 13.
Immutable Binding
RSocket Support
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
The primary comparison between Spring and Spring Boot are discussed below:
It aims to simplify Java EE It aims to shorten the code length and provide the
development that makes easiest way to develop Web Applications.
developers more productive.
The primary feature of the Spring The primary feature of Spring Boot
Framework is dependency is Autoconfiguration. It automatically configures
injection. the classes based on the requirement.
To test the Spring project, we Spring Boot offers embedded server such
need to set up the sever as Jetty and Tomcat, etc.
explicitly.
It does not provide support for It offers several plugins for working with an
an in-memory database. embedded and in-memory database such as H2.
Developers manually define Spring Boot comes with the concept of starter in
dependencies for the Spring pom.xml file that internally takes care of
project in pom.xml. downloading the dependencies JARs based on
Spring Boot Requirement.
Spring MVC: Spring MVC is a Web MVC Framework for building web applications. It
contains a lot of configuration files for various capabilities. It is an HTTP oriented web
application development framework.
Spring Boot and Spring MVC exist for different purposes. The primary comparison
between Spring Boot and Spring MVC are discussed below:
Spring Boot follows a layered architecture in which each layer communicates with the
layer directly below or above (hierarchical structure) it.
Before understanding the Spring Boot Architecture, we must know the different
layers and classes present in it. There are four layers in Spring Boot are as follows:
ADVERTISEMENT
ADVERTISEMENT
o Presentation Layer
o Business Layer
o Persistence Layer
o Database Layer
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 i.e., frontend part.
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 Initializr
Spring Initializr is a web-based tool provided by the Pivotal Web Service. With the
help of Spring Initializr, we can easily generate the structure of the Spring Boot
Project. It offers extensible API for creating JVM-based projects.
It also provides various options for the project that are expressed in a metadata
model. The metadata model allows us to configure the list of dependencies
supported by JVM and platform versions, etc. It serves its metadata in a well-known
that provides necessary assistance to third-party clients.
ADVERTISEMENT
ADVERTISEMENT
Supported Interface
o It supports IDE STS, IntelliJ IDEA Ultimate, NetBeans, Eclipse. You can
download the plugin from https://github.com/AlexFalappa/nb-springboot. If
you are using VSCode, download the plugin
from https://github.com/microsoft/vscode-spring-initializr.
o Use Custom Web UI http://start.spring.io or https://start-scs.cfapps.io.
o It also supports the command-line with the Spring Boot
CLI or cURL or HTTPie.
There is a Generate button. When we click on the button, it starts packing the
project and downloads the Jar or War file, which you have selected.
Step 3: Spring Tool Suite 3 Launcher dialog box appears on the screen. Click on
the Launch button. You can change the Workspace if you want.
ADVERTISEMENT
Step 2: Provide the Group and Artifact name. We have provided Group
name com.javatpoint and Artifact spring-boot-example.
File -> Import -> Existing Maven Project -> Next -> Browse -> Select the project ->
Finish
It takes some time to import the project. When the project imports successfully, we
can see the project directory in the Package Explorer. The following image shows
the project directory:
SpringBootExampleApplication.java
ADVERTISEMENT
ADVERTISEMENT
1. package com.javatpoint.springbootexample;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
4. @SpringBootApplication
5. public class SpringBootExampleApplication
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(SpringBootExampleApplication.class, args);
10. }
11. }
pom.xml
Download Project
Step 2: Click on the File menu -> New -> Maven Project
It shows the New Maven Project wizard. Click on the Next button.
Step 4: Provide the Group Id and Artifact Id. We have provided Group
Id com.javatpoint and Artifact Id spring-boot-example-sts. Now click on
the Finish button.
When we click on the Finish button, it creates the project directory, as shown in the
following image.
Step 5: Open the App.java file. We found the following code that is by default.
App.java
1. package com.javatpoint;
2. public class App
3. {
4. public static void main( String[] args )
5. {
6. System.out.println( "Hello World!" );
7. }
8. }
The Maven project has a pom.xml file which contains the following default
configuration.
ADVERTISEMENT
ADVERTISEMENT
pom.xml
1. <java.version>1.8</java.version>
Step 7: In order to make a Spring Boot Project, we need to configure it. So, we are
adding spring boot starter parent dependency in pom.xml file. Parent is used to
declare that our project is a child to this parent project.
1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-parent</artifactId>
4. <version>2.2.1.RELEASE</version>
5. <type>pom</type>
6. </dependency>
1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-web</artifactId>
4. <version>2.2.1.RELEASE</version>
5. </dependency>
Note: When we add the dependencies in the pom file, it downloads the related jar
file. We can see the downloaded jar files in the Maven Dependencies folder of the
project directory.
ADVERTISEMENT
After adding all the dependencies, the pom.xml file looks like the following:
pom.xml
ADVERTISEMENT
ADVERTISEMENT
Right-click on the package name -> New -> Class -> provide the class name ->
Finish
Step 10: After creating the class file, call the static method run() of the
SpringApplication class. In the following code, we are calling the run() method and
passing the class name as an argument.
1. SpringApplication.run(SpringBootExampleSts.class, args);
ADVERTISEMENT
ADVERTISEMENT
@SpringBootApplication
ADVERTISEMENT
ADVERTISEMENT
o @EnableAutoConfiguration: It enables the Spring Boot auto-configuration
mechanism.
o @ComponentScan: It scans the package where the application is located.
o @Configuration: It allows us to register extra beans in the context or import
additional configuration classes.
ADVERTISEMENT
ADVERTISEMENT
SpringBootApplicationSts.java
1. package com.javatpoint;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
4. @SpringBootApplication
5. public class SpringBootExampleSts
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(SpringBootExampleSts.class, args);
10. }
11. }