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.
Table of Contents
Preface................................................................................................................................................
1.Introduction...................................................................................................................................
1.1.Overview..............................................................................................................................1
1.2.Usagescenarios ....................................................................................................................3
2.What'snewinSpring2.0and2.5? .................................................................................................
2.1.Introduction..........................................................................................................................6
2.2.TheInversionofControl(IoC)container ................................................................................ 6
2.2.1.Newbeanscopes ........................................................................................................6
2.2.2.EasierXMLconfiguration...........................................................................................6
2.2.3.ExtensibleXMLauthoring ..........................................................................................7
2.2.4.Annotation-drivenconfiguration..................................................................................7
2.2.5.Autodetectingcomponentsintheclasspath ..................................................................7
2.3.AspectOrientedProgramming(AOP) ....................................................................................7
2.3.1.EasierAOPXMLconfiguration ..................................................................................8
2.3.2.Supportfor@AspectJaspects .....................................................................................8
2.3.3.Supportforbeannamepointcutelement ...................................................................... 8
2.3.4.SupportforAspectJload-timeweaving ........................................................................8
2.4.TheMiddleTier ....................................................................................................................8
2.4.1.EasierconfigurationofdeclarativetransactionsinXML ............................................... 8
2.4.2.FullWebSpheretransactionmanagementsupport .........................................................8
2.4.3.JPA ...........................................................................................................................9
2.4.4.AsynchronousJMS.....................................................................................................9
2.4.5.JDBC ........................................................................................................................9
2.5.TheWebTier........................................................................................................................10
2.5.1.SensibledefaultinginSpringMVC .............................................................................10
2.5.2.Portletframework.......................................................................................................10
2.5.3.Annotation-basedcontrollers.......................................................................................10
2.5.4.AformtaglibraryforSpringMVC ............................................................................. 10
2.5.5.Tiles2support ...........................................................................................................10
2.5.6.JSF1.2support ..........................................................................................................11
2.5.7.JAX-WSsupport ........................................................................................................11
2.6.Everythingelse .....................................................................................................................11
2.6.1.Dynamiclanguagesupport ..........................................................................................11
2.6.2.Enhancedtestingsupport ............................................................................................11
2.6.3.JMXsupport ..............................................................................................................11
2.6.4.Deploying a Spring application context as JCA adapter ................................................. 12
2.6.5.Taskscheduling .........................................................................................................12
2.6.6.Java5(Tiger)support .................................................................................................12
2.7.MigratingtoSpring2.5..........................................................................................................12
2.7.1.Changes.....................................................................................................................13
2.8.Updatedsampleapplications ..................................................................................................15
2.9.Improveddocumentation .......................................................................................................15
I.CoreTechnologies............................................................................................................................
3.TheIoCcontainer ..................................................................................................................
3.1.Introduction..................................................................................................................17
3.2.Basics-containersandbeans .........................................................................................17
3.2.1.Thecontainer .....................................................................................................18
3.2.2.Instantiatingacontainer ......................................................................................19
Spring Framework (2.5.6) ii
3.2.3.Thebeans ..........................................................................................................20
3.2.4.Usingthecontainer.............................................................................................24
3.3.Dependencies................................................................................................................25
3.3.1.Injectingdependencies........................................................................................25
3.3.2.Dependenciesandconfigurationindetail .............................................................30
3.3.3.Usingdepends-on ...............................................................................................39
3.3.4.Lazily-instantiatedbeans.....................................................................................39
3.3.5.Autowiringcollaborators ....................................................................................40
3.3.6.Checkingfordependencies..................................................................................42
3.3.7.MethodInjection ................................................................................................43
3.4.Beanscopes ..................................................................................................................46
3.4.1.Thesingletonscope ............................................................................................47
3.4.2.Theprototypescope ...........................................................................................48
3.4.3.Singletonbeanswithprototype-beandependencies ...............................................48
3.4.4.Theotherscopes.................................................................................................49
3.4.5.Customscopes ...................................................................................................53
3.5.Customizingthenatureofabean .................................................................................... 55
3.5.1.Lifecyclecallbacks .............................................................................................55
3.5.2.Knowingwhoyouare .........................................................................................59
3.6.Beandefinitioninheritance ............................................................................................61
3.7.Containerextensionpoints .............................................................................................62
3.7.1.CustomizingbeansusingBeanPostProcessors ......................................................62
3.7.2.CustomizingconfigurationmetadatawithBeanFactoryPostProcessors ...................65
3.7.3.CustomizinginstantiationlogicusingFactoryBeans .............................................68
3.8.TheApplicationContext.................................................................................................68
3.8.1.BeanFactoryorApplicationContext? ...................................................................69
3.8.2.InternationalizationusingMessageSources ..........................................................70
3.8.3.Events ...............................................................................................................72
3.8.4.Convenientaccesstolow-levelresources .............................................................74
3.8.5.ConvenientApplicationContextinstantiationforwebapplications ......................... 75
3.9.Gluecodeandtheevilsingleton ..................................................................................... 75
3.10. Deploying a Spring ApplicationContext as a J2EE RAR file .......................................... 76
3.11.Annotation-basedconfiguration....................................................................................77
3.11.1.@Required.......................................................................................................77
3.11.2.@Autowired.....................................................................................................78
3.11.3.Fine-tuningannotation-basedautowiringwithqualifiers .....................................80
3.11.4.CustomAutowireConfigurer ..............................................................................84
3.11.5.@Resource.......................................................................................................85
3.11.6.@PostConstructand@PreDestroy.....................................................................86
3.12.Classpathscanningformanagedcomponents ................................................................86
3.12.1.@Componentandfurtherstereotypeannotations ................................................86
3.12.2.Auto-detectingcomponents ...............................................................................87
3.12.3.Usingfilterstocustomizescanning ....................................................................88
3.12.4.Namingautodetectedcomponents......................................................................88
3.12.5.Providingascopeforautodetectedcomponents .................................................. 89
3.12.6.Providingqualifiermetadatawithannotations ....................................................90
3.13.RegisteringaLoadTimeWeaver ...................................................................................90
4.Resources...............................................................................................................................
4.1.Introduction..................................................................................................................92
4.2.TheResourceinterface ..................................................................................................92
4.3.Built-inResourceimplementations .................................................................................93
4.3.1.UrlResource.......................................................................................................93
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) iii
4.3.2.ClassPathResource .............................................................................................93
4.3.3.FileSystemResource ...........................................................................................94
4.3.4.ServletContextResource......................................................................................94
4.3.5.InputStreamResource..........................................................................................94
4.3.6.ByteArrayResource ............................................................................................94
4.4.TheResourceLoader......................................................................................................94
4.5.TheResourceLoaderAwareinterface ..............................................................................95
4.6.Resourcesasdependencies ............................................................................................96
4.7.ApplicationcontextsandResourcepaths ........................................................................96
4.7.1.Constructingapplicationcontexts ........................................................................96
4.7.2.Wildcardsinapplicationcontextconstructorresourcepaths .................................. 97
4.7.3.FileSystemResourcecaveats................................................................................99
5.Validation,Data-binding,theBeanWrapper,andPropertyEditors .......................................
5.1.Introduction..................................................................................................................101
5.2.ValidationusingSpring'sValidatorinterface...................................................................101
5.3.Resolvingcodestoerrormessages .................................................................................103
5.4.BeanmanipulationandtheBeanWrapper .......................................................................103
5.4.1.Settingandgettingbasicandnestedproperties ..................................................... 103
5.4.2.Built-inPropertyEditorimplementations ..............................................................105
6.AspectOrientedProgrammingwithSpring ...........................................................................
6.1.Introduction..................................................................................................................111
6.1.1.AOPconcepts ....................................................................................................111
6.1.2.SpringAOPcapabilitiesandgoals .......................................................................113
6.1.3.AOPProxies ......................................................................................................114
6.2.@AspectJsupport .........................................................................................................114
6.2.1.Enabling@AspectJSupport................................................................................114
6.2.2.Declaringanaspect.............................................................................................115
6.2.3.Declaringapointcut ...........................................................................................115
6.2.4.Declaringadvice ................................................................................................121
6.2.5.Introductions......................................................................................................127
6.2.6.Aspectinstantiationmodels.................................................................................127
6.2.7.Example ............................................................................................................128
6.3.Schema-basedAOPsupport ...........................................................................................129
6.3.1.Declaringanaspect.............................................................................................130
6.3.2.Declaringapointcut ...........................................................................................130
6.3.3.Declaringadvice ................................................................................................132
6.3.4.Introductions......................................................................................................136
6.3.5.Aspectinstantiationmodels.................................................................................137
6.3.6.Advisors ............................................................................................................137
6.3.7.Example ............................................................................................................137
6.4.ChoosingwhichAOPdeclarationstyletouse ................................................................. 139
6.4.1.SpringAOPorfullAspectJ? ...............................................................................139
6.4.2.@AspectJorXMLforSpringAOP? .................................................................... 139
6.5.Mixingaspecttypes.......................................................................................................140
6.6.Proxyingmechanisms....................................................................................................141
6.6.1.UnderstandingAOPproxies ................................................................................141
6.7.Programmaticcreationof@AspectJProxies ...................................................................144
6.8.UsingAspectJwithSpringapplications ..........................................................................144
6.8.1. Using AspectJ to dependency inject domain objects with Spring ............................ 144
6.8.2.OtherSpringaspectsforAspectJ .........................................................................147
6.8.3.ConfiguringAspectJaspectsusingSpringIoC ..................................................... 148
6.8.4.Load-time weaving with AspectJ in the Spring Framework ................................... 148
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) iv
6.9.FurtherResources .........................................................................................................155
7.SpringAOPAPIs ...................................................................................................................
7.1.Introduction..................................................................................................................156
7.2.PointcutAPIinSpring ...................................................................................................156
7.2.1.Concepts............................................................................................................156
7.2.2.Operationsonpointcuts ......................................................................................157
7.2.3.AspectJexpressionpointcuts ...............................................................................157
7.2.4.Conveniencepointcutimplementations ................................................................157
7.2.5.Pointcutsuperclasses ..........................................................................................159
7.2.6.Custompointcuts................................................................................................159
7.3.AdviceAPIinSpring ....................................................................................................159
7.3.1.Advicelifecycles ................................................................................................159
7.3.2.AdvicetypesinSpring ........................................................................................160
7.4.AdvisorAPIinSpring ...................................................................................................165
7.5.UsingtheProxyFactoryBeantocreateAOPproxies ........................................................ 165
7.5.1.Basics................................................................................................................165
7.5.2.JavaBeanproperties............................................................................................166
7.5.3.JDK-andCGLIB-basedproxies ..........................................................................167
7.5.4.Proxyinginterfaces.............................................................................................168
7.5.5.Proxyingclasses .................................................................................................169
7.5.6.Using'global'advisors ........................................................................................170
7.6.Conciseproxydefinitions ..............................................................................................170
7.7.CreatingAOPproxiesprogrammaticallywiththeProxyFactory ....................................... 171
7.8.Manipulatingadvisedobjects .........................................................................................171
7.9.Usingthe"autoproxy"facility ........................................................................................173
7.9.1.Autoproxybeandefinitions .................................................................................173
7.9.2.Usingmetadata-drivenauto-proxying ..................................................................175
7.10.UsingTargetSources ...................................................................................................177
7.10.1.Hotswappabletargetsources.............................................................................177
7.10.2.Poolingtargetsources .......................................................................................177
7.10.3.Prototypetargetsources ....................................................................................179
7.10.4.ThreadLocaltargetsources................................................................................179
7.11.DefiningnewAdvicetypes ..........................................................................................179
7.12.Furtherresources.........................................................................................................180
8.Testing ...................................................................................................................................
8.1.Introduction..................................................................................................................181
8.2.Unittesting ...................................................................................................................181
8.2.1.Mockobjects......................................................................................................181
8.2.2.Unittestingsupportclasses .................................................................................182
8.3.Integrationtesting .........................................................................................................182
8.3.1.Overview...........................................................................................................182
8.3.2.Whichsupportframeworktouse .........................................................................183
8.3.3.Commongoals ...................................................................................................183
8.3.4.JDBCtestingsupport ..........................................................................................185
8.3.5.Commonannotations ..........................................................................................185
8.3.6.JUnit3.8legacysupport......................................................................................187
8.3.7.SpringTestContextFramework ...........................................................................191
8.3.8.PetClinicexample ..............................................................................................201
8.4.FurtherResources .........................................................................................................203
II.MiddleTierDataAccess..................................................................................................................
9.Transactionmanagement .......................................................................................................
9.1.Introduction..................................................................................................................205
The Spring Framework - Reference Documentation
Spring Framework (2.5.6) v
- 1
- 2
- 3
- 4
- 5
- 6
前往页