2.0
Copyright © 2004-2006 Rod Johnson, Juergen Hoeller, Alef Arendsen, Colin Sampaleanu,
Rob Harrop, Thomas Risberg, Darren Davison, Dmitriy Kopylenko, Mark Pollack, Thierry
Templier, Erwin Vervaet, Portia Tung, Ben Hale, Adrian Colyer, John Lewis, Costin Leau,
Rick Evans
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.
Preface ................................................................................................................................................ xiv
1. Introduction .................................................................................................................................. 15
1.1. Overview ............................................................................................................................. 15
1.2. Usage scenarios .................................................................................................................... 17
2. What's new in Spring 2.0? ............................................................................................................. 20
2.1. Introduction ......................................................................................................................... 20
2.2. The Inversion of Control (IoC) container ............................................................................... 20
2.2.1. Easier XML configuration .......................................................................................... 20
2.2.2. New bean scopes ....................................................................................................... 20
2.2.3. Extensible XML authoring ......................................................................................... 21
2.3. Aspect Oriented Programming (AOP) .................................................................................... 21
2.3.1. Easier AOP XML configuration ................................................................................. 21
2.3.2. Support for @AspectJ aspects .................................................................................... 21
2.4. The Middle Tier ................................................................................................................... 21
2.4.1. Easier configuration of declarative transactions in XML .............................................. 22
2.4.2. JPA .......................................................................................................................... 22
2.4.3. Asynchronous JMS .................................................................................................... 22
2.4.4. JDBC ........................................................................................................................ 22
2.5. The Web Tier ....................................................................................................................... 22
2.5.1. A form tag library for Spring MVC ............................................................................. 22
2.5.2. Sensible defaulting in Spring MVC ............................................................................. 23
2.5.3. Portlet framework ...................................................................................................... 23
2.6. Everything else .................................................................................................................... 23
2.6.1. Dynamic language support ......................................................................................... 23
2.6.2. JMX ......................................................................................................................... 23
2.6.3. Task scheduling ......................................................................................................... 23
2.6.4. Java 5 (Tiger) support ................................................................................................ 23
2.7. Migrating to Spring 2.0 ......................................................................................................... 24
2.7.1. Changes .................................................................................................................... 24
2.8. Updated sample applications ................................................................................................. 25
2.9. Improved documentation ...................................................................................................... 26
I. Core Technologies ........................................................................................................................... 27
3. The IoC container ................................................................................................................. 28
3.1. Introduction ................................................................................................................. 28
3.2. Basics - containers and beans ........................................................................................ 28
3.2.1. The container .................................................................................................... 29
3.2.2. Instantiating a container ..................................................................................... 30
3.2.3. The beans .......................................................................................................... 31
3.2.4. Using the container ............................................................................................ 35
3.3. Dependencies ............................................................................................................... 36
3.3.1. Injecting dependencies ....................................................................................... 36
3.3.2. Constructor Argument Resolution ....................................................................... 40
3.3.3. Bean properties and constructor arguments detailed ............................................. 42
3.3.4. Using depends-on ............................................................................................. 48
3.3.5. Lazily-instantiating beans ................................................................................... 49
3.3.6. Autowiring collaborators .................................................................................... 50
3.3.7. Checking for dependencies ................................................................................. 51
3.3.8. Method Injection ............................................................................................... 52
3.4. Bean scopes ................................................................................................................. 55
3.4.1. The singleton scope ........................................................................................... 56
3.4.2. The prototype scope ........................................................................................... 57
3.4.3. The other scopes ................................................................................................ 58
Spring Framework (2.0) ii
3.4.4. Custom scopes .................................................................................................. 62
3.5. Customizing the nature of a bean ................................................................................... 63
3.5.1. Lifecycle interfaces ........................................................................................... 63
3.5.2. Knowing who you are ........................................................................................ 66
3.6. Bean definition inheritance ........................................................................................... 68
3.7. Container extension points ............................................................................................ 70
3.7.1. Customizing beans using BeanPostProcessors ................................................... 70
3.7.2. Customizing configuration metadata with BeanFactoryPostProcessors ............... 72
3.7.3. Customizing instantiation logic using FactoryBeans ............................................ 75
3.8. The ApplicationContext ............................................................................................. 75
3.8.1. Internationalization using MessageSources ......................................................... 76
3.8.2. Events ............................................................................................................... 78
3.8.3. Convenient access to low-level resources ............................................................ 80
3.8.4. Convenient ApplicationContext instantiation for web applications ..................... 80
3.9. Glue code and the evil singleton .................................................................................... 81
3.9.1. Using the Singleton-helper classes ...................................................................... 82
4. Resources .............................................................................................................................. 83
4.1. Introduction ................................................................................................................. 83
4.2. The Resource interface ................................................................................................. 83
4.3. Built-in Resource
implementations .............................................................................................................. 84
4.3.1. UrlResource ..................................................................................................... 84
4.3.2. ClassPathResource ........................................................................................... 84
4.3.3. FileSystemResource ......................................................................................... 85
4.3.4. ServletContextResource .................................................................................. 85
4.3.5. InputStreamResource ....................................................................................... 85
4.3.6. ByteArrayResource ........................................................................................... 85
4.4. The ResourceLoader .................................................................................................... 85
4.5. The ResourceLoaderAware
interface .......................................................................................................................... 86
4.6. Setting Resources as
properties ........................................................................................................................ 87
4.7. Application contexts and Resource
paths ............................................................................................................................... 87
4.7.1. Constructing application contexts ....................................................................... 87
4.7.2. Wildcards in application context constructor resource
paths ..................................................................................................................... 88
4.7.3. FileSystemResource caveats ............................................................................. 90
5. Validation, Data-binding, the BeanWrapper, and PropertyEditors ......................................... 92
5.1. Introduction ................................................................................................................. 92
5.2. Validation using Spring's Validator interface ................................................................ 92
5.3. Resolving codes to error messages ................................................................................. 94
5.4. Bean manipulation and the BeanWrapper ........................................................................ 94
5.4.1. Setting and getting basic and nested properties .................................................... 94
5.4.2. Built-in PropertyEditor implementations ............................................................. 96
6. Aspect Oriented Programming with Spring .......................................................................... 100
6.1. Introduction ................................................................................................................. 100
6.1.1. AOP concepts ................................................................................................... 100
6.1.2. Spring AOP capabilities and goals ...................................................................... 102
6.1.3. AOP Proxies in Spring ....................................................................................... 102
6.2. @AspectJ support ........................................................................................................ 103
6.2.1. Enabling @AspectJ Support ............................................................................... 103
The Spring Framework - Reference Documentation
Spring Framework (2.0) iii
6.2.2. Declaring an aspect ............................................................................................ 103
6.2.3. Declaring a pointcut ........................................................................................... 104
6.2.4. Declaring advice ................................................................................................ 109
6.2.5. Introductions ..................................................................................................... 115
6.2.6. Aspect instantiation models ................................................................................ 115
6.2.7. Example ........................................................................................................... 116
6.3. Schema-based AOP support .......................................................................................... 117
6.3.1. Declaring an aspect ............................................................................................ 118
6.3.2. Declaring a pointcut ........................................................................................... 118
6.3.3. Declaring advice ................................................................................................ 119
6.3.4. Introductions ..................................................................................................... 123
6.3.5. Aspect instantiation models ................................................................................ 124
6.3.6. Advisors ........................................................................................................... 124
6.3.7. Example ........................................................................................................... 125
6.4. Choosing which AOP declaration style to use ................................................................ 126
6.4.1. Spring AOP or full AspectJ? .............................................................................. 127
6.4.2. @AspectJ or XML for Spring AOP? ................................................................... 127
6.5. Mixing aspect types ...................................................................................................... 128
6.6. Proxying mechanisms ................................................................................................... 128
6.7. Programmatic creation of @AspectJ Proxies .................................................................. 128
6.8. Using AspectJ with Spring applications ......................................................................... 129
6.8.1. Using AspectJ to dependency inject domain objects with Spring ........................... 129
6.8.2. Other Spring aspects for AspectJ ........................................................................ 131
6.8.3. Configuring AspectJ aspects using Spring IoC ..................................................... 132
6.8.4. Using AspectJ Load-time weaving (LTW) with Spring applications ...................... 133
6.9. Further Resources ........................................................................................................ 134
7. Spring AOP APIs .................................................................................................................. 135
7.1. Introduction ................................................................................................................. 135
7.2. Pointcut API in Spring .................................................................................................. 135
7.2.1. Concepts ........................................................................................................... 135
7.2.2. Operations on pointcuts ..................................................................................... 136
7.2.3. AspectJ expression pointcuts .............................................................................. 136
7.2.4. Convenience pointcut implementations ............................................................... 136
7.2.5. Pointcut superclasses ......................................................................................... 138
7.2.6. Custom pointcuts ............................................................................................... 138
7.3. Advice API in Spring ................................................................................................... 138
7.3.1. Advice lifecycles ............................................................................................... 138
7.3.2. Advice types in Spring ....................................................................................... 139
7.4. Advisor API in Spring .................................................................................................. 144
7.5. Using the ProxyFactoryBean to create AOP proxies ....................................................... 144
7.5.1. Basics ............................................................................................................... 144
7.5.2. JavaBean properties ........................................................................................... 145
7.5.3. JDK- and CGLIB-based proxies ......................................................................... 146
7.5.4. Proxying interfaces ............................................................................................ 147
7.5.5. Proxying classes ................................................................................................ 148
7.5.6. Using 'global' advisors ....................................................................................... 149
7.6. Concise proxy definitions ............................................................................................. 149
7.7. Creating AOP proxies programmatically with the ProxyFactory ...................................... 150
7.8. Manipulating advised objects ........................................................................................ 151
7.9. Using the "autoproxy" facility ....................................................................................... 152
7.9.1. Autoproxy bean definitions ................................................................................ 152
7.9.2. Using metadata-driven auto-proxying ................................................................. 154
The Spring Framework - Reference Documentation
Spring Framework (2.0) iv
7.10. Using TargetSources ................................................................................................... 156
7.10.1. Hot swappable target sources ............................................................................ 156
7.10.2. Pooling target sources ...................................................................................... 157
7.10.3. Prototype target sources ................................................................................... 158
7.10.4. ThreadLocal target sources .............................................................................. 158
7.11. Defining new Advice types ......................................................................................... 159
7.12. Further resources ........................................................................................................ 159
8. Testing .................................................................................................................................. 160
8.1. Introduction ................................................................................................................. 160
8.2. Unit testing .................................................................................................................. 160
8.3. Integration testing ........................................................................................................ 160
8.3.1. Context management and caching ....................................................................... 161
8.3.2. Dependency Injection of test fixtures .................................................................. 161
8.3.3. Transaction management .................................................................................... 163
8.3.4. Convenience variables ....................................................................................... 163
8.3.5. Example ........................................................................................................... 164
8.3.6. Running integration tests .................................................................................... 165
8.4. Further Resources ........................................................................................................ 165
II. Middle Tier Data Access ................................................................................................................. 166
9. Transaction management ...................................................................................................... 167
9.1. Introduction ................................................................................................................. 167
9.2. Motivations .................................................................................................................. 167
9.3. Key abstractions ........................................................................................................... 169
9.4. Resource synchronization with transactions ................................................................... 171
9.4.1. High-level approach .......................................................................................... 171
9.4.2. Low-level approach ........................................................................................... 172
9.4.3. TransactionAwareDataSourceProxy .................................................................. 172
9.5. Declarative transaction management .............................................................................. 172
9.5.1. Understanding Spring's declarative transaction implementation ............................ 174
9.5.2. A first example .................................................................................................. 175
9.5.3. Rolling back ...................................................................................................... 178
9.5.4. Configuring different transactional semantics for different beans .......................... 178
9.5.5. <tx:advice/> settings ....................................................................................... 180
9.5.6. Using @Transactional ...................................................................................... 181
9.5.7. Advising transactional operations ....................................................................... 184
9.5.8. Using @Transactional with AspectJ .................................................................. 186
9.6. Programmatic transaction management .......................................................................... 187
9.6.1. Using the TransactionTemplate ........................................................................ 187
9.6.2. Using the PlatformTransactionManager ............................................................ 188
9.7. Choosing between programmatic and declarative transaction management ....................... 188
9.8. Application server-specific integration ........................................................................... 189
9.8.1. BEA WebLogic ................................................................................................. 189
9.8.2. IBM WebSphere ................................................................................................ 189
9.9. Solutions to common problems ..................................................................................... 189
9.9.1. Use of the wrong transaction manager for a specific DataSource .......................... 189
9.10. Further Resources ....................................................................................................... 190
10. DAO support ....................................................................................................................... 191
10.1. Introduction ............................................................................................................... 191
10.2. Consistent exception hierarchy .................................................................................... 191
10.3. Consistent abstract classes for DAO support ................................................................. 192
11. Data access using JDBC ...................................................................................................... 193
11.1. Introduction ............................................................................................................... 193
The Spring Framework - Reference Documentation
Spring Framework (2.0) v