Spring Framework Reference Documentation
4.3.19.RELEASE
Rod Johnson , Juergen Hoeller , Keith Donald , Colin Sampaleanu , Rob Harrop , Thomas Risberg , Alef
Arendsen , Darren Davison , Dmitriy Kopylenko , Mark Pollack , Thierry Templier , Erwin Vervaet , Portia
Tung , Ben Hale , Adrian Colyer , John Lewis , Costin Leau , Mark Fisher , Sam Brannen , Ramnivas
Laddad , Arjen Poutsma , Chris Beams , Tareq Abedrabbo , Andy Clement , Dave Syer , Oliver Gierke ,
Rossen Stoyanchev , Phillip Webb , Rob Winch , Brian Clozel , Stephane Nicoll , Sebastien Deleuze
Copyright © 2004-2016
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee
for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
Spring Framework Reference Documentation
4.3.19.RELEASE Spring Framework iii
Table of Contents
I. Overview of Spring Framework ................................................................................................ 1
1. Getting Started with Spring ............................................................................................. 2
2. Introduction to the Spring Framework .............................................................................. 3
2.1. Dependency Injection and Inversion of Control ...................................................... 3
2.2. Framework Modules ............................................................................................ 3
Core Container .................................................................................................. 4
AOP and Instrumentation ................................................................................... 5
Messaging ......................................................................................................... 5
Data Access/Integration ...................................................................................... 5
Web .................................................................................................................. 5
Test ................................................................................................................... 6
2.3. Usage scenarios ................................................................................................. 6
Dependency Management and Naming Conventions ............................................ 9
Spring Dependencies and Depending on Spring ......................................... 11
Maven Dependency Management ............................................................. 11
Maven "Bill Of Materials" Dependency ....................................................... 12
Gradle Dependency Management ............................................................. 12
Ivy Dependency Management ................................................................... 13
Distribution Zip Files ................................................................................. 13
Logging ............................................................................................................ 13
Using Log4j 1.2 or 2.x .............................................................................. 14
Avoiding Commons Logging ...................................................................... 15
Using SLF4J with Log4j or Logback .......................................................... 15
Using JUL (java.util.logging) ...................................................................... 16
Commons Logging on WebSphere ............................................................ 17
II. What’s New in Spring Framework 4.x .................................................................................... 18
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 19
3.1. Improved Getting Started Experience .................................................................. 19
3.2. Removed Deprecated Packages and Methods .................................................... 19
3.3. Java 8 (as well as 6 and 7) ............................................................................... 19
3.4. Java EE 6 and 7 ............................................................................................... 20
3.5. Groovy Bean Definition DSL .............................................................................. 20
3.6. Core Container Improvements ............................................................................ 21
3.7. General Web Improvements ............................................................................... 21
3.8. WebSocket, SockJS, and STOMP Messaging ..................................................... 21
3.9. Testing Improvements ........................................................................................ 22
4. New Features and Enhancements in Spring Framework 4.1 ............................................ 23
4.1. JMS Improvements ............................................................................................ 23
4.2. Caching Improvements ...................................................................................... 23
4.3. Web Improvements ............................................................................................ 24
4.4. WebSocket Messaging Improvements ................................................................. 25
4.5. Testing Improvements ........................................................................................ 25
5. New Features and Enhancements in Spring Framework 4.2 ............................................ 27
5.1. Core Container Improvements ............................................................................ 27
5.2. Data Access Improvements ................................................................................ 29
5.3. JMS Improvements ............................................................................................ 29
5.4. Web Improvements ............................................................................................ 29
Spring Framework Reference Documentation
4.3.19.RELEASE Spring Framework iv
5.5. WebSocket Messaging Improvements ................................................................. 30
5.6. Testing Improvements ........................................................................................ 31
6. New Features and Enhancements in Spring Framework 4.3 ............................................ 33
6.1. Core Container Improvements ............................................................................ 33
6.2. Data Access Improvements ................................................................................ 33
6.3. Caching Improvements ...................................................................................... 34
6.4. JMS Improvements ............................................................................................ 34
6.5. Web Improvements ............................................................................................ 34
6.6. WebSocket Messaging Improvements ................................................................. 35
6.7. Testing Improvements ........................................................................................ 35
6.8. Support for new library and server generations .................................................... 36
III. Core Technologies .............................................................................................................. 37
7. The IoC container ........................................................................................................ 38
7.1. Introduction to the Spring IoC container and beans .............................................. 38
7.2. Container overview ............................................................................................ 38
Configuration metadata ..................................................................................... 39
Instantiating a container .................................................................................... 40
Composing XML-based configuration metadata .......................................... 41
The Groovy Bean Definition DSL .............................................................. 42
Using the container .......................................................................................... 43
7.3. Bean overview ................................................................................................... 43
Naming beans .................................................................................................. 44
Aliasing a bean outside the bean definition ................................................ 45
Instantiating beans ........................................................................................... 46
Instantiation with a constructor .................................................................. 47
Instantiation with a static factory method .................................................... 47
Instantiation using an instance factory method ........................................... 47
7.4. Dependencies ................................................................................................... 49
Dependency Injection ....................................................................................... 49
Constructor-based dependency injection .................................................... 49
Setter-based dependency injection ............................................................ 51
Dependency resolution process ................................................................. 52
Examples of dependency injection ............................................................. 53
Dependencies and configuration in detail ........................................................... 55
Straight values (primitives, Strings, and so on) ........................................... 55
References to other beans (collaborators) .................................................. 57
Inner beans .............................................................................................. 58
Collections ............................................................................................... 58
Null and empty string values ..................................................................... 60
XML shortcut with the p-namespace .......................................................... 61
XML shortcut with the c-namespace .......................................................... 62
Compound property names ....................................................................... 63
Using depends-on ............................................................................................ 63
Lazy-initialized beans ....................................................................................... 63
Autowiring collaborators .................................................................................... 64
Limitations and disadvantages of autowiring ............................................... 65
Excluding a bean from autowiring .............................................................. 66
Method injection ............................................................................................... 66
Lookup method injection ........................................................................... 67
Arbitrary method replacement ................................................................... 69
Spring Framework Reference Documentation
4.3.19.RELEASE Spring Framework v
7.5. Bean scopes ..................................................................................................... 70
The singleton scope ......................................................................................... 71
The prototype scope ......................................................................................... 72
Singleton beans with prototype-bean dependencies ............................................ 73
Request, session, global session, application, and WebSocket scopes ................. 73
Initial web configuration ............................................................................ 74
Request scope ......................................................................................... 74
Session scope .......................................................................................... 75
Global session scope ............................................................................... 75
Application scope ..................................................................................... 75
Scoped beans as dependencies ................................................................ 76
Custom scopes ................................................................................................ 78
Creating a custom scope .......................................................................... 78
Using a custom scope .............................................................................. 79
7.6. Customizing the nature of a bean ....................................................................... 80
Lifecycle callbacks ............................................................................................ 80
Initialization callbacks ............................................................................... 81
Destruction callbacks ................................................................................ 81
Default initialization and destroy methods .................................................. 82
Combining lifecycle mechanisms ............................................................... 83
Startup and shutdown callbacks ................................................................ 84
Shutting down the Spring IoC container gracefully in non-web applications
................................................................................................................. 86
ApplicationContextAware and BeanNameAware ................................................. 86
Other Aware interfaces ..................................................................................... 87
7.7. Bean definition inheritance ................................................................................. 88
7.8. Container Extension Points ................................................................................ 90
Customizing beans using a BeanPostProcessor ................................................. 90
Example: Hello World, BeanPostProcessor-style ........................................ 91
Example: The RequiredAnnotationBeanPostProcessor ............................... 93
Customizing configuration metadata with a BeanFactoryPostProcessor ................ 93
Example: the Class name substitution PropertyPlaceholderConfigurer .......... 94
Example: the PropertyOverrideConfigurer .................................................. 95
Customizing instantiation logic with a FactoryBean ............................................. 96
7.9. Annotation-based container configuration ............................................................ 96
@Required ....................................................................................................... 98
@Autowired ..................................................................................................... 98
Fine-tuning annotation-based autowiring with @Primary .................................... 102
Fine-tuning annotation-based autowiring with qualifiers ..................................... 102
Using generics as autowiring qualifiers ............................................................ 108
CustomAutowireConfigurer .............................................................................. 108
@Resource .................................................................................................... 109
@PostConstruct and @PreDestroy .................................................................. 110
7.10. Classpath scanning and managed components ................................................ 110
@Component and further stereotype annotations ............................................. 111
Meta-annotations ............................................................................................ 111
Automatically detecting classes and registering bean definitions ........................ 112
Using filters to customize scanning .................................................................. 114
Defining bean metadata within components ..................................................... 115
Naming autodetected components ................................................................... 117