Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Simple, Portable, Fast and Flexible
By Mohit Kanwar
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
Overview
Introduction
to Spring
Working
with Spring
Spring Boot
Spring
Security
Spring Test
Introduction to
Spring
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
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
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
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?
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
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
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
Dependency Injection XML Config
Foo f = new Foo("Cleopatra");
Bar b = new Bar("Arthur",26);
b.setFoo(f);
Aspect Oriented Programming
Accounts
Business
Logic
Persistence
Security
Logging
Users
Business
Logic
Persistence
Security
Logging
Bill Payment
Business
Logic
Persistence
Security
Logging
Location Finder
Business
Logic
Persistence
Security
Logging
Non AOP
Aspect Oriented Programming
Accounts
Business
Logic
Users
Business
Logic
Bill Payment
Business
Logic
Location Finder
Business
Logic
AOP
PersistenceSecurityLogging Caching
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
Spring
Framework
Architecture
Typical Full
Fledged Web
Application
Custom Domain Logic
Aspects
Web Views
APIs
Persistence
Models
Notifications
Remote
Automation Testing
Open Source
https://github.com/spring-projects
Various Spring Projects
Spring-Boot Spring-Batch Spring-
Security
Spring-
Framework
Spring-Cloud Spring-
Integration
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
Spring Boot
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
Working with
Spring
Overview
Setting up
workspace
1
Hello World
in Spring
2
Spring Web-
MVC
3
Spring
Transactions
4
Aspects
5
My First Spring
Based Application
 Creating Environment
 Choosing dependencies
 Setting it all up!
Spring Boot it Up
 Spring Initializr
 https://start.spring.io/
Demo
Hello World!
My first Rest API using Spring
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.
Automation Test
@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.
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
Spring Boot Starter
@SpringBootApplication
@Configuration @EnableAutoConfiguration @ComponentScan
Build and Run the Jar
 mvn clean install
 java -jar target/demo-0.0.1-SNAPSHOT.jar
Test
http://localhost:8080/
Hello Web!
 Understanding MVC Architecture
 Understanding Templates
 First Spring based web-app
MVC
Architecture
Spring MVC
Architecture
The DispatcherServlet
The Spring Web model-view-controller (MVC) framework
is designed around a DispatcherServlet that handles all
the HTTP requests and responses.
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
Demo
GreetingController
/greeting
Model
Template
(greeting.html)
Response
Transactions!
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
Transaction
Commit and Rollback
State 1
Step 1
Step 2
Step 3
State 2
Commit
State 1
Rollback
State after Transaction Ends
Error
Aspects!
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
Spring Security
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
Authentication and Authorization
Spring Test
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
References
 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview-
getting-started-with-spring.html
 http://mohitkanwar.com/blog/2016/03/12/spring.html
 https://en.wikipedia.org/wiki/Spring_Framework
 https://spring.io/guides/gs/managing-transactions/
 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
 https://www.mkyong.com/spring/spring-aop-examples-advice/
 https://dzone.com/articles/aspect-oriented-programming-with-springboot
 http://www.tutorialspoint.com/spring/spring_bean_scopes.htm
 https://docs.spring.io/spring-
boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html
 http://projects.spring.io/spring-security/
 http://www.mkyong.com/tutorials/spring-mvc-tutorials/
 http://www.mkyong.com/tutorials/spring-security-tutorials/
 http://www.slideshare.net/rstoya05/testing-web-apps-with-spring-framework-32
 http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-
spring-mvc-applications-security/
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
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

More Related Content

Spring 1 day program

  • 1. Simple, Portable, Fast and Flexible By Mohit Kanwar
  • 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);
  • 13. Aspect Oriented Programming Accounts Business Logic Persistence Security Logging Users Business Logic Persistence Security Logging Bill Payment Business Logic Persistence Security Logging Location Finder Business Logic Persistence Security Logging Non AOP
  • 14. Aspect Oriented Programming Accounts Business Logic Users Business Logic Bill Payment Business Logic Location Finder Business Logic AOP PersistenceSecurityLogging Caching
  • 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
  • 17. Typical Full Fledged Web Application Custom Domain Logic Aspects Web Views APIs Persistence Models Notifications Remote Automation Testing
  • 19. Various Spring Projects Spring-Boot Spring-Batch Spring- Security Spring- Framework Spring-Cloud Spring- Integration
  • 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
  • 24. Overview Setting up workspace 1 Hello World in Spring 2 Spring Web- MVC 3 Spring Transactions 4 Aspects 5
  • 25. My First Spring Based Application  Creating Environment  Choosing dependencies  Setting it all up!
  • 26. Spring Boot it Up  Spring Initializr  https://start.spring.io/
  • 27. Demo
  • 28. Hello World! My first Rest API using Spring
  • 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
  • 33. Spring Boot Starter @SpringBootApplication @Configuration @EnableAutoConfiguration @ComponentScan
  • 34. Build and Run the Jar  mvn clean install  java -jar target/demo-0.0.1-SNAPSHOT.jar Test http://localhost:8080/
  • 35. Hello Web!  Understanding MVC Architecture  Understanding Templates  First Spring based web-app
  • 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
  • 43. Transaction Commit and Rollback State 1 Step 1 Step 2 Step 3 State 2 Commit State 1 Rollback State after Transaction Ends Error
  • 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
  • 51. References  https://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview- getting-started-with-spring.html  http://mohitkanwar.com/blog/2016/03/12/spring.html  https://en.wikipedia.org/wiki/Spring_Framework  https://spring.io/guides/gs/managing-transactions/  https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html  https://www.mkyong.com/spring/spring-aop-examples-advice/  https://dzone.com/articles/aspect-oriented-programming-with-springboot  http://www.tutorialspoint.com/spring/spring_bean_scopes.htm  https://docs.spring.io/spring- boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html  http://projects.spring.io/spring-security/  http://www.mkyong.com/tutorials/spring-mvc-tutorials/  http://www.mkyong.com/tutorials/spring-security-tutorials/  http://www.slideshare.net/rstoya05/testing-web-apps-with-spring-framework-32  http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of- spring-mvc-applications-security/
  • 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

  1. 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
  2. Spring handles the infrastructure so you can focus on your application.
  3. At this point you like to add Caching. What changes are required?
  4. http://mohitkanwar.com/blog/2016/03/12/spring.html
  5. http://mohitkanwar.com/blog/2016/03/12/spring.html