Logo Studenta

Manual de Referencia de Spring _Java_

¡Este material tiene más páginas!

Vista previa del material en texto

The Spring Framework - Reference Documentation
Version 2.5.1
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,
Mark Fisher, Sam Brannen, Ramnivas Laddad
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's new in Spring 2.0 and 2.5? ................................................................................................. 21
2.1.Introduction ..........................................................................................................................21
2.2. The Inversion of Control (IoC) container ................................................................................ 21
2.2.1.Newbeanscopes ........................................................................................................21
2.2.2.EasierXMLconfiguration...........................................................................................21
2.2.3.ExtensibleXMLauthoring ..........................................................................................22
2.2.4.Annotation-drivenconfiguration..................................................................................22
2.2.5.Autodetectingcomponents in theclasspath .................................................................. 22
2.3.AspectOrientedProgramming(AOP) ....................................................................................22
2.3.1.EasierAOPXMLconfiguration ..................................................................................23
2.3.2.Support for@AspectJaspects .....................................................................................23
2.3.3. Support for bean name pointcut element ...................................................................... 23
2.3.4.Support forAspectJ load-timeweaving ........................................................................ 23
2.4.TheMiddleTier ....................................................................................................................23
2.4.1. Easier configuration of declarative transactions in XML ............................................... 23
2.4.2.FullWebSphere transactionmanagement support ......................................................... 23
2.4.3.JPA ...........................................................................................................................24
2.4.4.AsynchronousJMS.....................................................................................................24
2.4.5.JDBC ........................................................................................................................24
2.5.TheWebTier ........................................................................................................................25
2.5.1.Sensibledefaulting inSpringMVC ............................................................................. 25
2.5.2.Portletframework.......................................................................................................25
2.5.3.Annotation-basedcontrollers .......................................................................................25
2.5.4. A form tag library for Spring MVC ............................................................................. 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 Spring application context as JCA adapter ................................................. 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 - containers andbeans ......................................................................................... 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.1) ii
3.3.2.Dependenciesandconfiguration indetail ............................................................. 45
3.3.3.Usingdepends-on ..............................................................................................53
3.3.4.Lazily-instantiatedbeans.....................................................................................54
3.3.5.Autowiringcollaborators ....................................................................................55
3.3.6.Checkingfordependencies..................................................................................57
3.3.7.MethodInjection ................................................................................................57
3.4.Beanscopes..................................................................................................................60
3.4.1.Thesingletonscope ............................................................................................61
3.4.2.Theprototypescope ...........................................................................................62
3.4.3.Singletonbeanswithprototype-beandependencies ............................................... 63
3.4.4.Theotherscopes .................................................................................................64
3.4.5.Customscopes ...................................................................................................68
3.5. Customizing the nature of a bean .................................................................................... 70
3.5.1.Lifecyclecallbacks .............................................................................................70
3.5.2.Knowingwhoyouare .........................................................................................74
3.6.Beandefinitioninheritance ............................................................................................76
3.7.Containerextensionpoints .............................................................................................77
3.7.1.CustomizingbeansusingBeanPostProcessors ....................................................77
3.7.2.CustomizingconfigurationmetadatawithBeanFactoryPostProcessors ............... 80
3.7.3.Customizing instantiation logicusingFactoryBeans ............................................ 83
3.8.TheApplicationContext ..............................................................................................83
3.8.1.BeanFactoryorApplicationContext? ................................................................84
3.8.2.InternationalizationusingMessageSources ..........................................................84
3.8.3.Events ...............................................................................................................87
3.8.4.Convenient access to low-level resources ............................................................. 88
3.8.5. ConvenientApplicationContext instantiation for web applications ...................... 89
3.9. Glue code and the evil singleton ..................................................................................... 90
3.10. Deploying a Spring ApplicationContext as a J2EE RAR file .......................................... 90
3.11.Annotation-basedconfiguration....................................................................................91
3.11.1.@Autowired ......................................................................................................92
3.11.2.Fine-tuningannotation-basedautowiringwithqualifiers ..................................... 94
3.11.3.CustomAutowireConfigurer .............................................................................97
3.11.4.@Resource........................................................................................................97
3.11.5.@PostConstructand@PreDestroy ....................................................................98
3.12.Classpath scanning formanagedcomponents ................................................................ 99
3.12.1.@Componentand further stereotypeannotations ................................................... 99
3.12.2.Auto-detectingcomponents ...............................................................................100
3.12.3.Using filters tocustomize scanning .................................................................... 100
3.12.4.Namingauto-detectedcomponents ....................................................................101
3.12.5. Providing a scope for auto-detected components ................................................. 102
3.12.6.Providingqualifiermetadatawithannotations .................................................... 102
3.13.RegisteringaLoadTimeWeaver .....................................................................................103
4.Resources ...............................................................................................................................104
4.1.Introduction ..................................................................................................................104
4.2.TheResourceinterface ..................................................................................................104
4.3.Built-inResourceimplementations ................................................................................105
4.3.1.UrlResource ......................................................................................................105
4.3.2.ClassPathResource ...........................................................................................105
4.3.3.FileSystemResource..........................................................................................106
4.3.4.ServletContextResource...................................................................................106
4.3.5.InputStreamResource ........................................................................................106
4.3.6.ByteArrayResource ...........................................................................................106
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) iii
4.4.TheResourceLoader .....................................................................................................106
4.5.TheResourceLoaderAwareinterface ..............................................................................107
4.6.Resourcesasdependencies ............................................................................................108
4.7.Applicationcontexts andResourcepaths ........................................................................ 108
4.7.1.Constructingapplicationcontexts ........................................................................108
4.7.2. Wildcards in application context constructor resource paths .................................. 109
4.7.3.FileSystemResourcecaveats ..............................................................................111
5. Validation, Data-binding, theBeanWrapper, andPropertyEditors .......................................... 113
5.1.Introduction ..................................................................................................................113
5.2.ValidationusingSpring'sValidator interface ................................................................. 113
5.3.Resolvingcodes toerrormessages ................................................................................. 115
5.4.Beanmanipulationand theBeanWrapper ........................................................................ 115
5.4.1. Setting and getting basic and nested properties ..................................................... 115
5.4.2.Built-inPropertyEditorimplementations ..............................................................117
6.AspectOrientedProgrammingwithSpring ........................................................................... 123
6.1.Introduction ..................................................................................................................123
6.1.1.AOPconcepts ....................................................................................................123
6.1.2.SpringAOPcapabilities andgoals ....................................................................... 125
6.1.3.AOPProxies ......................................................................................................126
6.2.@AspectJsupport .........................................................................................................126
6.2.1.Enabling@AspectJSupport ................................................................................126
6.2.2.Declaringanaspect.............................................................................................127
6.2.3.Declaringapointcut...........................................................................................127
6.2.4.Declaringadvice ................................................................................................133
6.2.5.Introductions......................................................................................................139
6.2.6.Aspectinstantiationmodels .................................................................................139
6.2.7.Example ............................................................................................................140
6.3.Schema-basedAOPsupport ...........................................................................................141
6.3.1.Declaringanaspect.............................................................................................142
6.3.2.Declaringapointcut ...........................................................................................142
6.3.3.Declaringadvice ................................................................................................143
6.3.4.Introductions......................................................................................................148
6.3.5.Aspectinstantiationmodels .................................................................................149
6.3.6.Advisors ............................................................................................................149
6.3.7.Example ............................................................................................................149
6.4. Choosing which AOP declaration style to use ................................................................. 151
6.4.1.SpringAOPor fullAspectJ? ............................................................................... 151
6.4.2. @AspectJ or XML for Spring AOP? .................................................................... 151
6.5.Mixingaspecttypes .......................................................................................................152
6.6.Proxyingmechanisms....................................................................................................152
6.6.1.UnderstandingAOPproxies ................................................................................153
6.7.Programmaticcreationof@AspectJProxies ................................................................... 155
6.8.UsingAspectJwithSpringapplications .......................................................................... 156
6.8.1. Using AspectJ to dependency inject domain objects with Spring ............................ 156
6.8.2.OtherSpringaspects forAspectJ ......................................................................... 159
6.8.3. Configuring AspectJ aspects using Spring IoC ..................................................... 159
6.8.4. Load-time weaving with AspectJ in the Spring Framework ................................... 160
6.9.FurtherResources .........................................................................................................167
7.SpringAOPAPIs ...................................................................................................................168
7.1.Introduction ..................................................................................................................168
7.2.PointcutAPIinSpring ...................................................................................................168
7.2.1.Concepts............................................................................................................168
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) iv
7.2.2.Operationsonpointcuts ......................................................................................169
7.2.3.AspectJexpressionpointcuts ...............................................................................169
7.2.4.Conveniencepointcutimplementations ................................................................169
7.2.5.Pointcutsuperclasses ..........................................................................................171
7.2.6.Custompointcuts................................................................................................171
7.3.AdviceAPIinSpring ....................................................................................................171
7.3.1.Advicelifecycles ................................................................................................171
7.3.2.Advice types inSpring ........................................................................................172
7.4.AdvisorAPIinSpring ...................................................................................................177
7.5. Using the ProxyFactoryBean to create AOP proxies ........................................................ 177
7.5.1.Basics................................................................................................................177
7.5.2.JavaBeanproperties ............................................................................................178
7.5.3.JDK-andCGLIB-basedproxies ..........................................................................179
7.5.4.Proxyinginterfaces .............................................................................................180
7.5.5.Proxyingclasses .................................................................................................181
7.5.6.Using'global'advisors ........................................................................................182
7.6.Conciseproxydefinitions ..............................................................................................182
7.7. Creating AOP proxies programmatically with the ProxyFactory ....................................... 183
7.8.Manipulatingadvisedobjects .........................................................................................184
7.9.Usingthe"autoproxy"facility ........................................................................................185
7.9.1.Autoproxybeandefinitions .................................................................................185
7.9.2.Usingmetadata-drivenauto-proxying ..................................................................187
7.10.UsingTargetSources ...................................................................................................189
7.10.1.Hotswappable targetsources .............................................................................189
7.10.2.Poolingtargetsources .......................................................................................190
7.10.3.Prototypetargetsources ....................................................................................191
7.10.4.ThreadLocaltargetsources ...............................................................................191
7.11.DefiningnewAdvice types ..........................................................................................192
7.12.Furtherresources .........................................................................................................192
8.Testing ...................................................................................................................................193
8.1.Introduction ..................................................................................................................193
8.2.Unittesting ...................................................................................................................193
8.2.1.Mockobjects......................................................................................................193
8.2.2.Unit testingsupportclasses .................................................................................194
8.3.Integrationtesting .........................................................................................................194
8.3.1.Overview ...........................................................................................................1948.3.2.Whichsupport framework touse ......................................................................... 195
8.3.3.Commongoals ...................................................................................................195
8.3.4.JDBCtestingsupport ..........................................................................................197
8.3.5.Commonannotations ..........................................................................................197
8.3.6.JUnit3.8 legacysupport ......................................................................................199
8.3.7.SpringTestContextFramework ...........................................................................203
8.3.8.PetClinicexample ..............................................................................................213
8.4.FurtherResources .........................................................................................................214
II.MiddleTierDataAccess ..................................................................................................................216
9.Transactionmanagement .......................................................................................................217
9.1.Introduction ..................................................................................................................217
9.2.Motivations ..................................................................................................................217
9.3.Keyabstractions............................................................................................................219
9.4.Resourcesynchronizationwith transactions ....................................................................221
9.4.1.High-levelapproach ...........................................................................................222
9.4.2.Low-levelapproach ............................................................................................222
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) v
9.4.3.TransactionAwareDataSourceProxy ...................................................................222
9.5.Declarativetransactionmanagement ...............................................................................223
9.5.1. Understanding the Spring Framework's declarative transaction implementation ...... 224
9.5.2.Afirstexample ...................................................................................................224
9.5.3.Rollingback.......................................................................................................227
9.5.4. Configuring different transactional semantics for different beans ........................... 229
9.5.5.<tx:advice/>settings ........................................................................................230
9.5.6.Using@Transactional .......................................................................................231
9.5.7.Transactionpropagation......................................................................................236
9.5.8.Advisingtransactionaloperations ........................................................................237
9.5.9.Using@TransactionalwithAspectJ ...................................................................240
9.6.Programmatictransactionmanagement ...........................................................................240
9.6.1.UsingtheTransactionTemplate .........................................................................241
9.6.2.UsingthePlatformTransactionManager .............................................................242
9.7. Choosing between programmatic and declarative transaction management ........................ 243
9.8.Applicationserver-specificintegration............................................................................243
9.8.1.BEAWebLogic ..................................................................................................243
9.8.2.IBMWebSphere ................................................................................................243
9.9.Solutions tocommonproblems ......................................................................................243
9.9.1. Use of the wrong transaction manager for a specific DataSource ........................... 244
9.10.FurtherResources........................................................................................................244
10.DAOsupport ........................................................................................................................245
10.1.Introduction ................................................................................................................245
10.2.Consistentexceptionhierarchy .....................................................................................245
10.3. Consistent abstract classes for DAO support ................................................................. 246
11.DataaccessusingJDBC .......................................................................................................247
11.1.Introduction ................................................................................................................247
11.1.1.Choosingastyle ...............................................................................................247
11.1.2.Thepackagehierarchy ......................................................................................248
11.2. Using the JDBC Core classes to control basic JDBC processing and error handling ......... 248
11.2.1.JdbcTemplate ..................................................................................................248
11.2.2.NamedParameterJdbcTemplate ..........................................................................251
11.2.3.SimpleJdbcTemplate ........................................................................................253
11.2.4.DataSource ......................................................................................................254
11.2.5.SQLExceptionTranslator .................................................................................255
11.2.6.Executingstatements ........................................................................................256
11.2.7.RunningQueries...............................................................................................256
11.2.8.Updatingthedatabase .......................................................................................257
11.2.9.Retrievingauto-generatedkeys ..........................................................................257
11.3.Controllingdatabaseconnections .................................................................................258
11.3.1.DataSourceUtils .............................................................................................258
11.3.2.SmartDataSource .............................................................................................258
11.3.3.AbstractDataSource ........................................................................................258
11.3.4.SingleConnectionDataSource ..........................................................................258
11.3.5.DriverManagerDataSource ...............................................................................258
11.3.6.TransactionAwareDataSourceProxy .................................................................259
11.3.7.DataSourceTransactionManager ......................................................................259
11.3.8.NativeJdbcExtractor .........................................................................................259
11.4.JDBCbatchoperations ................................................................................................260
11.4.1.Batchoperationswith the JdbcTemplate ............................................................. 260
11.4.2.Batchoperationswith theSimpleJdbcTemplate .................................................. 261
11.5. SimplifyingJDBC operations with the SimpleJdbc classes ............................................. 262
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) vi
11.5.1. InsertingdatausingSimpleJdbcInsert ................................................................262
11.5.2.Retrievingauto-generatedkeysusingSimpleJdbcInsert ....................................... 262
11.5.3. Specifying the columns to use for a SimpleJdbcInsert ......................................... 263
11.5.4. Using SqlParameterSource to provide parameter values ...................................... 263
11.5.5. Calling a stored procedure using SimpleJdbcCall ................................................ 264
11.5.6. Declaring parameters to use for a SimpleJdbcCall ............................................... 266
11.5.7.HowtodefineSqlParameters .............................................................................267
11.5.8. Calling a stored function using SimpleJdbcCall .................................................. 267
11.5.9. Returning ResultSet/REF Cursor from a SimpleJdbcCall ..................................... 268
11.6. Modeling JDBC operations as Java objects ................................................................... 269
11.6.1.SqlQuery..........................................................................................................269
11.6.2.MappingSqlQuery .............................................................................................269
11.6.3.SqlUpdate........................................................................................................270
11.6.4.StoredProcedure .............................................................................................270
11.6.5.SqlFunction ....................................................................................................273
11.7. Common issues with parameter and data value handling ................................................ 273
11.7.1. Providing SQL type information for parameters .................................................. 273
11.7.2.HandlingBLOBandCLOBobjects ................................................................... 274
11.7.3. Passing in lists of values for IN clause ............................................................... 275
11.7.4. Handling complex types for stored procedure calls ............................................. 276
12. Object Relational Mapping (ORM) data access .................................................................... 277
12.1.Introduction ................................................................................................................277
12.2.Hibernate ....................................................................................................................278
12.2.1.Resourcemanagement ......................................................................................278
12.2.2.SessionFactory setup in a Spring container ....................................................... 279
12.2.3.TheHibernateTemplate ...................................................................................279
12.2.4. ImplementingSpring-basedDAOswithout callbacks .......................................... 281
12.2.5. Implementing DAOs based on plain Hibernate 3 API .......................................... 281
12.2.6.Programmatictransactiondemarcation ...............................................................282
12.2.7.Declarativetransactiondemarcation ...................................................................283
12.2.8.Transactionmanagementstrategies ....................................................................284
12.2.9.Container resourcesversus local resources ......................................................... 286
12.2.10. Spurious application server warnings when using Hibernate .............................. 287
12.3.JDO............................................................................................................................288
12.3.1.PersistenceManagerFactorysetup ...................................................................288
12.3.2.JdoTemplateandJdoDaoSupport ......................................................................289
12.3.3. Implementing DAOs based on the plain JDO API ............................................... 289
12.3.4.Transactionmanagement...................................................................................291
12.3.5.JdoDialect ......................................................................................................292
12.4.OracleTopLink ...........................................................................................................293
12.4.1.SessionFactoryabstraction ..............................................................................293
12.4.2.TopLinkTemplateandTopLinkDaoSupport ........................................................294
12.4.3. Implementing DAOs based on plain TopLink API .............................................. 295
12.4.4.Transactionmanagement...................................................................................296
12.5.iBATISSQLMaps ......................................................................................................297
12.5.1.SettinguptheSqlMapClient .............................................................................297
12.5.2.UsingSqlMapClientTemplateandSqlMapClientDaoSupport .............................298
12.5.3. Implementing DAOs based on plain iBATIS API ............................................... 299
12.6.JPA ............................................................................................................................300
12.6.1. JPA setup in a Spring environment .................................................................... 300
12.6.2.JpaTemplateandJpaDaoSupport ......................................................................304
12.6.3. Implementing DAOs based on plain JPA ............................................................ 305
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) vii
12.6.4.ExceptionTranslation .......................................................................................307
12.7.TransactionManagement .............................................................................................308
12.8.JpaDialect.................................................................................................................309
III.TheWeb........................................................................................................................................310
13.WebMVCframework ..........................................................................................................311
13.1.Introduction ................................................................................................................311
13.1.1.PluggabilityofotherMVCimplementations ....................................................... 312
13.1.2.FeaturesofSpringWebMVC ........................................................................... 312
13.2.TheDispatcherServlet ..............................................................................................313
13.3.Controllers..................................................................................................................317
13.3.1.AbstractControllerandWebContentGenerator ...............................................318
13.3.2.Othersimplecontrollers ....................................................................................319
13.3.3.TheMultiActionController ............................................................................319
13.3.4.Commandcontrollers ........................................................................................322
13.4.Handlermappings .......................................................................................................323
13.4.1.BeanNameUrlHandlerMapping............................................................................32313.4.2.SimpleUrlHandlerMapping ...............................................................................324
13.4.3. Intercepting requests - theHandlerInterceptor interface ................................... 325
13.5.Viewsandresolvingthem ............................................................................................326
13.5.1. Resolving views - theViewResolver interface .................................................... 327
13.5.2.ChainingViewResolvers ...................................................................................328
13.5.3.Redirectingtoviews .........................................................................................329
13.6.Usinglocales ..............................................................................................................330
13.6.1.AcceptHeaderLocaleResolver ..........................................................................330
13.6.2.CookieLocaleResolver ....................................................................................330
13.6.3.SessionLocaleResolver...................................................................................331
13.6.4.LocaleChangeInterceptor ...............................................................................331
13.7.Usingthemes ..............................................................................................................331
13.7.1.Introduction .....................................................................................................331
13.7.2.Definingthemes ...............................................................................................332
13.7.3.Themeresolvers ...............................................................................................332
13.8.Spring'smultipart (fileupload)support ..........................................................................333
13.8.1.Introduction .....................................................................................................333
13.8.2.UsingtheMultipartResolver ...........................................................................333
13.8.3. Handling a file upload in a form ........................................................................ 334
13.9.UsingSpring's formtag library ..................................................................................... 336
13.9.1.Configuration ...................................................................................................337
13.9.2.Theformtag.....................................................................................................337
13.9.3.Theinputtag ...................................................................................................338
13.9.4.Thecheckboxtag .............................................................................................338
13.9.5.Thecheckboxestag ..........................................................................................340
13.9.6.Theradiobuttontag ........................................................................................340
13.9.7.Theradiobuttonstag ......................................................................................340
13.9.8.Thepasswordtag .............................................................................................341
13.9.9.Theselecttag .................................................................................................341
13.9.10.Theoptiontag ...............................................................................................341
13.9.11.Theoptionstag .............................................................................................342
13.9.12.Thetextareatag ............................................................................................342
13.9.13.Thehiddentag ...............................................................................................343
13.9.14.Theerrorstag ...............................................................................................343
13.10.Handlingexceptions ..................................................................................................345
13.11.Conventionoverconfiguration ...................................................................................345
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) viii
13.11.1.TheController -ControllerClassNameHandlerMapping ...................................345
13.11.2.TheModel -ModelMap (ModelAndView) ............................................................ 346
13.11.3.TheView-RequestToViewNameTranslator .....................................................348
13.12.Annotation-basedcontrollerconfiguration ...................................................................349
13.12.1. Setting up the dispatcher for annotation support ................................................ 349
13.12.2.Definingacontrollerwith@Controller ........................................................... 349
13.12.3.Mappingrequestswith@RequestMapping .........................................................350
13.12.4. Binding request parameters to method parameters with @RequestParam .............. 352
13.12.5. Providing a link to data from the model with @ModelAttribute ......................... 353
13.12.6. Specifying attributes to store in a Session with @SessionAttributes ................. 353
13.12.7.CustomizingWebDataBinderinitialization ........................................................354
13.13.FurtherResources ......................................................................................................354
14.Integratingviewtechnologies ...............................................................................................356
14.1.Introduction ................................................................................................................356
14.2.JSP&JSTL ................................................................................................................356
14.2.1.Viewresolvers .................................................................................................356
14.2.2. 'Plain-old' JSPsversusJSTL ..............................................................................356
14.2.3.Additional tagsfacilitatingdevelopment ............................................................357
14.3.Tiles ...........................................................................................................................357
14.3.1.Dependencies ...................................................................................................357
14.3.2.HowtointegrateTiles .......................................................................................357
14.4.Velocity&FreeMarker ................................................................................................359
14.4.1.Dependencies ...................................................................................................359
14.4.2.Contextconfiguration .......................................................................................359
14.4.3.Creatingtemplates ............................................................................................360
14.4.4.Advancedconfiguration ....................................................................................360
14.4.5.Bindsupport and formhandling ......................................................................... 361
14.5.XSLT .........................................................................................................................366
14.5.1.MyFirstWords ................................................................................................366
14.5.2.Summary .........................................................................................................369
14.6.Documentviews(PDF/Excel) ......................................................................................369
14.6.1.Introduction .....................................................................................................36914.6.2.Configurationandsetup ....................................................................................369
14.7.JasperReports..............................................................................................................371
14.7.1.Dependencies ...................................................................................................372
14.7.2.Configuration ...................................................................................................372
14.7.3.PopulatingtheModelAndView ............................................................................374
14.7.4.WorkingwithSub-Reports ................................................................................375
14.7.5.ConfiguringExporterParameters .......................................................................376
15. Integratingwithotherwebframeworks ............................................................................... 377
15.1.Introduction ................................................................................................................377
15.2.Commonconfiguration ................................................................................................377
15.3.JavaServerFaces .........................................................................................................379
15.3.1.DelegatingVariableResolver..............................................................................379
15.3.2.FacesContextUtils ............................................................................................379
15.4.Struts..........................................................................................................................380
15.4.1.ContextLoaderPlugin ........................................................................................380
15.4.2.ActionSupportClasses ......................................................................................382
15.5.Tapestry......................................................................................................................382
15.5.1.InjectingSpring-managedbeans ........................................................................383
15.6.WebWork ...................................................................................................................389
15.7.FurtherResources........................................................................................................389
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) ix
16.PortletMVCFramework .....................................................................................................390
16.1.Introduction ................................................................................................................390
16.1.1. Controllers - The C in MVC .............................................................................. 391
16.1.2. Views - The V in MVC ..................................................................................... 391
16.1.3.Web-scopedbeans ............................................................................................391
16.2.TheDispatcherPortlet ..............................................................................................391
16.3.TheViewRendererServlet ..........................................................................................393
16.4.Controllers..................................................................................................................394
16.4.1.AbstractControllerandPortletContentGenerator ........................................395
16.4.2.Othersimplecontrollers ....................................................................................396
16.4.3.CommandControllers .......................................................................................396
16.4.4.PortletWrappingController............................................................................397
16.5.Handlermappings .......................................................................................................397
16.5.1.PortletModeHandlerMapping............................................................................398
16.5.2.ParameterHandlerMapping ...............................................................................398
16.5.3.PortletModeParameterHandlerMapping............................................................399
16.5.4.AddingHandlerInterceptors...........................................................................399
16.5.5.HandlerInterceptorAdapter............................................................................400
16.5.6.ParameterMappingInterceptor ........................................................................400
16.6.Viewsandresolvingthem ............................................................................................400
16.7.Multipart (fileupload)support .....................................................................................400
16.7.1.UsingthePortletMultipartResolver ..............................................................401
16.7.2. Handling a file upload in a form ........................................................................ 401
16.8.Handlingexceptions ....................................................................................................404
16.9.Portletapplicationdeployment .....................................................................................404
IV.Integration.....................................................................................................................................406
17. Remoting and web services using Spring .............................................................................. 407
17.1.Introduction ................................................................................................................407
17.2.ExposingservicesusingRMI .......................................................................................408
17.2.1. Exporting the service using theRmiServiceExporter ......................................... 408
17.2.2. Linking in the service at the client ..................................................................... 409
17.3. Using Hessian or Burlap to remotely call services via HTTP .......................................... 409
17.3.1. Wiring up the DispatcherServlet for Hessian and co. ....................................... 409
17.3.2. Exposing your beans by using the HessianServiceExporter .............................. 410
17.3.3. Linking in the service on the client .................................................................... 410
17.3.4.UsingBurlap ....................................................................................................411
17.3.5. Applying HTTP basic authentication to a service exposed through Hessian or
Burlap.........................................................................................................................411
17.4.ExposingservicesusingHTTPinvokers ....................................................................... 411
17.4.1.Exposingtheserviceobject ...............................................................................412
17.4.2. Linking in the service at the client ..................................................................... 412
17.5.Webservices ...............................................................................................................413
17.5.1. Exposing servlet-based web services using JAX-RPC ......................................... 413
17.5.2.AccessingwebservicesusingJAX-RPC ............................................................ 414
17.5.3.RegisteringJAX-RPCBeanMappings ...............................................................415
17.5.4.RegisteringyourownJAX-RPCHandler ........................................................... 416
17.5.5. Exposing servlet-based web services using JAX-WS .......................................... 41617.5.6. Exporting standalone web services using JAX-WS ............................................. 417
17.5.7. Exporting web services using the JAX-WS RI's Spring support ........................... 418
17.5.8.AccessingwebservicesusingJAX-WS .............................................................. 418
17.5.9.ExposingwebservicesusingXFire .................................................................... 419
17.6.JMS............................................................................................................................420
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) x
17.6.1.Server-sideconfiguration ..................................................................................420
17.6.2.Client-sideconfiguration ...................................................................................421
17.7. Auto-detection is not implemented for remote interfaces ................................................ 422
17.8.Considerationswhenchoosinga technology .................................................................. 422
18.EnterpriseJavaBeans (EJB) integration .............................................................................. 423
18.1.Introduction ................................................................................................................423
18.2.AccessingEJBs ...........................................................................................................423
18.2.1.Concepts ..........................................................................................................423
18.2.2.AccessinglocalSLSBs .....................................................................................423
18.2.3.AccessingremoteSLSBs ..................................................................................425
18.2.4. Accessing EJB 2.x SLSBs versus EJB 3 SLSBs .................................................. 425
18.3. Using Spring's EJB implementation support classes ....................................................... 426
18.3.1.EJB2.xbaseclasses .........................................................................................426
18.3.2.EJB3injectioninterceptor ................................................................................427
19.JMS(JavaMessageService) .................................................................................................429
19.1.Introduction ................................................................................................................429
19.2.UsingSpringJMS .......................................................................................................430
19.2.1.JmsTemplate ....................................................................................................430
19.2.2.Connections .....................................................................................................430
19.2.3.DestinationManagement...................................................................................431
19.2.4.MessageListenerContainers .............................................................................431
19.2.5.Transactionmanagement...................................................................................432
19.3.SendingaMessage .......................................................................................................433
19.3.1.UsingMessageConverters ................................................................................434
19.3.2.SessionCallbackandProducerCallback ..........................................................434
19.4.Receivingamessage....................................................................................................435
19.4.1.SynchronousReception.....................................................................................435
19.4.2.AsynchronousReception -Message-DrivenPOJOs ............................................ 435
19.4.3.TheSessionAwareMessageListenerinterface ....................................................436
19.4.4.TheMessageListenerAdapter ..........................................................................436
19.4.5.Processingmessageswithin transactions ............................................................438
19.5.Support for JCAMessageEndpoints ............................................................................. 438
19.6.JMSNamespaceSupport .............................................................................................439
20.JMX .....................................................................................................................................444
20.1.Introduction ................................................................................................................444
20.2.Exportingyourbeans to JMX ....................................................................................... 444
20.2.1.CreatinganMBeanServer ..................................................................................445
20.2.2.ReusinganexistingMBeanServer ......................................................................446
20.2.3.Lazy-initializedMBeans ...................................................................................446
20.2.4.AutomaticregistrationofMBeans .....................................................................447
20.2.5.Controllingtheregistrationbehavior ..................................................................447
20.3. Controlling the management interface of your beans ...................................................... 448
20.3.1.TheMBeanInfoAssemblerInterface ...................................................................448
20.3.2.Usingsource-Levelmetadata .............................................................................448
20.3.3.UsingJDK5.0Annotations ...............................................................................450
20.3.4.Source-LevelMetadataTypes ...........................................................................452
20.3.5.TheAutodetectCapableMBeanInfoAssemblerinterface ......................................453
20.3.6. Defining Management interfaces using Java interfaces ........................................ 454
20.3.7.UsingMethodNameBasedMBeanInfoAssembler ....................................................455
20.4. Controlling theObjectNames for your beans .................................................................. 455
20.4.1.ReadingObjectNamesfromProperties .............................................................455
20.4.2.UsingtheMetadataNamingStrategy ..................................................................456
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) xi
20.4.3.The<context:mbean-export/>element ............................................................457
20.5.JSR-160Connectors ....................................................................................................457
20.5.1.Server-sideConnectors .....................................................................................457
20.5.2.Client-sideConnectors ......................................................................................458
20.5.3.JMXoverBurlap/Hessian/SOAP .......................................................................458
20.6.AccessingMBeansviaProxies .....................................................................................459
20.7.Notifications ...............................................................................................................459
20.7.1.RegisteringListenersforNotifications ...............................................................459
20.7.2.PublishingNotifications ....................................................................................462
20.8.FurtherResources........................................................................................................463
21.JCACCI ..............................................................................................................................46421.1.Introduction ................................................................................................................464
21.2.ConfiguringCCI .........................................................................................................464
21.2.1.Connectorconfiguration....................................................................................464
21.2.2.ConnectionFactoryconfigurationinSpring .......................................................465
21.2.3.ConfiguringCCIconnections ............................................................................465
21.2.4.UsingasingleCCIconnection ........................................................................... 466
21.3.UsingSpring'sCCIaccess support ................................................................................ 466
21.3.1.Recordconversion ............................................................................................467
21.3.2.TheCciTemplate ..............................................................................................467
21.3.3.DAOsupport ....................................................................................................469
21.3.4.Automaticoutput recordgeneration ...................................................................469
21.3.5.Summary .........................................................................................................469
21.3.6. Using a CCI Connection and Interaction directly ............................................ 470
21.3.7.ExampleforCciTemplateusage ........................................................................471
21.4. Modeling CCI access as operation objects ..................................................................... 473
21.4.1.MappingRecordOperation .................................................................................473
21.4.2.MappingCommAreaOperation .............................................................................473
21.4.3.Automaticoutput recordgeneration ...................................................................474
21.4.4.Summary .........................................................................................................474
21.4.5.ExampleforMappingRecordOperationusage ....................................................474
21.4.6.ExampleforMappingCommAreaOperationusage .................................................476
21.5.Transactions................................................................................................................477
22.Email....................................................................................................................................479
22.1.Introduction ................................................................................................................479
22.2.Usage .........................................................................................................................479
22.2.1.BasicMailSenderandSimpleMailMessageusage ............................................... 479
22.2.2. Using theJavaMailSender and theMimeMessagePreparator .............................. 480
22.3.UsingtheJavaMailMimeMessageHelper .......................................................................481
22.3.1.Sendingattachmentsand inline resources ........................................................... 482
22.3.2. Creating email content using a templating library ............................................... 482
23.SchedulingandThreadPooling ............................................................................................485
23.1.Introduction ................................................................................................................485
23.2.Using theOpenSymphonyQuartzScheduler ................................................................. 485
23.2.1.UsingtheJobDetailBean ...................................................................................485
23.2.2.UsingtheMethodInvokingJobDetailFactoryBean .............................................486
23.2.3. Wiring up jobs using triggers and the SchedulerFactoryBean ............................. 486
23.3.UsingJDKTimersupport ............................................................................................487
23.3.1.Creatingcustomtimers .....................................................................................487
23.3.2.UsingtheMethodInvokingTimerTaskFactoryBean .............................................488
23.3.3. Wrapping up: setting up the tasks using the TimerFactoryBean ........................... 488
23.4.TheSpringTaskExecutorabstraction ...........................................................................488
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) xii
23.4.1.TaskExecutortypes ..........................................................................................489
23.4.2.UsingaTaskExecutor ......................................................................................490
24.Dynamiclanguagesupport ...................................................................................................492
24.1.Introduction ................................................................................................................492
24.2.Afirstexample............................................................................................................492
24.3. Defining beans that are backed by dynamic languages ................................................... 494
24.3.1.Commonconcepts ............................................................................................494
24.3.2.JRubybeans .....................................................................................................498
24.3.3.Groovybeans ...................................................................................................500
24.3.4.BeanShellbeans ...............................................................................................502
24.4.Scenarios ....................................................................................................................503
24.4.1.ScriptedSpringMVCControllers ......................................................................503
24.4.2.ScriptedValidators ...........................................................................................504
24.5.Bitsandbobs ..............................................................................................................505
24.5.1.AOP-advisingscriptedbeans ........................................................................... 505
24.5.2.Scoping............................................................................................................505
24.6.FurtherResources........................................................................................................506
25. Annotations and Source Level Metadata Support ................................................................ 507
25.1.Introduction ................................................................................................................507
25.2.Spring'smetadatasupport ............................................................................................508
25.3.Annotations ................................................................................................................509
25.3.1.@Required ......................................................................................................509
25.3.2.Other@Annotations inSpring ...........................................................................510
25.4. Integrationwith JakartaCommonsAttributes ................................................................ 510
25.5.MetadataandSpringAOPautoproxying ....................................................................... 512
25.5.1.Fundamentals ...................................................................................................512
25.5.2.Declarativetransactionmanagement..................................................................513
25.5.3.Pooling ............................................................................................................513
25.5.4.Custommetadata ..............................................................................................514
25.6. Using attributes to minimize MVC web tier configuration .............................................. 514
V.Sampleapplications ........................................................................................................................517
26.Showcaseapplications ..........................................................................................................518
26.1.Introduction ................................................................................................................518
26.2. Spring MVC Controllers implemented in a dynamic language ........................................ 518
26.2.1.Buildanddeployment .......................................................................................518
26.3. Implementing DAOs usingSimpleJdbcTemplate and@Repository ................................ 519
26.3.1.Thedomain ......................................................................................................519
26.3.2.Thedataaccessobjects .....................................................................................519
26.3.3.Build ...............................................................................................................519
A.XMLSchema-basedconfiguration ...................................................................................................520
A.1.Introduction .........................................................................................................................520
A.2.XMLSchema-basedconfiguration ........................................................................................520
A.2.1.Referencingtheschemas ............................................................................................520
A.2.2.Theutilschema .......................................................................................................521
A.2.3.Thejeeschema .........................................................................................................527
A.2.4.Thelangschema .......................................................................................................530
A.2.5.Thejmsschema .........................................................................................................530
A.2.6.Thetx (transaction)schema .......................................................................................531
A.2.7.Theaopschema .........................................................................................................531
A.2.8.Thecontextschema ..................................................................................................532
A.2.9.Thetoolschema .......................................................................................................533
A.2.10.Thebeansschema ...................................................................................................533
The Spring Framework - Reference Documentation
Spring Framework (2.5.1) xiii
A.3.SettingupyourIDE ..............................................................................................................534
A.3.1.SettingupEclipse ......................................................................................................534
A.3.2.SettingupIntelliJ IDEA .............................................................................................537
A.3.3.Integrationissues .......................................................................................................540
B.ExtensibleXMLauthoring ...............................................................................................................541
B.1.Introduction .........................................................................................................................541
B.2.Authoringtheschema ...........................................................................................................541
B.3.CodingaNamespaceHandler .................................................................................................542
B.4.CodingaBeanDefinitionParser ..........................................................................................543
B.5. Registering the handler and the schema .................................................................................. 544
B.5.1.'META-INF/spring.handlers' ...................................................................................544
B.5.2.'META-INF/spring.schemas' .....................................................................................544
B.6. Using a custom extension in your Spring XML configuration .................................................. 544
B.7.Meatierexamples .................................................................................................................545
B.7.1. Nesting custom tags within custom tags ...................................................................... 545
B.7.2.Customattributeson 'normal' elements ........................................................................ 548
B.8.FurtherResources .................................................................................................................549
C.spring-beans-2.0.dtd ...................................................................................................................551
D.spring.tld ........................................................................................................................................560
D.1.Introduction .........................................................................................................................560
D.2.Thebindtag ........................................................................................................................560
D.3.TheescapeBodytag ..............................................................................................................560
D.4.ThehasBindErrorstag ........................................................................................................561
D.5.ThehtmlEscapetag ..............................................................................................................561
D.6.Themessagetag ...................................................................................................................561
D.7.ThenestedPathtag ..............................................................................................................562
D.8.Thethemetag.......................................................................................................................562
D.9.Thetransformtag ...............................................................................................................563
E.spring-form.tld ................................................................................................................................564
E.1.Introduction..........................................................................................................................564
E.2.Thecheckboxtag ..................................................................................................................564
E.3.Thecheckboxestag ..............................................................................................................566
E.4.Theerrorstag .....................................................................................................................567
E.5.Theformtag .........................................................................................................................569
E.6.Thehiddentag .....................................................................................................................570
E.7.Theinputtag .......................................................................................................................570

Otros materiales

Materiales relacionados

50 pag.