SpringBoot-Training 1.0
SpringBoot-Training 1.0
Introduction
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.
Spring Boot is a project that is built on the top of the Spring Framework. It provides an
easier and faster way to set up, configure, and run both simple and web-based
applications.
A spring module provides the RAD (Rapid Application Development) feature to the
Spring Framework. It is used to create a stand-alone Spring-based application that you
can just run because it needs minimal spring configuration.
In short, Spring Boot is the combination of Spring Framework and Embedded Servers.
We can use Spring STS IDE/Intellij Idea or Spring Initializr to develop Spring Boot Java
applications.
Why should we use Spring Boot Framework?
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:
o Spring Data: It simplifies data access from the relational and NoSQL databases.
o Spring Batch: It provides powerful batch processing.
o Spring Security: It is a security framework that provides robust security to
applications.
o Spring Social: It supports integration with social networking like LinkedIn.
o Spring Integration: It is an implementation of Enterprise Integration Patterns. It
facilitates integration with other enterprise applications using lightweight
messaging and declarative adapters.
o It creates stand-alone Spring applications that can be started using Java -jar.
o It tests web applications easily with the help of different Embedded HTTP servers
such as Tomcat, Jetty, etc. We don't need to deploy WAR files.
o It provides opinionated 'starter' POMs to simplify our Maven configuration.
o It provides production-ready features such as metrics, health
checks, and externalized configuration.
o There is no requirement for XML configuration.
o It offers a CLI tool for developing and testing the Spring Boot application.
o It offers the number of plug-ins.
o It also minimizes writing multiple boilerplate codes (the code that has to be
included in many places with little or no alteration), XML configuration, and
annotations.
o It increases productivity and reduces development time.
Limitations of Spring Boot
Spring Boot can use dependencies that are not going to be used in the application. These
dependencies increase the size of the application.
To create a Spring Boot application, following are the prerequisites. In this tutorial, we will
use Spring Tool Suite (STS) IDE.
o Java 1.8
o Maven 3.0+
o Spring Framework 5.0.0.BUILD-SNAPSHOT
o An IDE (Spring Tool Suite) is recommended.
o Web Development
o Spring Application
o Application events and listeners
o Admin features
o Externalized Configuration
o Properties Files
o YAML Support
o Type-safe Configuration
o Logging
o Security
Spring Boot Modules
Spring Boot starters are templates that contain a collection of all the relevant
transitive dependencies that are needed to start a particular functionality. For example,
If you want to create a Spring WebMVC application then in a traditional setup, you
would have included all required dependencies yourself. It leaves the chances of version
conflict which ultimately result in more runtime exceptions.
With Spring boot, to create MVC application all you need to import is spring-boot-
starter-web dependency.
Above spring-boot-starter-web dependency, internally imports all given dependencies and
add to your project. Notice how some dependencies are direct, and some dependencies further
refer to other starter templates which transitively downloads more dependencies.
Also, notice that you do not need to provide version information into child dependencies.
All versions are resolved in relation to version of parent starter (in our example
it’s 2.0.4.RELEASE).
2. Spring boot auto configuration
You can exclude tomcat and include any other embedded server if you want. Or you can
make exclude server environment altogether. It’s all configuration based.
For example, below configuration exclude tomcat and include jetty as embedded
server.
It enables the scanning of config classes, files and load them into spring context. In
below example, execution start with main() method. It start loading all the config files,
configure them and bootstrap the application based on application
To execute the application, you can run the main() method from IDE such eclipse, or you can
build the jar file and execute from command prompt.
Spring Boot starters are templates that contain a collection of all the relevant transitive
dependencies that are needed to start a particular functionality. Each starter has a
special file, which contains the list of all the provided dependencies Spring provides.
These files can be found inside pom.xml files in respective starter module. e.g.
So next time when you want to give your project any specific functionality, I will suggest
to check for existing starter templates to see if you can use it directly. Ongoing
community additions are always on, so this list is already growing and you can
contribute to it as well.
I am listing down some very frequently use spring starters and what dependencies they
bring along, for information only.
Spring Boot Annotations
Spring Boot Annotations is a form of metadata that provides data about a program. In
other words, annotations are used to provide supplemental information about a
program. It is not a part of the application that we develop. It does not have a direct
effect on the operation of the code they annotate. It does not change the action of the
compiled program.
In this section, we are going to discuss some important Spring Boot Annotation that
we will use later in this tutorial.
@Required: It applies to the bean setter method. It indicates that the annotated bean
must be populated at configuration time with the required property, else it throws an
exception BeanInitilizationException.
@Service: It is also used at class level. It tells the Spring that class contains the business
logic.
@RequestMapping: It is used to map the web requests. It has many optional elements
like consumes, header, method, name, params, path, produces, and value. We use it
with the class as well as the method.
@GetMapping: It maps the HTTP GET requests on the specific handler method. It is used
to create a web service endpoint that fetches It is used instead of
using: @RequestMapping(method = RequestMethod.GET)
@PostMapping: It maps the HTTP POST requests on the specific handler method. It is
used to create a web service endpoint that creates It is used instead of
using: @RequestMapping(method = RequestMethod.POST)
@PutMapping: It maps the HTTP PUT requests on the specific handler method. It is used
to create a web service endpoint that creates or updates It is used instead of
using: @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping: It maps the HTTP DELETE requests on the specific handler method. It
is used to create a web service endpoint that deletes a resource. It is used instead of
using: @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping: It maps the HTTP PATCH requests on the specific handler method. It is
used instead of using: @RequestMapping(method = RequestMethod.PATCH)
@ResponseBody: It binds the method return value to the response body. It tells the
Spring Boot Framework to serialize a return an object into JSON and XML format.
@PathVariable: It is used to extract the values from the URI. It is most suitable for the
RESTful web service, where the URL contains a path variable. We can define multiple
@PathVariable in a method.
@RequestParam: It is used to extract the query parameters form the URL. It is also
known as a query parameter. It is most suitable for web applications. It can specify
default values if the query parameter is not present in the URL.
@RequestHeader: It is used to get the details about the HTTP request headers. We use
this annotation as a method parameter. The optional elements of the annotation
are name, required, value, defaultValue. For each detail in the header, we should
specify separate annotations. We can use it multiple time in a method
Spring provides excellent support for both task scheduling and asynchronous method
execution based on cron expression using @Scheduled annotation. The @Scheduled
annotation can be added to a method along with trigger metadata.
Let’s say you want to run job at every 10 seconds interval. You can achieve this job
scheduling in below steps:
Now you can add @Scheduled annotations on methods which you want to schedule. Only
condition is that methods should be without arguments.
For every annotated method without arguments, the appropriate executor thread pool will
be created. This thread pool will manage the scheduled invocation of the annotated method.
Configure a task to run after a fixed delay. In given example, the duration between the
end of last execution and the start of next execution is fixed. The task always waits until
the previous one is finished.
@Scheduled(fixedDelay = 10000)
}
2.3. Spring boot cron job example
@Scheduled annotation is very flexible and may take cron expression as well.
@Scheduled(cron = "0 10 10 10 * ?")