Documentation

Telechargé par Abdul Ahad
Spring Framework Reference Documentation
3.2.18.RELEASE
Rod Johnson , Juergen Hoeller , Keith Donald , Colin Sampaleanu , Rob Harrop , Thomas
Risberg , Alef Arendsen , 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 , Chris Beams , Tareq
Abedrabbo , Andy Clement , Dave Syer , Oliver Gierke , Rossen Stoyanchev , Phillip Webb
Copyright © 2004-2016
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.
Spring Framework Reference Documentation
3.2.18.RELEASE Spring Framework iii
Table of Contents
I. Overview of Spring Framework ................................................................................................ 1
1. Introduction to Spring Framework .................................................................................... 2
1.1. Dependency Injection and Inversion of Control ...................................................... 2
1.2. Modules .............................................................................................................. 2
Core Container .................................................................................................. 3
Data Access/Integration ...................................................................................... 3
Web .................................................................................................................. 4
AOP and Instrumentation ................................................................................... 4
Test ................................................................................................................... 4
1.3. Usage scenarios ................................................................................................. 5
Dependency Management and Naming Conventions ............................................ 8
Spring Dependencies and Depending on Spring ......................................... 10
Maven Dependency Management ............................................................. 10
Ivy Dependency Management ................................................................... 11
Logging ............................................................................................................ 12
Not Using Commons Logging ................................................................... 12
Using SLF4J ............................................................................................ 13
Using Log4J ............................................................................................. 14
II. What's New in Spring 3 ........................................................................................................ 16
2. New Features and Enhancements in Spring Framework 3.0 ............................................ 17
2.1. Java 5 .............................................................................................................. 17
2.2. Improved documentation .................................................................................... 17
2.3. New articles and tutorials ................................................................................... 17
2.4. New module organization and build system ......................................................... 18
2.5. Overview of new features .................................................................................. 19
Core APIs updated for Java 5 ........................................................................... 19
Spring Expression Language ............................................................................ 19
The Inversion of Control (IoC) container ............................................................ 20
Java based bean metadata ....................................................................... 20
Defining bean metadata within components ............................................... 21
General purpose type conversion system and field formatting system .................. 21
The Data Tier .................................................................................................. 22
The Web Tier ................................................................................................... 22
Comprehensive REST support .................................................................. 22
@MVC additions ...................................................................................... 22
Declarative model validation ............................................................................. 22
Early support for Java EE 6 .............................................................................. 22
Support for embedded databases ...................................................................... 22
3. New Features and Enhancements in Spring Framework 3.1 ............................................ 23
3.1. Cache Abstraction ............................................................................................. 23
3.2. Bean Definition Profiles ...................................................................................... 23
3.3. Environment Abstraction .................................................................................... 23
3.4. PropertySource Abstraction ................................................................................ 23
3.5. Code equivalents for Spring's XML namespaces ................................................. 23
3.6. Support for Hibernate 4.x ................................................................................... 24
3.7. TestContext framework support for @Configuration classes and bean definition
profiles ..................................................................................................................... 24
Spring Framework Reference Documentation
3.2.18.RELEASE Spring Framework iv
3.8. c: namespace for more concise constructor injection ............................................ 24
3.9. Support for injection against non-standard JavaBeans setters ............................... 24
3.10. Support for Servlet 3 code-based configuration of Servlet Container .................... 25
3.11. Support for Servlet 3 MultipartResolver ............................................................. 25
3.12. JPA EntityManagerFactory bootstrapping without persistence.xml ....................... 25
3.13. New HandlerMethod-based Support Classes For Annotated Controller
Processing ............................................................................................................... 25
3.14. "consumes" and "produces" conditions in @RequestMapping ............................ 26
3.15. Flash Attributes and RedirectAttributes .................................................... 26
3.16. URI Template Variable Enhancements .............................................................. 26
3.17. @Valid On @RequestBody Controller Method Arguments ................................ 27
3.18. @RequestPart Annotation On Controller Method Arguments ............................. 27
3.19. UriComponentsBuilder and UriComponents .............................................. 27
4. New Features and Enhancements in Spring Framework 3.2 ............................................ 28
4.1. Support for Servlet 3 based asynchronous request processing .............................. 28
4.2. Spring MVC Test framework .............................................................................. 28
4.3. Content negotiation improvements ...................................................................... 28
4.4. @ControllerAdvice annotation ...................................................................... 28
4.5. Matrix variables ................................................................................................. 29
4.6. Abstract base class for code-based Servlet 3+ container initialization .................... 29
4.7. ResponseEntityExceptionHandler class ..................................................... 29
4.8. Support for generic types in the RestTemplate and in @RequestBody
arguments ................................................................................................................ 29
4.9. Jackson JSON 2 and related improvements ........................................................ 29
4.10. Tiles 3 ............................................................................................................. 29
4.11. @RequestBody improvements ......................................................................... 30
4.12. HTTP PATCH method ...................................................................................... 30
4.13. Excluded patterns in mapped interceptors ......................................................... 30
4.14. Using meta-annotations for injection points and for bean definition methods ......... 30
4.15. Initial support for JCache 0.5 ............................................................................ 30
4.16. Support for @DateTimeFormat without Joda Time ........................................... 30
4.17. Global date & time formatting ........................................................................... 30
4.18. New Testing Features ...................................................................................... 30
4.19. Concurrency refinements across the framework ................................................. 31
4.20. New Gradle-based build and move to GitHub .................................................... 31
4.21. Refined Java SE 7 / OpenJDK 7 support .......................................................... 31
III. Core Technologies .............................................................................................................. 32
5. The IoC container ........................................................................................................ 33
5.1. Introduction to the Spring IoC container and beans .............................................. 33
5.2. Container overview ............................................................................................ 33
Configuration metadata ..................................................................................... 34
Instantiating a container .................................................................................... 35
Composing XML-based configuration metadata .......................................... 36
Using the container .......................................................................................... 37
5.3. Bean overview ................................................................................................... 38
Naming beans .................................................................................................. 39
Aliasing a bean outside the bean definition ................................................ 39
Instantiating beans ........................................................................................... 40
Instantiation with a constructor .................................................................. 40
Instantiation with a static factory method .................................................... 41
Spring Framework Reference Documentation
3.2.18.RELEASE Spring Framework v
Instantiation using an instance factory method ........................................... 41
5.4. Dependencies ................................................................................................... 42
Dependency injection ....................................................................................... 42
Constructor-based dependency injection .................................................... 43
Setter-based dependency injection ............................................................ 45
Dependency resolution process ................................................................. 46
Examples of dependency injection ............................................................. 47
Dependencies and configuration in detail ........................................................... 49
Straight values (primitives, Strings, and so on) ....................................... 49
References to other beans (collaborators) .................................................. 50
Inner beans .............................................................................................. 51
Collections ............................................................................................... 51
Null and empty string values ..................................................................... 54
XML shortcut with the p-namespace .......................................................... 54
XML shortcut with the c-namespace .......................................................... 55
Compound property names ....................................................................... 56
Using depends-on .......................................................................................... 57
Lazy-initialized beans ....................................................................................... 57
Autowiring collaborators .................................................................................... 58
Limitations and disadvantages of autowiring ............................................... 59
Excluding a bean from autowiring .............................................................. 59
Method injection ............................................................................................... 60
Lookup method injection ........................................................................... 60
Arbitrary method replacement ................................................................... 62
5.5. Bean scopes ..................................................................................................... 63
The singleton scope ......................................................................................... 64
The prototype scope ......................................................................................... 64
Singleton beans with prototype-bean dependencies ............................................ 65
Request, session, and global session scopes .................................................... 66
Initial web configuration ............................................................................ 66
Request scope ......................................................................................... 66
Session scope .......................................................................................... 67
Global session scope ............................................................................... 67
Scoped beans as dependencies ................................................................ 67
Custom scopes ................................................................................................ 69
Creating a custom scope .......................................................................... 69
Using a custom scope .............................................................................. 70
5.6. Customizing the nature of a bean ....................................................................... 71
Lifecycle callbacks ............................................................................................ 71
Initialization callbacks ............................................................................... 72
Destruction callbacks ................................................................................ 72
Default initialization and destroy methods .................................................. 73
Combining lifecycle mechanisms ............................................................... 74
Startup and shutdown callbacks ................................................................ 75
Shutting down the Spring IoC container gracefully in non-web applications
................................................................................................................. 76
ApplicationContextAware and BeanNameAware ........................................ 77
Other Aware interfaces .................................................................................... 78
5.7. Bean definition inheritance ................................................................................. 79
5.8. Container Extension Points ................................................................................ 80