Reference Documentation
3.0
Copyright © 2004-2010 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, Dave Syer, Oliver Gierke
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 ....................................................................................3
Web .................................................................................................................4
AOP and Instrumentation ..................................................................................4
Test .................................................................................................................4
1.3. Usage scenarios .................................................................................................5
Dependency Management and Naming Conventions ...........................................6
Spring Dependencies and Depending on Spring ..........................................8
Maven Dependency Management ..............................................................8
Ivy Dependency Management .................................................................10
Logging .........................................................................................................10
Not Using Commons Logging .................................................................11
Using SLF4J ..........................................................................................11
Using Log4J ...........................................................................................13
II. What's New in Spring 3.0 ...................................................................................................15
2. New Features and Enhancements in Spring 3.0 .............................................................16
2.1. Java 5 ..............................................................................................................16
2.2. Improved documentation ..................................................................................16
2.3. New articles and tutorials ..................................................................................16
2.4. New module organization and build system .......................................................17
2.5. Overview of new features .................................................................................18
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 .................................................................................................21
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
III. Core Technologies ............................................................................................................23
3. The IoC container .......................................................................................................24
Spring Framework
3.0 Reference Documentation ii
3.1. Introduction to the Spring IoC container and beans .............................................24
3.2. Container overview ..........................................................................................24
Configuration metadata ...................................................................................25
Instantiating a container ..................................................................................26
Composing XML-based configuration metadata .......................................28
Using the container .........................................................................................28
3.3. Bean overview .................................................................................................29
Naming beans .................................................................................................30
Aliasing a bean outside the bean definition ...............................................31
Instantiating beans ..........................................................................................32
Instantiation with a constructor ................................................................32
Instantiation with a static factory method .................................................33
Instantiation using an instance factory method ..........................................33
3.4. Dependencies ...................................................................................................35
Dependency injection ......................................................................................35
Constructor-based dependency injection ...................................................35
Setter-based dependency injection ...........................................................37
Dependency resolution process ................................................................38
Examples of dependency injection ...........................................................40
Dependencies and configuration in detail .........................................................42
Straight values (primitives, Strings, and so on) .........................................42
References to other beans (collaborators) .................................................43
Inner beans .............................................................................................44
Collections .............................................................................................45
Null and empty string values ...................................................................47
XML shortcut with the p-namespace ........................................................48
Compound property names ......................................................................49
Using depends-on ...........................................................................................49
Lazy-initialized beans .....................................................................................50
Autowiring collaborators .................................................................................50
Limitations and disadvantages of autowiring ............................................52
Excluding a bean from autowiring ...........................................................52
Method injection ............................................................................................53
Lookup method injection .........................................................................54
Arbitrary method replacement .................................................................55
3.5. Bean scopes .....................................................................................................56
The singleton scope ........................................................................................57
The prototype scope ........................................................................................58
Singleton beans with prototype-bean dependencies ...........................................59
Request, session, and global session scopes ......................................................59
Initial web configuration .........................................................................59
Request scope .........................................................................................60
Session scope .........................................................................................60
Global session scope ...............................................................................61
Scoped beans as dependencies .................................................................61
Spring Framework
3.0 Reference Documentation iii
Custom scopes ...............................................................................................63
Creating a custom scope ..........................................................................63
Using a custom scope ..............................................................................64
3.6. Customizing the nature of a bean .......................................................................65
Lifecycle callbacks .........................................................................................66
Initialization callbacks ............................................................................66
Destruction callbacks ..............................................................................67
Default initialization and destroy methods ................................................67
Combining lifecycle mechanisms ............................................................69
Startup and shutdown callbacks ...............................................................69
Shutting down the Spring IoC container gracefully in non-web applications 71
ApplicationContextAware and BeanNameAware ..............................................72
Other Aware interfaces ...................................................................................72
3.7. Bean definition inheritance ...............................................................................74
3.8. Container extension points ................................................................................76
Customizing beans using the BeanPostProcessor Interface ................................76
Example: Hello World, BeanPostProcessor-style ......................................77
Example: The RequiredAnnotationBeanPostProcessor ..............................78
Customizing configuration metadata with BeanFactoryPostProcessor interface ...79
Example: the PropertyPlaceholderConfigurer ...........................................80
Example: the PropertyOverrideConfigurer ...............................................81
Customizing instantiation logic with the FactoryBean Interface .........................82
3.9. Annotation-based container configuration ..........................................................83
@Required .....................................................................................................84
@Autowired and @Inject ................................................................................85
Fine-tuning annotation-based autowiring with qualifiers ...................................87
CustomAutowireConfigurer ............................................................................93
@Resource .....................................................................................................93
@PostConstruct and @PreDestroy ...................................................................94
3.10. Classpath scanning and managed components ..................................................95
@Component and further stereotype annotations ..............................................95
Automatically detecting classes and registering bean definitions ........................96
Using filters to customize scanning ..................................................................97
Defining bean metadata within components ......................................................98
Naming autodetected components ....................................................................99
Providing a scope for autodetected components ..............................................100
Providing qualifier metadata with annotations ................................................101
3.11. Java-based container configuration ................................................................102
Basic concepts: @Configuration and @Bean ..................................................102
Instantiating the Spring container using AnnotationConfigApplicationContext .102
Simple construction ..............................................................................103
Building the container programmatically using register(Class<?>...) ........103
Enabling component scanning with scan(String...) ..................................104
Support for web applications with AnnotationConfigWebApplicationContext
.............................................................................................................104
Spring Framework
3.0 Reference Documentation iv
Composing Java-based configurations ............................................................105
Using the @Import annotation ...............................................................105
Combining Java and XML configuration ................................................108
Using the @Bean annotation .........................................................................110
Declaring a bean ...................................................................................110
Injecting dependencies ..........................................................................111
Receiving lifecycle callbacks .................................................................111
Specifying bean scope ...........................................................................112
Customizing bean naming .....................................................................114
Bean aliasing ........................................................................................114
Further information about how Java-based configuration works internally ........114
3.12. Registering a LoadTimeWeaver ....................................................................115
3.13. Additional Capabilities of the ApplicationContext ..........................................116
Internationalization using MessageSource ......................................................116
Standard and Custom Events .........................................................................119
Convenient access to low-level resources .......................................................122
Convenient ApplicationContext instantiation for web applications ...................123
Deploying a Spring ApplicationContext as a J2EE RAR file ............................124
3.14. The BeanFactory ..........................................................................................125
BeanFactory or ApplicationContext? .............................................................125
Glue code and the evil singleton ....................................................................126
4. Resources .................................................................................................................128
4.1. Introduction ...................................................................................................128
4.2. The Resource interface ...................................................................................128
4.3. Built-in Resource implementations ..................................................................129
UrlResource .................................................................................................129
ClassPathResource .......................................................................................130
FileSystemResource .....................................................................................130
ServletContextResource ................................................................................130
InputStreamResource ....................................................................................130
ByteArrayResource ......................................................................................131
4.4. The ResourceLoader .......................................................................................131
4.5. The ResourceLoaderAware interface ...............................................................132
4.6. Resources as dependencies .............................................................................133
4.7. Application contexts and Resource paths .........................................................133
Constructing application contexts ..................................................................133
Constructing ClassPathXmlApplicationContext instances - shortcuts .......134
Wildcards in application context constructor resource paths ............................135
Ant-style Patterns .................................................................................135
The classpath*: prefix ...........................................................................136
Other notes relating to wildcards ............................................................136
FileSystemResource caveats ..........................................................................137
5. Validation, Data Binding, and Type Conversion ..........................................................139
5.1. Introduction ...................................................................................................139
5.2. Validation using Spring's Validator interface ....................................................139
Spring Framework
3.0 Reference Documentation v
- 1
- 2
- 3
前往页