Reference Documentation
Total Page:16
File Type:pdf, Size:1020Kb
Reference Documentation Version 1.2.7 (Work in progress) Copyright (c) 2004-2006 Rod Johnson, Juergen Hoeller, Alef Arendsen, Colin Sampaleanu, Rob Harrop, Thomas Risberg, Darren Davison, Dmitriy Kopylenko, Mark Pollack, Thierry Templier, Erwin Vervaet 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 Preface ................................................................................................................................................ 1. Introduction .................................................................................................................................. 1.1. Overview ............................................................................................................................. 1 1.2. Usage scenarios .................................................................................................................... 2 2. Background information ............................................................................................................... 2.1. Inversion of Control / Dependency Injection .......................................................................... 5 3. Beans, BeanFactory and the ApplicationContext .......................................................................... 3.1. Introduction ......................................................................................................................... 6 3.2. BeanFactory and BeanDefinitions - the basics ........................................................................ 6 3.2.1. The BeanFactory ....................................................................................................... 6 3.2.2. The BeanDefinition ................................................................................................... 8 3.2.3. The bean class ........................................................................................................... 8 3.2.4. The bean identifiers (id and name) ............................................................................................................................ 10 3.2.5. To singleton or not to singleton .................................................................................. 10 3.3. Properties, collaborators, autowiring and dependency checking .................................................................................................................................. 11 3.3.1. Setting bean properties and collaborators .................................................................... 11 3.3.2. Constructor Argument Resolution ............................................................................... 14 3.3.3. Bean properties and constructor arguments detailed ..................................................... 15 3.3.4. Method Injection ....................................................................................................... 19 3.3.5. Using depends-on ...................................................................................................... 21 3.3.6. Autowiring collaborators ............................................................................................ 22 3.3.7. Checking for dependencies ......................................................................................... 23 3.4. Customizing the nature of a bean ........................................................................................... 23 3.4.1. Lifecycle interfaces ................................................................................................... 24 3.4.2. Knowing who you are ................................................................................................ 25 3.4.3. FactoryBean .............................................................................................................. 26 3.5. Abstract and child bean definitions ........................................................................................ 26 3.6. Interacting with the BeanFactory ........................................................................................... 27 3.6.1. Obtaining a FactoryBean, not its product ..................................................................... 28 3.7. Customizing beans with BeanPostProcessors ......................................................................... 28 3.8. Customizing bean factories with BeanFactoryPostProcessors .................................................. 28 3.8.1. The PropertyPlaceholderConfigurer ............................................................................ 29 3.8.2. The PropertyOverrideConfigurer ................................................................................ 30 3.9. Registering additional custom PropertyEditors ....................................................................... 30 3.10. Using the alias element to add aliases for existing beans ....................................................... 31 3.11. Introduction to the ApplicationContext ................................................................................ 32 3.12. Added functionality of the ApplicationContext .................................................................................................................. 32 3.12.1. Using the MessageSource ......................................................................................... 32 3.12.2. Propagating events ................................................................................................... 33 3.12.3. Low-level resources and the application context ........................................................ 34 3.13. Customized behavior in the ApplicationContext ................................................................... 35 3.13.1. ApplicationContextAware marker interface ........................................................................................................................ 35 3.13.2. The BeanPostProcessor ............................................................................................ 35 3.13.3. The BeanFactoryPostProcessor ................................................................................. 35 Spring Framework Version 1.2.7 ii Spring - Java/J2EE Application Framework 3.13.4. The PropertyPlaceholderConfigurer .......................................................................... 36 3.14. Registering additional custom PropertyEditors ..................................................................... 36 3.15. Setting a bean property or constructor arg from a property expression ............................................................................................................................... 37 3.16. Setting a bean property or constructor arg from a field value ....................................................................................................................................... 38 3.17. Invoking another method and optionally using the return value. ...................................................................................................................................... 38 3.18. Importing Bean Definitions from One File Into Another ....................................................... 39 3.19. Creating an ApplicationContext from a web application ........................................................ 40 3.20. Glue code and the evil singleton .......................................................................................... 41 3.20.1. Using SingletonBeanFactoryLocator and ContextSingletonBeanFactoryLocator ............................................................................. 41 4. Abstracting Access to Low-Level Resources .................................................................................. 4.1. Overview ............................................................................................................................. 43 4.2. The Resource interface ......................................................................................................... 43 4.3. Built-in Resource implementations ........................................................................................ 44 4.3.1. UrlResource .............................................................................................................. 44 4.3.2. ClassPathResource .................................................................................................... 44 4.3.3. FileSystemResource .................................................................................................. 45 4.3.4. ServletContextResource ............................................................................................. 45 4.3.5. InputStreamResource ................................................................................................. 45 4.3.6. ByteArrayResource ................................................................................................... 45 4.4. The ResourceLoader Interface ............................................................................................... 45 4.5. The ResourceLoaderAware interface ..................................................................................... 46 4.6. Setting Resources as properties ............................................................................................. 46 4.7. Application contexts and Resource