2. About us
Software Engineers
Working with Spring since 2006
Pragmatic programmers
SpringByExample.com.ua
founders
SpringByExample.com.ua
2
@ua_spring
3. Contents
Intro
IoC using Spring
Persistence with Spring
Web applications with Spring MVC
SpringByExample.com.ua
3
@ua_spring
4. Intro
What we will do today?
Meet some new faces
Learn some new stuff
Code some new stuff
SpringByExample.com.ua
4
@ua_spring
5. SpringFramework
history
In October 2002 Rod Johnson
wrote his famous book
The first milestone release 1.0 -
June 2004
Company renamed from
Interface21 to SpringSource in
2007
SpringSource acquired by
VMWare in 2009
SpringByExample.com.ua
5
@ua_spring
7. IoC using Spring
Buiding xml contexts
Bean scopes & lifecycles
Constructor vs Setter injection
Property placeholders
p & c and util name spaces
Annotation context
Using component scan
AOP usage
Spring AOP vs AspectJ
SpringByExample.com.ua
7
@ua_spring
8. Inversion of Control
Inversion of regular approach where object was
responsible for satisfying it’s own dependencies
Implementation of Dependency Injection
Container instantiates and wires dependencies
SpringByExample.com.ua
8
@ua_spring
9. IoC
Pros vs Cons (to discuss)
SpringByExample.com.ua
9
@ua_spring
10. Injection types
Setter injection– when you are using setter to fulfill
bean dependency
Constructor injection – when constructor is called to
set-up a bean with dependencies
SpringByExample.com.ua
10
@ua_spring
11. CI vs SI
Pro vs Cons (to discuss)
SpringByExample.com.ua
11
@ua_spring
12. Context
An object which contains beans declarations with
their dependencies
BeanFactory interface implementation
SpringByExample.com.ua
12
@ua_spring
16. Bean lifecycle
Depends on scope and lazy initialization
Main bean scopes:
Singleton and Prototype
(Also there are multiple web scopes)
SpringByExample.com.ua
16
@ua_spring
18. Bean Post-processing
• Bean instantiated for singletons with
Bean
instantiation
eager initialization
Dependency
• Dependencies are injected into beans
injection
• Bean post-processing actions
Bean post-
processing
• You can put your custom logic in here
SpringByExample.com.ua
18
@ua_spring
28. @Autowired
Spring proprietary annotation for property injection
Supports @Qualifier to select implementation by
name if more than one exists in context
Has required parameter to enforce dependency
loading
SpringByExample.com.ua
28
@ua_spring
45. Spring AOP
Spring AOP defaults to using standard J2SE
dynamic proxies for AOP proxies. This enables any
interface (or set of interfaces) to be proxied.
Spring AOP can also use CGLIB proxies. This is
necessary to proxy classes, rather than interfaces
It is good practice to program to interfaces rather
than classes, business classes normally will
implement one or more business interfaces.
SpringByExample.com.ua
45
@ua_spring
46. AspectJ
Spring AOP is often uses the AspectJ declaration
style
AspectJ itself uses Load Time Weaving (LTW) to
modify the bean code according to Aspect logic
LTW could do much more than dynamic proxies but it
is more time-consuming operation. This means that
LTW has a performance drawback.
SpringByExample.com.ua
46
@ua_spring
47. Persistence with Spring
Building DAO
Embedded Datasources
JDBC vs ORM DAO’s
Integration testing with Spring
@Transactional & transactions with Spring
SpringByExample.com.ua
47
@ua_spring
48. Spring supports
JDBC
JPA
JDO
Concrete ORM
NoSQL templates (Mongo/Redis)
Graph DB abstractions - Spring Data Graph project
SpringByExample.com.ua
48
@ua_spring
49. Persistence layer
• Persistence service which has domain specific logic
Service
• Spring JDBCTemplate
• SessionFactory, EntityManagerFactory
Repository
(DAO) • Redis/Mongo template
• RDBMS (or NoSQL)
DB
SpringByExample.com.ua
49
@ua_spring
53. Intro to Hibernate
Object Relational Mapping - is a programming
technique for converting data between incompatible
type systems in object-oriented programming
languages. This creates, in effect, a "virtual object
database" that can be used from within the
programming language.
SpringByExample.com.ua
53
@ua_spring
59. Read-only transactions
A read-only transaction can be used when your
code reads but does not modify data. Read-only
transactions can be a useful optimization in some
cases, such as when you are using Hibernate.
SpringByExample.com.ua
59
@ua_spring
60. Isolation levels
DEFAULT - The default isolation level for the
underlying resource (usually this is the
READ_COMMITTED isolation level)
SpringByExample.com.ua
60
@ua_spring
61. Isolation levels
READ_UNCOMMITTED - Least restrictive isolation
level. A transaction can read data updates even if
they haven't yet been committed. (The updates
might subsequently be rolled-back).
SpringByExample.com.ua
61
@ua_spring
62. Isolation levels
READ_COMMITTED - A transaction can only read
data that has already been committed. The
transaction doesn't see uncommitted updates.
SpringByExample.com.ua
62
@ua_spring
63. Isolation levels
REPEATABLE_READ - A transaction always reads
the same value after it has read some data. Other
transactions can update the data in parallel with the
original transaction.
SpringByExample.com.ua
63
@ua_spring
64. Isolation levels
SERIALIZABLE - This is the most restrictive
isolation level. Transactions are completely isolated
from each other. Resource managers frequently
implement this isolation level by locking large
amounts of data
SpringByExample.com.ua
64
@ua_spring
66. Propagation
SUPPORTS
If client has transaction, bean method is included in it
If client doesn't have transaction, bean method doesn't
create one
SpringByExample.com.ua
66
@ua_spring
67. Propagation
REQUIRED
If client has a transaction, bean method is included in it
If client doesn't have a transaction, bean method creates
one
SpringByExample.com.ua
67
@ua_spring
68. Propagation
REQUIRES_NEW
If client has a transaction, bean method creates new one
anyway
If client doesn't have a transaction, bean method creates
one
SpringByExample.com.ua
68
@ua_spring
69. Propagation
MANDATORY
If client has a transaction, bean method uses it
If client doesn't have a transaction, exception!
SpringByExample.com.ua
69
@ua_spring
70. Propagation
NEVER
If client has a transaction, exception!
If client doesn't have transaction, bean method doesn't
create one
SpringByExample.com.ua
70
@ua_spring
71. Persistence layer testing
Context loading in test
Environment abstraction and profiles usage
Transactional methods
Embedded data source could be used
SpringByExample.com.ua
71
@ua_spring
73. Spring MVC
Application context
Dispatcher Servlet and it’s context
Controllers and mappings
Implementing CRUD logic
Using Spring for UI tests
SpringByExample.com.ua
73
@ua_spring