Reference Documentation
3.0
Copyright © 2004-2011 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, Rossen Stoyanchev
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
XML shortcut with the c-namespace ........................................................49
Compound property names ......................................................................50
Using depends-on ...........................................................................................50
Lazy-initialized beans .....................................................................................51
Autowiring collaborators .................................................................................51
Limitations and disadvantages of autowiring ............................................53
Excluding a bean from autowiring ...........................................................53
Method injection ............................................................................................54
Lookup method injection .........................................................................55
Arbitrary method replacement .................................................................56
3.5. Bean scopes .....................................................................................................57
The singleton scope ........................................................................................58
The prototype scope ........................................................................................59
Singleton beans with prototype-bean dependencies ...........................................60
Request, session, and global session scopes ......................................................60
Initial web configuration .........................................................................60
Request scope .........................................................................................61
Session scope .........................................................................................61
Global session scope ...............................................................................62
Spring Framework
3.0 Reference Documentation iii
Scoped beans as dependencies .................................................................62
Custom scopes ...............................................................................................64
Creating a custom scope ..........................................................................64
Using a custom scope ..............................................................................65
3.6. Customizing the nature of a bean .......................................................................66
Lifecycle callbacks .........................................................................................67
Initialization callbacks ............................................................................67
Destruction callbacks ..............................................................................68
Default initialization and destroy methods ................................................68
Combining lifecycle mechanisms ............................................................70
Startup and shutdown callbacks ...............................................................70
Shutting down the Spring IoC container gracefully in non-web applications 72
ApplicationContextAware and BeanNameAware ..............................................73
Other Aware interfaces ...................................................................................73
3.7. Bean definition inheritance ...............................................................................75
3.8. Container Extension Points ...............................................................................77
Customizing beans using a BeanPostProcessor .................................................77
Example: Hello World, BeanPostProcessor-style ......................................78
Example: The RequiredAnnotationBeanPostProcessor ..............................79
Customizing configuration metadata with a BeanFactoryPostProcessor ..............80
Example: the PropertyPlaceholderConfigurer ...........................................81
Example: the PropertyOverrideConfigurer ...............................................82
Customizing instantiation logic with a FactoryBean ..........................................83
3.9. Annotation-based container configuration ..........................................................84
@Required .....................................................................................................85
@Autowired and @Inject ................................................................................86
Fine-tuning annotation-based autowiring with qualifiers ...................................88
CustomAutowireConfigurer ............................................................................94
@Resource .....................................................................................................94
@PostConstruct and @PreDestroy ...................................................................96
3.10. Classpath scanning and managed components ..................................................96
@Component and further stereotype annotations ..............................................97
Automatically detecting classes and registering bean definitions ........................97
Using filters to customize scanning ..................................................................98
Defining bean metadata within components ......................................................99
Naming autodetected components ..................................................................100
Providing a scope for autodetected components ..............................................101
Providing qualifier metadata with annotations ................................................102
3.11. Using JSR 330 standard annotations ..............................................................103
Dependency Injection with @Inject and @Named ..........................................103
@Named: a standard equivalent to the @Component annotation ......................104
Limitations of the standard approach ..............................................................105
3.12. Java-based container configuration ................................................................105
Basic concepts: @Configuration and @Bean ..................................................105
Instantiating the Spring container using AnnotationConfigApplicationContext .106
Spring Framework
3.0 Reference Documentation iv
Simple construction ..............................................................................106
Building the container programmatically using register(Class<?>...) ........107
Enabling component scanning with scan(String...) ..................................107
Support for web applications with AnnotationConfigWebApplicationContext
.............................................................................................................108
Composing Java-based configurations ............................................................109
Using the @Import annotation ...............................................................109
Combining Java and XML configuration ................................................111
Using the @Bean annotation .........................................................................114
Declaring a bean ...................................................................................114
Injecting dependencies ..........................................................................114
Receiving lifecycle callbacks .................................................................115
Specifying bean scope ...........................................................................116
Customizing bean naming .....................................................................117
Bean aliasing ........................................................................................118
Further information about how Java-based configuration works internally ........118
3.13. Registering a LoadTimeWeaver ....................................................................119
3.14. Additional Capabilities of the ApplicationContext ..........................................119
Internationalization using MessageSource ......................................................120
Standard and Custom Events .........................................................................123
Convenient access to low-level resources .......................................................126
Convenient ApplicationContext instantiation for web applications ...................126
Deploying a Spring ApplicationContext as a J2EE RAR file ............................127
3.15. The BeanFactory ..........................................................................................128
BeanFactory or ApplicationContext? .............................................................128
Glue code and the evil singleton ....................................................................130
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
Spring Framework
3.0 Reference Documentation v
- 1
- 2
前往页