The Spring Framework - Reference Documentation
Version 2.5.6
Copyright © 2004-2008 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,
Mark Fisher, Sam Brannen, Ramnivas Laddad, Arjen Poutsma
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................................................................................................................................................xv
1.Introduction...................................................................................................................................16
1.1.Overview..............................................................................................................................16
1.2.Usagescenarios ....................................................................................................................18
2.What'snewinSpring2.0and2.5? ................................................................................................. 21
2.1.Introduction..........................................................................................................................21
2.2.TheInversionofControl(IoC)container ................................................................................ 21
2.2.1.Newbeanscopes ........................................................................................................21
2.2.2.EasierXMLconfiguration...........................................................................................22
2.2.3.ExtensibleXMLauthoring ..........................................................................................22
2.2.4.Annotation-drivenconfiguration..................................................................................22
2.2.5.Autodetectingcomponentsintheclasspath ..................................................................22
2.3.AspectOrientedProgramming(AOP) ....................................................................................23
2.3.1.EasierAOPXMLconfiguration ..................................................................................23
2.3.2.Supportfor@AspectJaspects .....................................................................................23
2.3.3.Supportforbeannamepointcutelement ...................................................................... 23
2.3.4.SupportforAspectJload-timeweaving ........................................................................23
2.4.TheMiddleTier ....................................................................................................................23
2.4.1.EasierconfigurationofdeclarativetransactionsinXML ............................................... 23
2.4.2.FullWebSpheretransactionmanagementsupport .........................................................24
2.4.3.JPA ...........................................................................................................................24
2.4.4.AsynchronousJMS.....................................................................................................24
2.4.5.JDBC ........................................................................................................................24
2.5.TheWebTier........................................................................................................................25
2.5.1.SensibledefaultinginSpringMVC .............................................................................25
2.5.2.Portletframework.......................................................................................................25
2.5.3.Annotation-basedcontrollers.......................................................................................25
2.5.4.AformtaglibraryforSpringMVC ............................................................................. 25
2.5.5.Tiles2support ...........................................................................................................25
2.5.6.JSF1.2support ..........................................................................................................26
2.5.7.JAX-WSsupport ........................................................................................................26
2.6.Everythingelse .....................................................................................................................26
2.6.1.Dynamiclanguagesupport ..........................................................................................26
2.6.2.Enhancedtestingsupport ............................................................................................26
2.6.3.JMXsupport ..............................................................................................................26
2.6.4.Deploying a Springapplication context as JCAadapter ................................................. 27
2.6.5.Taskscheduling .........................................................................................................27
2.6.6.Java5(Tiger)support .................................................................................................27
2.7.MigratingtoSpring2.5..........................................................................................................27
2.7.1.Changes.....................................................................................................................28
2.8.Updatedsampleapplications ..................................................................................................30
2.9.Improveddocumentation .......................................................................................................30
I.CoreTechnologies............................................................................................................................31
3.TheIoCcontainer ..................................................................................................................32
3.1.Introduction..................................................................................................................32
3.2.Basics-containersandbeans .........................................................................................32
3.2.1.Thecontainer .....................................................................................................33
3.2.2.Instantiatingacontainer ......................................................................................34
3.2.3.Thebeans ..........................................................................................................35
3.2.4.Usingthecontainer.............................................................................................39
3.3.Dependencies................................................................................................................40
3.3.1.Injectingdependencies........................................................................................40
Spring Framework (2.5.6) ii
3.3.2.Dependenciesandconfigurationindetail .............................................................45
3.3.3.Usingdepends-on ..............................................................................................54
3.3.4.Lazily-instantiatedbeans.....................................................................................54
3.3.5.Autowiringcollaborators ....................................................................................55
3.3.6.Checkingfordependencies..................................................................................57
3.3.7.MethodInjection ................................................................................................58
3.4.Beanscopes ..................................................................................................................61
3.4.1.Thesingletonscope ............................................................................................62
3.4.2.Theprototypescope ...........................................................................................63
3.4.3.Singletonbeanswithprototype-beandependencies ...............................................64
3.4.4.Theotherscopes.................................................................................................64
3.4.5.Customscopes ...................................................................................................68
3.5.Customizingthenatureofabean .................................................................................... 70
3.5.1.Lifecyclecallbacks .............................................................................................70
3.5.2.Knowingwhoyouare .........................................................................................74
3.6.Beandefinitioninheritance ............................................................................................76
3.7.Containerextensionpoints .............................................................................................77
3.7.1.CustomizingbeansusingBeanPostProcessors ....................................................78
3.7.2.CustomizingconfigurationmetadatawithBeanFactoryPostProcessors ...............80
3.7.3.CustomizinginstantiationlogicusingFactoryBeans ............................................83
3.8.TheApplicationContext ..............................................................................................84
3.8.1.BeanFactoryorApplicationContext? ................................................................84
3.8.2.InternationalizationusingMessageSources ..........................................................85
3.8.3.Events ...............................................................................................................88
3.8.4.Convenientaccesstolow-levelresources .............................................................89
3.8.5.ConvenientApplicationContext instantiationforwebapplications ...................... 90
3.9.Gluecodeandtheevilsingleton ..................................................................................... 91
3.10. Deploying a Spring ApplicationContext as a J2EE RAR file .......................................... 91
3.11.Annotation-basedconfiguration....................................................................................92
3.11.1.@Required........................................................................................................93
3.11.2.@Autowired ......................................................................................................93
3.11.3.Fine-tuningannotation-basedautowiringwithqualifiers .....................................95
3.11.4.CustomAutowireConfigurer .............................................................................99
3.11.5.@Resource........................................................................................................100
3.11.6.@PostConstructand@PreDestroy ....................................................................101
3.12.Classpathscanningformanagedcomponents ................................................................102
3.12.1.@Component andfurtherstereotypeannotations ...................................................102
3.12.2.Auto-detectingcomponents ...............................................................................102
3.12.3.Usingfilterstocustomizescanning ....................................................................103
3.12.4.Namingautodetectedcomponents......................................................................104
3.12.5.Providingascopeforautodetectedcomponents .................................................. 105
3.12.6.Providingqualifiermetadatawithannotations ....................................................105
3.13.RegisteringaLoadTimeWeaver .....................................................................................106
4.Resources...............................................................................................................................107
4.1.Introduction..................................................................................................................107
4.2.TheResourceinterface..................................................................................................107
4.3.Built-inResourceimplementations ................................................................................108
4.3.1.UrlResource ......................................................................................................108
4.3.2.ClassPathResource ...........................................................................................108
4.3.3.FileSystemResource..........................................................................................109
4.3.4.ServletContextResource...................................................................................109
4.3.5.InputStreamResource ........................................................................................109
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) iii
4.3.6.ByteArrayResource ...........................................................................................109
4.4.TheResourceLoader .....................................................................................................109
4.5.TheResourceLoaderAwareinterface ..............................................................................110
4.6.Resourcesasdependencies............................................................................................111
4.7.ApplicationcontextsandResource paths ........................................................................111
4.7.1.Constructingapplicationcontexts ........................................................................111
4.7.2.Wildcardsinapplicationcontextconstructorresourcepaths .................................. 112
4.7.3.FileSystemResourcecaveats ..............................................................................114
5.Validation,Data-binding,theBeanWrapper,andPropertyEditors .......................................... 116
5.1.Introduction..................................................................................................................116
5.2.ValidationusingSpring'sValidator interface.................................................................116
5.3.Resolvingcodestoerrormessages .................................................................................118
5.4.BeanmanipulationandtheBeanWrapper ........................................................................118
5.4.1.Settingandgettingbasicandnestedproperties ..................................................... 118
5.4.2.Built-inPropertyEditorimplementations ..............................................................120
6.AspectOrientedProgrammingwithSpring ...........................................................................126
6.1.Introduction..................................................................................................................126
6.1.1.AOPconcepts ....................................................................................................126
6.1.2.SpringAOPcapabilitiesandgoals .......................................................................128
6.1.3.AOPProxies ......................................................................................................129
6.2.@AspectJsupport .........................................................................................................129
6.2.1.Enabling@AspectJSupport................................................................................129
6.2.2.Declaringanaspect.............................................................................................130
6.2.3.Declaringapointcut ...........................................................................................130
6.2.4.Declaringadvice ................................................................................................136
6.2.5.Introductions......................................................................................................142
6.2.6.Aspectinstantiationmodels.................................................................................143
6.2.7.Example ............................................................................................................143
6.3.Schema-basedAOPsupport ...........................................................................................145
6.3.1.Declaringanaspect.............................................................................................145
6.3.2.Declaringapointcut ...........................................................................................145
6.3.3.Declaringadvice ................................................................................................147
6.3.4.Introductions......................................................................................................151
6.3.5.Aspectinstantiationmodels.................................................................................152
6.3.6.Advisors ............................................................................................................152
6.3.7.Example ............................................................................................................153
6.4.ChoosingwhichAOPdeclarationstyletouse ................................................................. 154
6.4.1.SpringAOPorfullAspectJ? ...............................................................................154
6.4.2.@AspectJorXMLforSpringAOP? .................................................................... 155
6.5.Mixingaspecttypes.......................................................................................................156
6.6.Proxyingmechanisms....................................................................................................156
6.6.1.UnderstandingAOPproxies ................................................................................157
6.7.Programmaticcreationof@AspectJProxies ...................................................................159
6.8.UsingAspectJwithSpringapplications ..........................................................................159
6.8.1. Using AspectJ to dependency inject domain objects with Spring ............................ 159
6.8.2.OtherSpringaspectsforAspectJ .........................................................................162
6.8.3.ConfiguringAspectJaspectsusingSpringIoC ..................................................... 163
6.8.4.Load-time weaving withAspectJ in the SpringFramework ................................... 163
6.9.FurtherResources .........................................................................................................170
7.SpringAOPAPIs ...................................................................................................................171
7.1.Introduction..................................................................................................................171
7.2.PointcutAPIinSpring ...................................................................................................171
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) iv
7.2.1.Concepts............................................................................................................171
7.2.2.Operationsonpointcuts ......................................................................................172
7.2.3.AspectJexpressionpointcuts ...............................................................................172
7.2.4.Conveniencepointcutimplementations ................................................................172
7.2.5.Pointcutsuperclasses ..........................................................................................174
7.2.6.Custompointcuts................................................................................................174
7.3.AdviceAPIinSpring ....................................................................................................174
7.3.1.Advicelifecycles ................................................................................................174
7.3.2.AdvicetypesinSpring ........................................................................................175
7.4.AdvisorAPIinSpring ...................................................................................................180
7.5.UsingtheProxyFactoryBeantocreateAOPproxies ........................................................ 180
7.5.1.Basics................................................................................................................180
7.5.2.JavaBeanproperties............................................................................................181
7.5.3.JDK-andCGLIB-basedproxies ..........................................................................182
7.5.4.Proxyinginterfaces.............................................................................................183
7.5.5.Proxyingclasses .................................................................................................184
7.5.6.Using'global'advisors ........................................................................................185
7.6.Conciseproxydefinitions ..............................................................................................185
7.7.CreatingAOPproxiesprogrammaticallywiththeProxyFactory ....................................... 186
7.8.Manipulatingadvisedobjects .........................................................................................187
7.9.Usingthe"autoproxy"facility ........................................................................................188
7.9.1.Autoproxybeandefinitions .................................................................................188
7.9.2.Usingmetadata-drivenauto-proxying ..................................................................190
7.10.UsingTargetSources ...................................................................................................192
7.10.1.Hotswappabletargetsources.............................................................................192
7.10.2.Poolingtargetsources .......................................................................................193
7.10.3.Prototypetargetsources ....................................................................................194
7.10.4.ThreadLocaltargetsources ...............................................................................194
7.11.DefiningnewAdvicetypes ..........................................................................................195
7.12.Furtherresources.........................................................................................................195
8.Testing ...................................................................................................................................196
8.1.Introduction..................................................................................................................196
8.2.Unittesting ...................................................................................................................196
8.2.1.Mockobjects......................................................................................................196
8.2.2.Unittestingsupportclasses .................................................................................197
8.3.Integrationtesting .........................................................................................................197
8.3.1.Overview...........................................................................................................197
8.3.2.Whichsupportframeworktouse .........................................................................198
8.3.3.Commongoals ...................................................................................................198
8.3.4.JDBCtestingsupport ..........................................................................................200
8.3.5.Commonannotations ..........................................................................................200
8.3.6.JUnit3.8legacysupport......................................................................................202
8.3.7.SpringTestContextFramework ...........................................................................206
8.3.8.PetClinicexample ..............................................................................................216
8.4.FurtherResources .........................................................................................................218
II.MiddleTierDataAccess..................................................................................................................219
9.Transactionmanagement .......................................................................................................220
9.1.Introduction..................................................................................................................220
9.2.Motivations ..................................................................................................................220
9.3.Keyabstractions............................................................................................................222
9.4.Resourcesynchronizationwithtransactions ....................................................................224
9.4.1.High-levelapproach ...........................................................................................224
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) v