Reference Documentation
3.0
Copyright © 2004-2009 Rod Johnson, Juergen Hoeller, Keith Donald, Colin Sampaleanu, Rob
Harrop, Alef Arendsen, Thomas Risberg, Darren Davison, Dmitriy Kopylenko, Mark Pollack,
Thierry Templier, Erwin Vervaet, Portia Tung, Ben Hale, Adrian Colyer, John Lewis, Costin
Leau, Mark Fisher, Sam Brannen, Ramnivas Laddad, Arjen Poutsma, Chris Beams, Tareq
Abedrabbo, Andy Clement
Copies of this document may be made for your own use and for distribution to others, provided
that you do not charge any fee for such copies and further provided that each copy contains this
Copyright Notice, whether distributed in print or electronically.
Table of Contents
I. Overview of Spring Framework ..............................................................................................1
1. Introduction to Spring Framework ..................................................................................2
1.1. Dependency Injection and Inversion of Control ....................................................2
1.2. Modules ............................................................................................................3
Core Container .................................................................................................3
Data Access/Integration ....................................................................................4
Web .................................................................................................................4
AOP and Instrumentation ..................................................................................5
Test .................................................................................................................5
1.3. Usage scenarios .................................................................................................5
Dependency Management and Naming Conventions ...........................................9
Spring Dependencies and Depending on Spring ........................................11
Maven Dependency Management ............................................................12
Ivy Dependency Management .................................................................13
Logging .........................................................................................................14
Not Using Commons Logging .................................................................14
Using SLF4J ..........................................................................................15
Using Log4J ...........................................................................................16
II. What's New in Spring 3.0 ...................................................................................................18
2. New Features and Enhancements in Spring 3.0 .............................................................19
2.1. Java 5 ..............................................................................................................19
2.2. Improved documentation ..................................................................................19
2.3. New articles and tutorials ..................................................................................19
2.4. New module organization and build system .......................................................20
2.5. Overview of new features .................................................................................21
Core APIs updated for Java 5 ..........................................................................21
Spring Expression Language ...........................................................................22
The Inversion of Control (IoC) container ..........................................................22
Java based bean metadata ........................................................................22
Defining bean metadata within components ..............................................24
General purpose type conversion system and field formatting system .................24
The Data Tier .................................................................................................24
The Web Tier .................................................................................................24
Comprehensive REST support .................................................................24
@MVC additions ...................................................................................25
Declarative model validation ...........................................................................25
Early support for Java EE 6 .............................................................................25
Support for embedded databases ......................................................................25
III. Core Technologies ............................................................................................................26
3. The IoC container .......................................................................................................27
Spring Framework
3.0 Reference Documentation ii
3.1. Introduction to the Spring IoC container and beans .............................................27
3.2. Container overview ..........................................................................................27
Configuration metadata ...................................................................................28
Instantiating a container ..................................................................................30
Composing XML-based configuration metadata .......................................31
Using the container .........................................................................................32
3.3. Bean overview .................................................................................................32
Naming beans .................................................................................................34
Aliasing a bean outside the bean definition ...............................................34
Instantiating beans ..........................................................................................35
Instantiation with a constructor ................................................................36
Instantiation with a static factory method .................................................36
Instantiation using an instance factory method ..........................................37
3.4. Dependencies ...................................................................................................38
Dependency injection ......................................................................................38
Constructor-based dependency injection ...................................................39
Setter-based dependency injection ...........................................................40
Dependency resolution process ................................................................41
Examples of dependency injection ...........................................................43
Dependencies and configuration in detail .........................................................45
Straight values (primitives, Strings, and so on) .........................................45
References to other beans (collaborators) .................................................47
Inner beans .............................................................................................47
Collections .............................................................................................48
Null and empty string values ...................................................................50
XML shortcut with the p-namespace ........................................................51
Compound property names ......................................................................52
Using depends-on ...........................................................................................52
Lazy-initialized beans .....................................................................................53
Autowiring collaborators .................................................................................53
Limitations and disadvantages of autowiring ............................................55
Excluding a bean from autowiring ...........................................................56
Checking for dependencies ..............................................................................56
Method injection ............................................................................................57
Lookup method injection .........................................................................58
Arbitrary method replacement .................................................................59
3.5. Bean scopes .....................................................................................................60
The singleton scope ........................................................................................62
The prototype scope ........................................................................................62
Singleton beans with prototype-bean dependencies ...........................................63
Request, session, and global session scopes ......................................................64
Initial web configuration .........................................................................64
Request scope .........................................................................................65
Session scope .........................................................................................65
Global session scope ...............................................................................65
Spring Framework
3.0 Reference Documentation iii
Scoped beans as dependencies .................................................................66
Custom scopes ...............................................................................................68
Creating a custom scope ..........................................................................68
Using a custom scope ..............................................................................69
3.6. Customizing the nature of a bean .......................................................................70
Lifecycle callbacks .........................................................................................70
Initialization callbacks ............................................................................71
Destruction callbacks ..............................................................................71
Default initialization and destroy methods ................................................72
Combining lifecycle mechanisms ............................................................73
Startup and shutdown callbacks ...............................................................74
Shutting down the Spring IoC container gracefully in non-web applications 76
ApplicationContextAware and BeanNameAware ..............................................76
Other Aware interfaces ...................................................................................77
3.7. Bean definition inheritance ...............................................................................79
3.8. Container extension points ................................................................................81
Customizing beans using the BeanPostProcessor Interface ................................81
Example: Hello World, BeanPostProcessor-style ......................................82
Example: The RequiredAnnotationBeanPostProcessor ..............................83
Customizing configuration metadata with BeanFactoryPostProcessor interface ...84
Example: the PropertyPlaceholderConfigurer ...........................................85
Example: the PropertyOverrideConfigurer ...............................................86
Customizing instantiation logic with the FactoryBean Interface .........................87
3.9. Annotation-based container configuration ..........................................................88
@Required .....................................................................................................89
@Autowired and @Inject ................................................................................90
Fine-tuning annotation-based autowiring with qualifiers ...................................92
CustomAutowireConfigurer ............................................................................97
@Resource .....................................................................................................98
@PostConstruct and @PreDestroy ...................................................................99
3.10. Classpath scanning and managed components ................................................100
@Component and further stereotype annotations ............................................100
Automatically detecting classes and registering bean definitions ......................101
Using filters to customize scanning ................................................................102
Defining bean metadata within components ....................................................103
Naming autodetected components ..................................................................104
Providing a scope for autodetected components ..............................................105
Providing qualifier metadata with annotations ................................................106
3.11. Java-based container configuration ................................................................107
Basic concepts: @Configuration and @Bean ..................................................107
Instantiating the Spring container using AnnotationConfigApplicationContext .107
Simple construction ..............................................................................108
Building the container programmatically using register(Class<?>...) ........108
Enabling component scanning with scan(String...) ..................................108
Support for web applications with AnnotationConfigWebApplicationContext
Spring Framework
3.0 Reference Documentation iv
.............................................................................................................109
Composing Java-based configurations ............................................................110
Using the @Import annotation ...............................................................110
Combining Java and XML configuration ................................................112
Using the @Bean annotation .........................................................................115
Declaring a bean ...................................................................................115
Injecting dependencies ..........................................................................115
Receiving lifecycle callbacks .................................................................116
Specifying bean scope ...........................................................................117
Customizing bean naming .....................................................................118
Bean aliasing ........................................................................................119
3.12. Registering a LoadTimeWeaver ....................................................................119
3.13. Additional Capabilities of the ApplicationContext ..........................................119
Internationalization using MessageSource ......................................................120
Standard and Custom Events .........................................................................123
Convenient access to low-level resources .......................................................125
Convenient ApplicationContext instantiation for web applications ...................126
Deploying a Spring ApplicationContext as a J2EE RAR file ............................127
3.14. The BeanFactory ..........................................................................................128
BeanFactory or ApplicationContext? .............................................................128
Glue code and the evil singleton ....................................................................129
4. Resources .................................................................................................................131
4.1. Introduction ...................................................................................................131
4.2. The Resource interface ...................................................................................131
4.3. Built-in Resource implementations ..................................................................132
UrlResource .................................................................................................132
ClassPathResource .......................................................................................133
FileSystemResource .....................................................................................133
ServletContextResource ................................................................................133
InputStreamResource ....................................................................................133
ByteArrayResource ......................................................................................134
4.4. The ResourceLoader .......................................................................................134
4.5. The ResourceLoaderAware interface ...............................................................135
4.6. Resources as dependencies .............................................................................136
4.7. Application contexts and Resource paths .........................................................136
Constructing application contexts ..................................................................136
Constructing ClassPathXmlApplicationContext instances - shortcuts .......137
Wildcards in application context constructor resource paths ............................138
Ant-style Patterns .................................................................................138
The classpath*: prefix ...........................................................................139
Other notes relating to wildcards ............................................................139
FileSystemResource caveats ..........................................................................140
5. Validation, Data Binding, and Type Conversion ..........................................................142
5.1. Introduction ...................................................................................................142
5.2. Validation using Spring's Validator interface ....................................................142
Spring Framework
3.0 Reference Documentation v
- 1
- 2
- 3
- 4
- 5
前往页