0% found this document useful (0 votes)
0 views41 pages

Introduction+to+Spring+Boot

Uploaded by

Jc Tuzon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views41 pages

Introduction+to+Spring+Boot

Uploaded by

Jc Tuzon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to Spring Boot

Spring in a Nutshell

▸ Very popular framework for building Java applications


▸ • Provides a large number of helper classes and annotations
3

The Problem

▸ Building the traditional spring application is really HARD!!!


Q: Which JAR dependencies do I need?
Q: How do I set up configuration (xml or
Java)?

And that's Q: How do I install the server? (Tomcat,


JUST the basics JBoss etc…)
for getting started
4

What is Spring Boot?

▸ Java Spring Boot (Spring Boot) is a tool that makes developing web application
and microservices with Spring Framework faster and easier through three core
capabilities:
 Autoconfiguration

 An opinionated approach to configuration

 The ability to create standalone applications


5

Spring Boot Solution

▸ Make it easier to get started with Spring development


▸ Minimize the amount of manual configuration
▸ Perform auto-configuration based on props files and JAR classpath
▸ Help to resolve dependency conflicts (Maven or Gradle)
▸ Provide an embedded HTTP server so you can get started quickly
▹ Tomcat, Jetty, Undertow, …
6

Spring Boot and Spring

▸ Spring Boot uses Spring behind the scenes


▸ Spring Boot simply makes it easier to use Spring
7

Spring Boot Initializr

▸ Quickly create a starter Spring Boot project


▸ Select your dependencies
▸ Creates a Maven/Gradle project
▸ Import the project into your IDE
▸ Eclipse, IntelliJ, NetBeans etc …
8

Spring Boot Features


9

Spring Boot Initializr

http://start.spring.io
10

Spring Boot Embedded Server

▸ Provide an embedded HTTP server so you can get started quickly


▸ Tomcat, Jetty, Undertow, …
▸ No need to install a server separately
Self-contained unit
Nothing else to
install

mycode

JAR File includes your


application code
AND
Tomcat Include the server
11

Running Spring Boot Apps

▸ Spring Boot apps can be run standalone (includes embedded server)


▸ Run the Spring Boot app from the IDE or command-line

 Java –jar Myapp.jar

mycode

Tomcat
Name of our JAR file
12

Spring Boot FAQ #1

Q: Does Spring Boot replace Spring MVC, Spring REST etc …?

▸ No. Instead, Spring Boot actually uses those technologies


Spring Boot
Spring Spring
MVC REST Spring…

Spring Spring
Spring…
Core AOP
13

Spring Boot FAQ #2

Q: Does Spring Boot run code faster than regular Spring code?

▸ No.
▸ • Behind the scenes, Spring Boot uses same code of Spring Framework
▸ Remember, Spring Boot is about making it easier to get started
▸ Minimizing configuration etc …
14

Spring Boot FAQ #3

Q: Do I need a special IDE for Spring Boot?

▸ No.
▸ You can use any IDE for Spring Boot apps … even use plain text editor
▸ The Spring team provides free Spring Tool Suite (STS) [IDE plugins]
▸ Some IDEs provide fancy Spring tooling support
▸ Not a requirement. Feel free to use the IDE that work s best for you
15

Spring Boot Architecture


16

Spring Boot Architecture

▸ 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.
17

Spring Boot Flow Architecture


18

Spring Boot Initializr Demo


19

Spring Initializr

▸ Quickly create a starter Spring


project
▸ Select your dependencies
▸ Creates a Maven/Gradle project
▸ Import the project into your IDE
▸ Eclipse, IntelliJ, NetBeans etc …
20

Quick Word on Maven

▸ When building your Java project, you may need additional JAR files
• For example: Spring, Hibernate, Commons Logging, JSON etc…

▸ One approach is to download the JAR files from each project web site
▸ Manually add the JAR files to your build path / classpath
21

Development Process

1. Configure our project at Spring Initializr website

2. Download the zip file

3. Unzip the file

4. Import the project into our IDE


22

Spring Boot Components


23

Spring Boot Starters

▸ Spring Boot provides a number of starters that allow us to add jars in the
classpath. Spring Boot built-in starters make development easier and rapid.
Spring Boot Starters are the dependency descriptors.
▸ We will explore this statement in detail with one example. For instance, we
would like to develop a Spring WebApplication with Tomcat WebServer.
▸ Then we need to add the following minimal jar dependencies in your Maven’s
pom.xml file or Gradle’s build.gradle file

▹ Spring core Jar file(spring-core-xx.jar)


▹ Spring Web Jar file(spring-web-xx.jar)
▹ Spring Web MVC Jar file(spring-webmvc-xx.jar)
▹ Servlet Jar file(servlet-xx.jar)
24

