This document provides an overview of the Spring Framework including:
- What Spring is and its benefits such as infrastructure focused development and aspects.
- Key Spring concepts like dependency injection, the IOC container, and aspect oriented programming.
- The Spring architecture including Spring beans and various Spring projects.
- How to get started with Spring Boot and its features for creating stand-alone applications.
- Examples of using Spring for web applications, transactions, and testing.
2. Foreword
This a basic introduction to Spring Framework and niche technologies
To understand technical aspects it is recommended to have prior knowledge
of
Java
Maven
SDLC
Non-Functional Requirements
Git
Make this discussion yours : peer to peer learning
5. Overview
What is Spring?
1
Whats in it for me?
2
Basic Concepts
•Dependency Injection
•IOC Container
•Aspect Oriented
Programming
3
Spring Architecture
•Spring Beans
4
Other Spring
Projects
5
6. What is Spring
Spring is
• An Application Development
Framework
• Inversion of Control Container
• Open Source
• Alternate to Enterprise Java
Beans
Spring Allows us
• To create an HTTP endpoint,
without dealing with servlet
API
• To Focus on functionality of a
method, without having to
deal with transactions API
7. What’s in it for me?
Infrastructure
Focused
Development
Aspects
Open Source
Existing Technologies
Automation Testing
Fast
Modular
Huge Community
Independent of
Container
Easy to understand
Transactional
Reliable
Configurable
Extensible
Secure
Customizable
Cost Effective
Lightweight
8. Dependency Injection
An Implementation of
Factory Design
Pattern
What to instantiate?
Builder Design
Pattern
How to instantiate?
Service Locator
Design Pattern
Where to find it?
9. Dependency Injection
It is at the heart of Spring
It helps keeping our classes as independent as possible
Increases reuse by applying low coupling
Easy testing
Readable
Passes the dependency (a service) to a dependent object (a client) rather
than allowing a client to build or find the service
10. IOC Container
The container gets its
instructions on what
objects to instantiate,
configure and assemble by
reading configuration
metadata provided.
The configuration metadata
can be represented either
by
XML
Java annotations
Java code
11. Spring Beans
singleton
A single instance
per Spring IoC
container
(default).
prototype
New Instance
whenever
requested
request
Once every HTTP
request
session
Once per HTTP
Session.
global-
session
Once per global
HTTP Session
Spring beans are
The objects that form the backbone of an application
Managed by Spring IOC Container
Scope
12. Dependency Injection XML Config
Foo f = new Foo("Cleopatra");
Bar b = new Bar("Arthur",26);
b.setFoo(f);
15. Aspect Oriented Programming
@Before – Run before the
method execution
@After – Run after the
method returned a result
@AfterReturning – Run after
the method returned a
result, intercept the
returned result as well.
@Around – Run around the
method execution, combine
all three advices above.
@AfterThrowing – Run after
the method throws an
exception
Pointcut (AspectJ Pointcut
Expression Language)
Join Point
Advice
20. Summary
What is Spring?
1
Whats in it for me?
2
Basic Concepts
•Dependency Injection
•IOC Container
•Aspect Oriented
Programming
3
Spring Architecture
•Spring Beans
4
Other Spring
Projects
5
22. Spring Boot
Spring Boot is a brand new framework from the team at Pivotal, designed to
simplify the bootstrapping and development of a new Spring application. The
framework takes an opinionated approach to configuration, freeing
developers from the need to define boilerplate configuration.
Features
Create stand-alone Spring applications
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated 'starter' POMs to simplify your Maven configuration
Automatically configure Spring whenever possible
Provide production-ready features such as metrics, health checks and externalized
configuration
Absolutely no code generation and no requirement for XML configuration
29. Spring Boot Maven Plugin
Uber Jar
• It collects all the jars on the classpath and builds a single, runnable
"über-jar", which makes it more convenient to execute and transport
your service.
Runnable
Class
• It searches for the public static void main() method to flag as a
runnable class.
Dependency
Resolver
• It provides a built-in dependency resolver that sets the version
number to match Spring Boot dependencies. You can override any
version you wish, but it will default to Boot’s chosen set of versions.
31. @Autowired
Helps finding a desired implementation spring bean
Inject its instance
Get rid of new keyword
Implementations can be injected at runtime instead of compile time.
32. Hello World Controller
• Makes the class a
Spring Aware MVC
Controller
@Controller
• Map a HTTP request
method to Bean
method
@RequestMapping
• Sets the returned value
from method as the
HTTP response body
@ResponseBody
38. The DispatcherServlet
The Spring Web model-view-controller (MVC) framework
is designed around a DispatcherServlet that handles all
the HTTP requests and responses.
39. Spring boot Devtools
Enable hot swapping
Switches template engines to disable caching
Enables LiveReload to refresh browser automatically
Other reasonable defaults based on development instead of production
42. Transaction
A transaction is a set of instructions which are treated as a single unit of work
They should complete entirely or nothing
Atomicity
• Single unit
of operation
• It should
either be
complete or
not at all
• No middle
state
Consistency
• Consistency
of
referential
integrity
Isolation
• Each
transaction
must be
isolated
from others
Durability
• Changes are
stored
permanently
• Must not be
erased due
to system
failure
45. Important Definitions
Aspects
Cross cutting concerns of an application e.g. Logging, Transaction Management
Join Point
A point where an aspect is expected
Advice
Action taken at particular Join Point
Pointcut
Determine weather an advice is to be executed at a Join Point
47. Spring Security
Spring Security is a framework that focuses on providing both authentication
and authorization to Java Applications
Features
Comprehensive and extensible support for both Authentication and Authorization
Protection against attacks like session fixation, clickjacking, CSRF etc
Servlet API integration
Optional integration with Spring Web MVC
50. Spring Test
Spring Test Framework supports
Unit testing with mock objects
IOC container to create dependencies for integration testing
Transaction management for integration testing
Third party frameworks like
Junit
TestNG
Mockito
52. Further reading
S.No Title Resource
1 War Packaging https://docs.spring.io/spring-
boot/docs/current/reference/htmlsingle/#buil
d-tool-plugins-maven-packaging
2 Getting Started https://docs.spring.io/spring/docs/current/spr
ing-framework-reference/html/overview-
getting-started-with-spring.html
3 Testing with non-
default options
https://spring.io/guides/gs/testing-web/
4 Spring Security https://spring.io/guides/tutorials/spring-
security-and-angular-js/
6 Annotations http://www.tutorialspoint.com/spring/spring_
autowired_annotation.htm
7 DevTools http://docs.spring.io/spring-
boot/docs/current/reference/html/using-
boot-devtools.html
53. About Me
Mohit Kanwar
Email :
m.m.kanwar@gmail.com
Social
https://www.linkedin.com/in/mohit-kanwar-7668a211/
https://github.com/mohitkanwar
https://www.slideshare.net/mohitkanwar1
Blog
http://mohitkanwar.com
Editor's Notes
Started as an alternate to EJBs
Reducing complexity
Improving performance
POJO Based
Independence from Containers
XML Based Project Configurations
Configurations defined in XML
Annotation based Project Configurations
Independence from XML
Spring Boot
Automatic Configurations
Spring handles the infrastructure so you can focus on your application.
At this point you like to add Caching. What changes are required?