Reference Documentation
Total Page:16
File Type:pdf, Size:1020Kb
Reference Documentation (Work in progress) 3.0.M3 Copyright © 2004-2009 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, Arjen Poutsma, Chris Beams, Tareq Abed Rabbo 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 Table of Contents Preface ................................................................................................................................xxiii 1. Introduction ..........................................................................................................................1 1.1. Dependency Injection .................................................................................................1 1.2. Modules ....................................................................................................................2 Core Container .........................................................................................................3 Data Access/Integration ............................................................................................4 Web .........................................................................................................................4 AOP and Instrumentation ..........................................................................................5 Test .........................................................................................................................5 1.3. Usage scenarios .........................................................................................................5 2. What's new in Spring 3.0? ...................................................................................................10 2.1. Java 5 ......................................................................................................................10 2.2. Improved documentation ..........................................................................................10 2.3. New module organization and build system ...............................................................11 2.4. Overview of new features .........................................................................................12 Core APIs updated for Java 5 ..................................................................................12 Spring Expression Language ...................................................................................13 The Inversion of Control (IoC) container ..................................................................13 Java based bean metadata ................................................................................13 Defining bean metadata within components ......................................................14 The Data Tier .........................................................................................................14 The Web Tier .........................................................................................................14 Comprehensive REST support .........................................................................15 @MVC additions ...........................................................................................15 Declarative model validation ...................................................................................15 Early support for Java EE 6 .....................................................................................15 3. Getting started with Spring ..................................................................................................16 3.1. Creating an ApplicationContext ................................................................................16 3.2. The Data Access Object ............................................................................................16 3.3. The Business Layer ..................................................................................................16 3.4. The Web UI .............................................................................................................16 I. Core Technologies ...............................................................................................................17 4. The IoC container .......................................................................................................18 4.1. Introduction .....................................................................................................18 4.2. Basics - containers and beans ............................................................................18 The container .................................................................................................19 Configuration metadata ...........................................................................20 Instantiating a container ..................................................................................21 Composing XML-based configuration metadata .......................................22 The beans .......................................................................................................23 3.0.M3 Reference Documentation ii Spring Framework Naming beans .........................................................................................24 Instantiating beans ..................................................................................25 Using the container .........................................................................................28 4.3. Dependencies ...................................................................................................28 Injecting dependencies ....................................................................................28 Constructor Injection ..............................................................................28 Setter Injection .......................................................................................30 Some examples ......................................................................................33 Dependencies and configuration in detail .........................................................34 Straight values (primitives, Strings, etc.) ..................................................35 References to other beans (collaborators) .................................................36 Inner beans .............................................................................................37 Collections .............................................................................................38 Nulls ......................................................................................................41 Shortcuts and other convenience options for XML-based configuration metadata ................................................................................................41 Compound property names ......................................................................44 Using depends-on ...........................................................................................44 Lazily-instantiated beans .................................................................................45 Autowiring collaborators .................................................................................46 Excluding a bean from being available for autowiring ...............................48 Checking for dependencies ..............................................................................48 Method Injection ............................................................................................49 Lookup method injection .........................................................................50 Arbitrary method replacement .................................................................51 4.4. Bean scopes .....................................................................................................52 The singleton scope ........................................................................................54 The prototype scope ........................................................................................54 Singleton beans with prototype-bean dependencies ...........................................56 The other scopes .............................................................................................56 Initial web configuration .........................................................................57 The request scope ...................................................................................58 The session scope ...................................................................................58 The global session scope .........................................................................58 Scoped beans as dependencies .................................................................59 Custom scopes ...............................................................................................61 Creating your own custom scope .............................................................61 Using a custom scope ..............................................................................62 4.5. Customizing the nature of a bean .......................................................................63 Lifecycle callbacks