Major Advantages of Spring Boot Starter

▸ Spring Boot Starter reduces defining many dependencies simplify project


build dependencies.
▸ Spring Boot Starter simplifies project build dependencies.
25

Spring Boot Starter Actuator

▸ Spring Boot Actuator is a sub-project of the Spring Boot Framework. It


includes a number of additional features that help us to monitor and manage
the Spring Boot application. It contains the actuator endpoints (the place
where the resources live).
▸ We can use HTTP and JMX endpoints to manage and monitor the Spring Boot
application. If we want to get production-ready features in an application, we
should use the Spring Boot actuator.
26

Spring Boot Actuator Feature

▸ There are three main features of Spring Boot Actuator:


▹ Endpoints

▹ Metrics

▹ Audit
27

Spring Boot Actuator Feature

▸ Endpoint: The actuator endpoints allows us to monitor and interact with the
application. Spring Boot provides a number of built-in endpoints. We can also
create our own endpoint.

▸ Metrics: Spring Boot Actuator provides dimensional metrics by integrating


with the micrometer. The micrometer is integrated into Spring Boot. It is the
instrumentation library powering the delivery of application metrics from
Spring.

▸ Audit: Spring Boot provides a flexible audit framework that publishes events
to an AuditEventRepository. It automatically publishes the authentication
events if spring-security is in execution.
28

Spring Boot Auto-configuration

▸ Spring Boot auto-configuration automatically configures the Spring


application based on the jar dependencies that we have added.
▸ For example, if the H2 database Jar is present in the classpath and we have
not configured any beans related to the database manually, the Spring Boot's
auto-configuration feature automatically configures it in the project.
29

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.
30

Spring Boot Annotations

▸ Spring annotations present in the org.springframework.boot.autoconfigure


and org.springframework.boot.autoconfigure.condition packages are
commonly known as Spring Boot annotations. Some of the annotations that
are available in this category are:
▹ @SpringBootApplication
▹ @SpringBootConfiguration
▹ @EnableAutoConfiguration
▹ @ComponentScan
▸ Auto-Configuration Conditions
▹ @ConditionalOnClass, and @ConditionalOnMissingClass
▹ @ConditionalOnBean, and @ConditionalOnMissingBean
▹ @ConditionalOnProperty
▹ @ConditionalOnResource
▹ @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
▹ @ConditionalExpression
▹ @Conditional
31

1. @SpringBootApplication Annotation
▸ This annotation is used to mark the main class of a Spring Boot application. It
encapsulates @SpringBootConfiguration, @EnableAutoConfiguration,and

▸ @ComponentScan annotations with their default attributes.


32

2. @SpringBootConfiguration Annotation
▸ It is a class-level annotation that is part of the Spring Boot framework. It implies that a class
provides Spring Boot application configuration.
▸ It can be used as an alternative to Spring’s standard @Configuration annotation so that
configuration can be found automatically.
▸ Most Spring Boot Applications use @SpringBootConfiguration via SpringBootApplication. If
an application uses @SpringBootApplication, it is already using @SpringBootConfiguration.
33

3. @EnableAutoConfiguration Annotation
▸ @EnableAutoConfiguration: It auto-configures the bean that is present in the classpath and
configures it to run the methods. The use of this annotation is reduced in Spring Boot 1.2.0
release because developers provided an alternative of the annotation, i.e.
@SpringBootApplication.
34

4. @ComponentScan Annotation
▸ @ComponentScan tells Spring in which packages you have annotated classes that should
be managed by Spring
35
5. @ConditionalOnClass Annotation and
@ConditionalOnMissingClass Annotation
▸ @ConditionalOnClass Annotation used to mark auto-configuration bean if the class in the
annotation’s argument is present/absent.
36
6. @ConditionalOnBean Annotation and
@ConditionalOnMissingBean Annotation
▸ These annotations are used to let a bean be included based on the presence or absence of
specific beans.
37

7. @ConditionalOnProperty Annotation
▸ These annotations are used to let configuration be included based on the presence and
value of a Spring Environment property.
38

8. @ConditionalOnResource Annotation
▸ These annotations are used to let configuration be included only when a specific resource is
present in the classpath.
39

9. @ConditionalOnExpression Annotation
▸ These annotations are used to let configuration be included based on the result of a SpEL
(Spring Expression Language)
▸ SpEL (Spring Expression Language): It is a powerful expression language that supports
querying and manipulating an object graph at runtime.
40

10. @ConditionalOnCloudPlatform Annotation


▸ These annotations are used to let configuration be included when the specified cloud
platform is active.
41

THANKS!

You might also like