SPRING BOOT – PART1
[email protected]
INDEX
Introduction
Advantages & Features
Spring Vs Spring Boot
First Spring Boot Application
Spring Boot configuration
www.4kitsolutions.com
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
www.4kitsolutions.com
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.
www.4kitsolutions.com
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
www.4kitsolutions.com
SPRING VS SPRING BOOT
www.4kitsolutions.com
SPRING VS SPRING BOOT....
www.4kitsolutions.com
COMPARISON WITH SPRING
Maven Dependencies
Minimum dependencies required to create a web application
using Spring:-
www.4kitsolutions.com
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
www.4kitsolutions.com
COMPARISON WITH SPRING....
How Spring Boot Bootstraps?
The entry point of a Spring Boot application is the class which is
annotated with @SpringBootApplication:
www.4kitsolutions.com
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
www.4kitsolutions.com
WHAT IS STARTER TEMPLATE?....
www.4kitsolutions.com
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
www.4kitsolutions.com
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 5.0.0.BUILD-SNAPSHOT
Any IDE like Eclipse or NetBeans
www.4kitsolutions.com
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
www.4kitsolutions.com
SPRING BOOT MAVEN PROJECT
Creating Spring Boot project by creating maven project. It
includes the following steps.
Create simple Maven
Project
www.4kitsolutions.com
SPRING BOOT MAVEN PROJECT....
www.4kitsolutions.com
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
www.4kitsolutions.com
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
www.4kitsolutions.com
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
www.4kitsolutions.com
CREATING SPRINGBOOT APPLICATION
Step2 : Add dependencies to pom.xml file as per our need. Here,
we will include only starter dependency by adding spring-boot-
starter.
www.4kitsolutions.com
CREATING SPRINGBOOT APPLICATION
Final pom.xml after adding parent & minimum dependency
www.4kitsolutions.com
CREATING SPRINGBOOT APPLICATION
Step3 : Create our first Sprint Boot application class by adding
@SpringBootApplication annotation
www.4kitsolutions.com
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:-
www.4kitsolutions.com
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 pom.xml as shown below:-
www.4kitsolutions.com
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 :-
www.4kitsolutions.com
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
www.4kitsolutions.com
SPRING STARTER PROJECT WIZARD
www.4kitsolutions.com
SPRING STARTER PROJECT WIZARD....
New Spring Boot project will be created automatically
www.4kitsolutions.com
SPRING INITIALIZR
One of the ways to Bootstrapping a Spring Boot application is by
using Spring Initializr
To start visit Spring Initializr web page www.start.spring.io
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
www.4kitsolutions.com
SPRING INITIALIZR....
www.4kitsolutions.com
SPRING INITIALIZR ....
Save project and extract it
Import as Maven project by using import option from the IDE
www.4kitsolutions.com
SPRING INITIALIZR ....
After finishing, our Spring
Boot Project should look
like this
www.4kitsolutions.com
CHAPTER 3
CONFIGURATION
APPLICATION PROPERTIES
Spring Boot provides various properties which can be specified
inside our project's application.properties file
These properties have default values and we can set that inside
the properties file
The application.properties file is located in the
src/main/resources directory
Properties are used to set values like: server-port number,
database connection configuration etc.
www.4kitsolutions.com
APPLICATION PROPERTIES....
For example we can change port of Tomcat server in property
file as shown below:
www.4kitsolutions.com
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
www.4kitsolutions.com
SPRING BOOT LOGGING ....
Spring Boot print logs in console by default, but we can enable
logging for file also using simple property
logging.file can be specified in applicaton.properties to specify
location and name of log file
We can control other parameters also like format, path etc
www.4kitsolutions.com
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
www.4kitsolutions.com
Embedded server
www.4kitsolutions.com
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
www.4kitsolutions.com
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
www.4kitsolutions.com
THANK YOU
HAPPY LEARNING!!
[email protected]