(Ioc) Container
Total Page:16
File Type:pdf, Size:1020Kb
2.0 Copyright © 2004-2006 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, Rick Evans 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. Preface ................................................................................................................................................ xiv 1. Introduction .................................................................................................................................. 15 1.1. Overview ............................................................................................................................. 15 1.2. Usage scenarios .................................................................................................................... 17 2. What's new in Spring 2.0? ............................................................................................................. 20 2.1. Introduction ......................................................................................................................... 20 2.2. The Inversion of Control (IoC) container ............................................................................... 20 2.2.1. Easier XML configuration .......................................................................................... 20 2.2.2. New bean scopes ....................................................................................................... 20 2.2.3. Extensible XML authoring ......................................................................................... 21 2.3. Aspect Oriented Programming (AOP) .................................................................................... 21 2.3.1. Easier AOP XML configuration ................................................................................. 21 2.3.2. Support for @AspectJ aspects .................................................................................... 21 2.4. The Middle Tier ................................................................................................................... 21 2.4.1. Easier configuration of declarative transactions in XML .............................................. 22 2.4.2. JPA .......................................................................................................................... 22 2.4.3. Asynchronous JMS .................................................................................................... 22 2.4.4. JDBC ........................................................................................................................ 22 2.5. The Web Tier ....................................................................................................................... 22 2.5.1. A form tag library for Spring MVC ............................................................................. 22 2.5.2. Sensible defaulting in Spring MVC ............................................................................. 23 2.5.3. Portlet framework ...................................................................................................... 23 2.6. Everything else .................................................................................................................... 23 2.6.1. Dynamic language support ......................................................................................... 23 2.6.2. JMX ......................................................................................................................... 23 2.6.3. Task scheduling ......................................................................................................... 23 2.6.4. Java 5 (Tiger) support ................................................................................................ 23 2.7. Migrating to Spring 2.0 ......................................................................................................... 24 2.7.1. Changes .................................................................................................................... 24 2.8. Updated sample applications ................................................................................................. 25 2.9. Improved documentation ...................................................................................................... 26 I. Core Technologies ........................................................................................................................... 27 3. The IoC container ................................................................................................................. 28 3.1. Introduction ................................................................................................................. 28 3.2. Basics - containers and beans ........................................................................................ 28 3.2.1. The container .................................................................................................... 29 3.2.2. Instantiating a container ..................................................................................... 30 3.2.3. The beans .......................................................................................................... 31 3.2.4. Using the container ............................................................................................ 35 3.3. Dependencies ............................................................................................................... 36 3.3.1. Injecting dependencies ....................................................................................... 36 3.3.2. Constructor Argument Resolution ....................................................................... 40 3.3.3. Bean properties and constructor arguments detailed ............................................. 42 3.3.4. Using depends-on ............................................................................................. 48 3.3.5. Lazily-instantiating beans ................................................................................... 49 3.3.6. Autowiring collaborators .................................................................................... 50 3.3.7. Checking for dependencies ................................................................................. 51 3.3.8. Method Injection ............................................................................................... 52 3.4. Bean scopes ................................................................................................................. 55 3.4.1. The singleton scope ........................................................................................... 56 3.4.2. The prototype scope ........................................................................................... 57 3.4.3. The other scopes ................................................................................................ 58 Spring Framework (2.0) ii The Spring Framework - Reference Documentation 3.4.4. Custom scopes .................................................................................................. 62 3.5. Customizing the nature of a bean ................................................................................... 63 3.5.1. Lifecycle interfaces ........................................................................................... 63 3.5.2. Knowing who you are ........................................................................................ 66 3.6. Bean definition inheritance ........................................................................................... 68 3.7. Container extension points ............................................................................................ 70 3.7.1. Customizing beans using BeanPostProcessors ................................................... 70 3.7.2. Customizing configuration metadata with BeanFactoryPostProcessors ............... 72 3.7.3. Customizing instantiation logic using FactoryBeans ............................................ 75 3.8. The ApplicationContext ............................................................................................. 75 3.8.1. Internationalization using MessageSources ......................................................... 76 3.8.2. Events ............................................................................................................... 78 3.8.3. Convenient access to low-level resources ............................................................ 80 3.8.4. Convenient ApplicationContext instantiation for web applications ..................... 80 3.9. Glue code and the evil singleton .................................................................................... 81 3.9.1. Using the Singleton-helper classes ...................................................................... 82 4. Resources .............................................................................................................................. 83 4.1. Introduction ................................................................................................................. 83 4.2. The Resource interface ................................................................................................