2.0.6
Copyright © 2004-2007 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 ...................................................................................................... 25
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 .................................................................................................... 28
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 ............................................................................................. 50
3.3.5. Lazily-instantiated beans .................................................................................... 51
3.3.6. Autowiring collaborators .................................................................................... 51
3.3.7. Checking for dependencies ................................................................................. 53
3.3.8. Method Injection ............................................................................................... 54
3.4. Bean scopes ................................................................................................................. 57
3.4.1. The singleton scope ........................................................................................... 57
3.4.2. The prototype scope ........................................................................................... 58
3.4.3. Singleton beans with prototype-bean dependencies .............................................. 59
Spring Framework (2.0.6) ii
3.4.4. The other scopes ................................................................................................ 60
3.4.5. Custom scopes .................................................................................................. 64
3.5. Customizing the nature of a bean ................................................................................... 65
3.5.1. Lifecycle interfaces ........................................................................................... 65
3.5.2. Knowing who you are ........................................................................................ 69
3.6. Bean definition inheritance ........................................................................................... 71
3.7. Container extension points ............................................................................................ 72
3.7.1. Customizing beans using BeanPostProcessors ................................................... 72
3.7.2. Customizing configuration metadata with BeanFactoryPostProcessors ............... 75
3.7.3. Customizing instantiation logic using FactoryBeans ............................................ 78
3.8. The ApplicationContext ............................................................................................. 78
3.8.1. Internationalization using MessageSources ......................................................... 79
3.8.2. Events ............................................................................................................... 81
3.8.3. Convenient access to low-level resources ............................................................ 83
3.8.4. Convenient ApplicationContext instantiation for web applications ..................... 83
3.9. Glue code and the evil singleton .................................................................................... 84
3.9.1. Using the Singleton-helper classes ...................................................................... 84
4. Resources .............................................................................................................................. 86
4.1. Introduction ................................................................................................................. 86
4.2. The Resource interface ................................................................................................. 86
4.3. Built-in Resource implementations ............................................................................... 87
4.3.1. UrlResource ..................................................................................................... 87
4.3.2. ClassPathResource ........................................................................................... 87
4.3.3. FileSystemResource ......................................................................................... 88
4.3.4. ServletContextResource .................................................................................. 88
4.3.5. InputStreamResource ....................................................................................... 88
4.3.6. ByteArrayResource ........................................................................................... 88
4.4. The ResourceLoader .................................................................................................... 88
4.5. The ResourceLoaderAware interface ............................................................................. 89
4.6. Resources as dependencies ........................................................................................... 90
4.7. Application contexts and Resource paths ....................................................................... 90
4.7.1. Constructing application contexts ....................................................................... 90
4.7.2. Wildcards in application context constructor resource paths ................................. 91
4.7.3. FileSystemResource caveats ............................................................................. 93
5. Validation, Data-binding, the BeanWrapper, and PropertyEditors ......................................... 95
5.1. Introduction ................................................................................................................. 95
5.2. Validation using Spring's Validator interface ................................................................ 95
5.3. Resolving codes to error messages ................................................................................. 97
5.4. Bean manipulation and the BeanWrapper ........................................................................ 97
5.4.1. Setting and getting basic and nested properties .................................................... 97
5.4.2. Built-in PropertyEditor implementations ............................................................. 99
6. Aspect Oriented Programming with Spring .......................................................................... 103
6.1. Introduction ................................................................................................................. 103
6.1.1. AOP concepts ................................................................................................... 103
6.1.2. Spring AOP capabilities and goals ...................................................................... 105
6.1.3. AOP Proxies ..................................................................................................... 106
6.2. @AspectJ support ........................................................................................................ 106
6.2.1. Enabling @AspectJ Support ............................................................................... 106
6.2.2. Declaring an aspect ............................................................................................ 107
6.2.3. Declaring a pointcut ........................................................................................... 107
6.2.4. Declaring advice ................................................................................................ 112
6.2.5. Introductions ..................................................................................................... 117
The Spring Framework - Reference Documentation
Spring Framework (2.0.6) iii
6.2.6. Aspect instantiation models ................................................................................ 118
6.2.7. Example ........................................................................................................... 119
6.3. Schema-based AOP support .......................................................................................... 120
6.3.1. Declaring an aspect ............................................................................................ 121
6.3.2. Declaring a pointcut ........................................................................................... 121
6.3.3. Declaring advice ................................................................................................ 122
6.3.4. Introductions ..................................................................................................... 126
6.3.5. Aspect instantiation models ................................................................................ 127
6.3.6. Advisors ........................................................................................................... 127
6.3.7. Example ........................................................................................................... 128
6.4. Choosing which AOP declaration style to use ................................................................ 129
6.4.1. Spring AOP or full AspectJ? .............................................................................. 129
6.4.2. @AspectJ or XML for Spring AOP? ................................................................... 130
6.5. Mixing aspect types ...................................................................................................... 130
6.6. Proxying mechanisms ................................................................................................... 131
6.6.1. Understanding AOP proxies ............................................................................... 131
6.7. Programmatic creation of @AspectJ Proxies .................................................................. 133
6.8. Using AspectJ with Spring applications ......................................................................... 134
6.8.1. Using AspectJ to dependency inject domain objects with Spring ........................... 134
6.8.2. Other Spring aspects for AspectJ ........................................................................ 136
6.8.3. Configuring AspectJ aspects using Spring IoC ..................................................... 137
6.8.4. Using AspectJ Load-time weaving (LTW) with Spring applications ...................... 138
6.9. Further Resources ........................................................................................................ 139
7. Spring AOP APIs .................................................................................................................. 140
7.1. Introduction ................................................................................................................. 140
7.2. Pointcut API in Spring .................................................................................................. 140
7.2.1. Concepts ........................................................................................................... 140
7.2.2. Operations on pointcuts ..................................................................................... 141
7.2.3. AspectJ expression pointcuts .............................................................................. 141
7.2.4. Convenience pointcut implementations ............................................................... 141
7.2.5. Pointcut superclasses ......................................................................................... 143
7.2.6. Custom pointcuts ............................................................................................... 143
7.3. Advice API in Spring ................................................................................................... 143
7.3.1. Advice lifecycles ............................................................................................... 143
7.3.2. Advice types in Spring ....................................................................................... 144
7.4. Advisor API in Spring .................................................................................................. 149
7.5. Using the ProxyFactoryBean to create AOP proxies ....................................................... 149
7.5.1. Basics ............................................................................................................... 149
7.5.2. JavaBean properties ........................................................................................... 150
7.5.3. JDK- and CGLIB-based proxies ......................................................................... 151
7.5.4. Proxying interfaces ............................................................................................ 152
7.5.5. Proxying classes ................................................................................................ 153
7.5.6. Using 'global' advisors ....................................................................................... 154
7.6. Concise proxy definitions ............................................................................................. 154
7.7. Creating AOP proxies programmatically with the ProxyFactory ...................................... 155
7.8. Manipulating advised objects ........................................................................................ 156
7.9. Using the "autoproxy" facility ....................................................................................... 157
7.9.1. Autoproxy bean definitions ................................................................................ 157
7.9.2. Using metadata-driven auto-proxying ................................................................. 159
7.10. Using TargetSources ................................................................................................... 161
7.10.1. Hot swappable target sources ............................................................................ 161
7.10.2. Pooling target sources ...................................................................................... 162
The Spring Framework - Reference Documentation
Spring Framework (2.0.6) iv
7.10.3. Prototype target sources ................................................................................... 163
7.10.4. ThreadLocal target sources .............................................................................. 163
7.11. Defining new Advice types ......................................................................................... 164
7.12. Further resources ........................................................................................................ 164
8. Testing .................................................................................................................................. 165
8.1. Introduction ................................................................................................................. 165
8.2. Unit testing .................................................................................................................. 165
8.3. Integration testing ........................................................................................................ 165
8.3.1. Context management and caching ....................................................................... 166
8.3.2. Dependency Injection of test fixtures .................................................................. 166
8.3.3. Transaction management .................................................................................... 168
8.3.4. Convenience variables ....................................................................................... 168
8.3.5. Java5+ specific support ...................................................................................... 169
8.3.6. PetClinic example .............................................................................................. 170
8.4. Further Resources ........................................................................................................ 171
II. Middle Tier Data Access ................................................................................................................. 172
9. Transaction management ...................................................................................................... 173
9.1. Introduction ................................................................................................................. 173
9.2. Motivations .................................................................................................................. 173
9.3. Key abstractions ........................................................................................................... 175
9.4. Resource synchronization with transactions ................................................................... 177
9.4.1. High-level approach .......................................................................................... 177
9.4.2. Low-level approach ........................................................................................... 178
9.4.3. TransactionAwareDataSourceProxy .................................................................. 178
9.5. Declarative transaction management .............................................................................. 178
9.5.1. Understanding the Spring Framework's declarative transaction implementation ..... 180
9.5.2. A first example .................................................................................................. 181
9.5.3. Rolling back ...................................................................................................... 184
9.5.4. Configuring different transactional semantics for different beans .......................... 184
9.5.5. <tx:advice/> settings ....................................................................................... 186
9.5.6. Using @Transactional ...................................................................................... 187
9.5.7. Advising transactional operations ....................................................................... 191
9.5.8. Using @Transactional with AspectJ .................................................................. 193
9.6. Programmatic transaction management .......................................................................... 194
9.6.1. Using the TransactionTemplate ........................................................................ 194
9.6.2. Using the PlatformTransactionManager ............................................................ 196
9.7. Choosing between programmatic and declarative transaction management ....................... 197
9.8. Application server-specific integration ........................................................................... 197
9.8.1. BEA WebLogic ................................................................................................. 197
9.8.2. IBM WebSphere ................................................................................................ 197
9.9. Solutions to common problems ..................................................................................... 197
9.9.1. Use of the wrong transaction manager for a specific DataSource .......................... 197
9.10. Further Resources ....................................................................................................... 198
10. DAO support ....................................................................................................................... 199
10.1. Introduction ............................................................................................................... 199
10.2. Consistent exception hierarchy .................................................................................... 199
10.3. Consistent abstract classes for DAO support ................................................................. 200
11. Data access using JDBC ...................................................................................................... 201
11.1. Introduction ............................................................................................................... 201
11.1.1. The package hierarchy ..................................................................................... 201
11.2. Using the JDBC Core classes to control basic JDBC processing and error handling ........ 202
11.2.1. JdbcTemplate .................................................................................................. 202
The Spring Framework - Reference Documentation
Spring Framework (2.0.6) v
- 1
- 2
- 3
前往页