SPRING BOOT – PART1
info@[Link]
INDEX
Introduction
Advantages & Features
Spring Vs Spring Boot
First Spring Boot Application
Spring Boot configuration
[Link]
CHAPTER 1
INTRODUCTION
WHAT IS SPRING-BOOT?
Spring Boot is an open source Java-based framework used to create a micro
Service
It is a Spring module which provides RAD (Rapid Application Development)
feature to Spring framework
It is used to create Stand alone Spring based application that we can just run
because it needs very little spring configuration
It does not generate code & there is no requirement for XML configuration
Highly dependent on starter templates feature, which is very powerful & works
flawlessly
[Link]
ADVANTAGES OF SPRING BOOT
It is very easy to develop Spring Based applications with Java or
Groovy
It reduces lots of development time & increases productivity
It avoids writing lots of boilerplate Code, Annotations and XML
Configuration
It is very easy to integrate Spring Boot Application with its Spring
Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring
Security etc.
[Link]
ADVANTAGES OF SPRING BOOT....
It provides Embedded HTTP servers like Tomcat, Jetty etc.
to develop & test our web applications very easily
It provides lots of plugins to develop and test Spring Boot
applications very easily using Build Tools like Maven & Gradle
It provides lots of plugins to work with embedded and in-
memory Databases very easily
[Link]
SPRING VS SPRING BOOT
[Link]
SPRING VS SPRING BOOT....
[Link]
COMPARISON WITH SPRING
Maven Dependencies
Minimum dependencies required to create a web application
using Spring:-
[Link]
COMPARISON WITH SPRING....
Maven Dependencies:
Unlike Spring, Spring Boot requires only one dependency to get
a web application up and running:-
All other dependencies are added
[Link]
COMPARISON WITH SPRING....
How Spring Boot Bootstraps?
The entry point of a Spring Boot application is the class which is
annotated with @SpringBootApplication:
[Link]
WHAT IS STARTER TEMPLATE?
Spring Boot starters are templates that contain a collection
of all relevant transitive dependencies, that are needed to start
a particular functionality
In traditional setup, to create a Spring WebMVC app we have
to included all required dependencies, which leaves the chances of
version conflict which ultimately result in runtime exceptions
With String boot, to create a MVC application all we need to do,
is to import is spring-boot-starter-web dependency
[Link]
WHAT IS STARTER TEMPLATE?....
[Link]
WHAT IS STARTER TEMPLATE?....
Spring Boot provides a number of starter dependencies for different
Spring modules. Some of the most commonly used ones are:
spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
Note that all Spring Boot starters follow the same naming pattern
spring-boot-starter- *, where * indicates that it is a type of the
application
[Link]
CHAPTER 2
FIRST
SPRING BOOT
APPLICATION
PREREQUISITE OF SPRING BOOT
To create a Spring Boot application following are the prerequisites:
Java 1.8
Gradle 2.3+ or Maven 3.0+
Spring Framework [Link]-SNAPSHOT
Any IDE like Eclipse or NetBeans
[Link]
SPRING BOOT PROJECT
There are multiple ways to create Spring Boot project. We can use
any of the following approach to create application:-
Spring Maven Project
Spring Starter Project Wizard
Spring Initializer
Spring Boot CLI
[Link]
SPRING BOOT MAVEN PROJECT
Creating Spring Boot project by creating maven project. It
includes the following steps.
Create simple Maven
Project
[Link]
SPRING BOOT MAVEN PROJECT....
[Link]
SPRING BOOT MAVEN PROJECT
Cross-check newly created Maven
project
As shown correct JRE System Library
(if not correct)
Right click on project properties and go to Java Build Path
Select Libraries Tab
Click on Edit to correct jre selection
Apply and Close
[Link]
SPRING BOOT MAVEN PROJECT
Run maven build command to compile our newly created maven
project and test our application
We can create any sample class to make sure everything is in
place
[Link]
CREATING SPRINGBOOT APPLICATION
If we are simply creating Maven project, we need to configure it
in order to make it a Spring Boot project.
Step1 : First we need to add parent to our Maven project. It is
used to declare that our project is a child to this newly added
parent project
[Link]
CREATING SPRINGBOOT APPLICATION
Step2 : Add dependencies to [Link] file as per our need. Here,
we will include only starter dependency by adding spring-boot-
starter.
[Link]
CREATING SPRINGBOOT APPLICATION
Final [Link] after adding parent & minimum dependency
[Link]
CREATING SPRINGBOOT APPLICATION
Step3 : Create our first Sprint Boot application class by adding
@SpringBootApplication annotation
[Link]
CREATING SPRINGBOOT APPLICATION
Step4 : Now run above class as "Spring Boot App". This will
simply start our application, perform some activity like batching
etc and then shutdown, as shown below in output console:-
[Link]
CREATING SPRINGBOOT APPLICATION
Step5 : Now we will make our application web based. For this we
just need to add web dependency by adding spring-boot-
starter-web in our [Link] as shown below:-
[Link]
CREATING SPRINGBOOT APPLICATION
Step6 : Now again run our class as a "Spring Boot App". This will
start Tomcat server (inbuilt) at default port 8080 as shown
below in output :-
[Link]
SPRING-BOOT-STARTER VS
SPRING-BOOT-STARTER-WEB
spring-boot-starter -> provides basic development & run time
infrastructure for our application along with core spring
features
spring-boot-starter-web -> is used to add web capabilities such
as spring-mvc to our project
Note:- If we use spring-boot-starter-web,
no need to add spring-boot-starter
dependency explicitly
[Link]
SPRING STARTER PROJECT WIZARD
[Link]
SPRING STARTER PROJECT WIZARD....
New Spring Boot project will be created automatically
[Link]
SPRING INITIALIZR
One of the ways to Bootstrapping a Spring Boot application is by
using Spring Initializr
To start visit Spring Initializr web page [Link]
and fill details like Project, Spring Boot version, group, artifact
and required dependencies etc to run the application
After filling click Generate -> Project will be downloaded to our
local system in .zip format
Extract the project and import as Maven project in Eclipse
[Link]
SPRING INITIALIZR....
[Link]
SPRING INITIALIZR ....
Save project and extract it
Import as Maven project by using import option from the IDE
[Link]
SPRING INITIALIZR ....
After finishing, our Spring
Boot Project should look
like this
[Link]
CHAPTER 3
CONFIGURATION
APPLICATION PROPERTIES
Spring Boot provides various properties which can be specified
inside our project's [Link] file
These properties have default values and we can set that inside
the properties file
The [Link] file is located in the
src/main/resources directory
Properties are used to set values like: server-port number,
database connection configuration etc.
[Link]
APPLICATION PROPERTIES....
For example we can change port of Tomcat server in property
file as shown below:
[Link]
SPRING BOOT LOGGING
Spring Boot provide default logging feature and hence we see
the logs in the console.
Additional logging can be configured using SL4J
[Link]
SPRING BOOT LOGGING ....
Spring Boot print logs in console by default, but we can enable
logging for file also using simple property
[Link] can be specified in [Link] to specify
location and name of log file
We can control other parameters also like format, path etc
[Link]
Embedded server
Spring boot applications always include tomcat as embedded
server dependency. It means we can run the Spring boot
applications from the command prompt without needling complex
server infrastructure
We can exclude tomcat & include any other embedded server if
we want. Or we can make exclude server environment altogether
[Link]
Embedded server
[Link]
SPRING BOOT AUTOCONFIGURATION
Auto-configuration is enabled with following annotation
@EnableAutoConfiguration
Spring boot auto configuration scans the classpath, finds the
libraries in classpath & then attempt to guess the best
configuration for them & finally configure all such beans
Example, if we have tomcat related jar in our classpath, it will
automatically configure TomcatServletWebServerFactory class
Spring Boot automatically scans all the components included in
the project by using @ComponentScan annotation
[Link]
Spring Boot Application
Entry point of Spring Boot Application is the class contains
@SpringBootApplication annotation. This class should have the
main method to run the Spring Boot application
If we added @SpringBootApplication annotation, no need to add
following annotations :-
@EnableAutoConfiguration
@ComponentScan
@SpringBootConfiguration
The @SpringBootApplication annotation includes all other
annotations
[Link]
THANK YOU
HAPPY LEARNING!!
info@[Link]