站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework reference 1.2.9 参考手册英文版

Spring - Java/J2EE Application Framework - Spring Framework reference 1.2.9 参考手册英文版

Spring - Java/J2EE Application Framework

Reference Documentation

Rod Johnson

Juergen Hoeller

Alef Arendsen

Colin Sampaleanu

Rob Harrop

Thomas Risberg

Darren Davison

Dmitriy Kopylenko

Mark Pollack

Thierry Templier

Erwin Vervaet

Version 1.2.9

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.

(Work in progress)


Preface
1. Introduction
1.1. Overview
1.2. Usage scenarios
2. Background information
2.1. Inversion of Control / Dependency Injection
3. Beans, BeanFactory and the ApplicationContext
3.1. Introduction
3.2. BeanFactory and BeanDefinitions - the basics
3.2.1. The BeanFactory
3.2.2. The BeanDefinition
3.2.3. The bean class
3.2.3.1. Bean creation via constructor
3.2.3.2. Bean creation via static factory method
3.2.3.3. Bean creation via instance factory method
3.2.4. The bean identifiers (id and name)
3.2.5. To singleton or not to singleton
3.3. Properties, collaborators, autowiring and dependency checking
3.3.1. Setting bean properties and collaborators
3.3.2. Constructor Argument Resolution
3.3.2.1. Constructor Argument Type Matching
3.3.2.2. Constructor Argument Index
3.3.3. Bean properties and constructor arguments detailed
3.3.3.1. The value element
3.3.3.2. The null element
3.3.3.3. The collection elements
3.3.3.4. Inner bean definitions via nested bean elements
3.3.3.5. The idref element
3.3.3.6. The ref element
3.3.3.7. Value and Ref shortcut forms
3.3.3.8. Compound property names
3.3.4. Method Injection
3.3.4.1. Lookup method Injection
3.3.4.2. Arbitrary method replacement
3.3.5. Using depends-on
3.3.6. Autowiring collaborators
3.3.7. Checking for dependencies
3.4. Customizing the nature of a bean
3.4.1. Lifecycle interfaces
3.4.1.1. InitializingBean / init-method
3.4.1.2. DisposableBean / destroy-method
3.4.2. Knowing who you are
3.4.2.1. BeanFactoryAware
3.4.2.2. BeanNameAware
3.4.3. FactoryBean
3.5. Abstract and child bean definitions
3.6. Interacting with the BeanFactory
3.6.1. Obtaining a FactoryBean, not its product
3.7. Customizing beans with BeanPostProcessors
3.8. Customizing bean factories with BeanFactoryPostProcessors
3.8.1. The PropertyPlaceholderConfigurer
3.8.2. The PropertyOverrideConfigurer
3.9. Registering additional custom PropertyEditors
3.10. Using the alias element to add aliases for existing beans
3.11. Introduction to the ApplicationContext
3.12. Added functionality of the ApplicationContext
3.12.1. Using the MessageSource
3.12.2. Propagating events
3.12.3. Low-level resources and the application context
3.13. Customized behavior in the ApplicationContext
3.13.1. ApplicationContextAware marker interface
3.13.2. The BeanPostProcessor
3.13.3. The BeanFactoryPostProcessor
3.13.4. The PropertyPlaceholderConfigurer
3.14. Registering additional custom PropertyEditors
3.15. Setting a bean property or constructor arg from a property expression
3.16. Setting a bean property or constructor arg from a field value
3.17. Invoking another method and optionally using the return value.
3.18. Importing Bean Definitions from One File Into Another
3.19. Creating an ApplicationContext from a web application
3.20. Glue code and the evil singleton
3.20.1. Using SingletonBeanFactoryLocator and ContextSingletonBeanFactoryLocator
4. Abstracting Access to Low-Level Resources
4.1. Overview
4.2. The Resource interface
4.3. Built-in Resource implementations
4.3.1. UrlResource
4.3.2. ClassPathResource
4.3.3. FileSystemResource
4.3.4. ServletContextResource
4.3.5. InputStreamResource
4.3.6. ByteArrayResource
4.4. The ResourceLoader Interface
4.5. The ResourceLoaderAware interface
4.6. Setting Resources as properties
4.7. Application contexts and Resource paths
4.7.1. Constructing application contexts
4.7.2. The classpath*: prefix
4.7.3. Unexpected application context handling of FileSystemResource absolute paths
5. PropertyEditors, data binding, validation and the BeanWrapper
5.1. Introduction
5.2. Binding data using the DataBinder
5.3. Bean manipulation and the BeanWrapper
5.3.1. Setting and getting basic and nested properties
5.3.2. Built-in PropertyEditors, converting types
5.3.3. Other features worth mentioning
5.4. Validation using Spring's Validator interface
5.5. The Errors interface
5.6. Resolving codes to error messages
6. Spring AOP: Aspect Oriented Programming with Spring
6.1. Concepts
6.1.1. AOP concepts
6.1.2. Spring AOP capabilities and goals
6.1.3. AOP Proxies in Spring
6.2. Pointcuts in Spring
6.2.1. Concepts
6.2.2. Operations on pointcuts
6.2.3. Convenience pointcut implementations
6.2.3.1. Static pointcuts
6.2.3.2. Dynamic pointcuts
6.2.4. Pointcut superclasses
6.2.5. Custom pointcuts
6.3. Advice types in Spring
6.3.1. Advice lifecycles
6.3.2. Advice types in Spring
6.3.2.1. Interception around advice
6.3.2.2. Before advice
6.3.2.3. Throws advice
6.3.2.4. After Returning advice
6.3.2.5. Introduction advice
6.4. Advisors in Spring
6.5. Using the ProxyFactoryBean to create AOP proxies
6.5.1. Basics
6.5.2. JavaBean properties
6.5.3. Proxying interfaces
6.5.4. Proxying classes
6.5.5. Using 'global' advisors
6.6. Convenient proxy creation
6.6.1. TransactionProxyFactoryBean
6.6.2. EJB proxies
6.7. Concise proxy definitions
6.8. Creating AOP proxies programmatically with the ProxyFactory
6.9. Manipulating advised objects
6.10. Using the "autoproxy" facility
6.10.1. Autoproxy bean definitions
6.10.1.1. BeanNameAutoProxyCreator
6.10.1.2. DefaultAdvisorAutoProxyCreator
6.10.1.3. AbstractAdvisorAutoProxyCreator
6.10.2. Using metadata-driven auto-proxying
6.11. Using TargetSources
6.11.1. Hot swappable target sources
6.11.2. Pooling target sources
6.11.3. Prototype target sources
6.11.4. ThreadLocal target sources
6.12. Defining new Advice types
6.13. Further reading and resources
7. AspectJ Integration
7.1. Overview
7.2. Configuring AspectJ aspects using Spring IoC
7.2.1. "Singleton" aspects
7.2.1.1. Example
7.2.1.2. Ordering issues
7.2.2. Non-singleton aspects
7.2.3. Gotchas
7.3. Using AspectJ pointcuts to target Spring advice
7.4. Spring aspects for AspectJ
8. Transaction management
8.1. The Spring transaction abstraction
8.2. Transaction strategies
8.3. Resource synchronization with transactions
8.3.1. High-level approach
8.3.2. Low-level approach
8.3.3. TransactionAwareDataSourceProxy
8.4. Programmatic transaction management
8.4.1. Using the TransactionTemplate
8.4.2. Using the PlatformTransactionManager
8.5. Declarative transaction management
8.5.1. Source Annotations for Transaction Demarcation
8.5.1.1. The Transactional Annotation
8.5.2. BeanNameAutoProxyCreator, another declarative approach
8.5.3. AOP and Transactions
8.6. Choosing between programmatic and declarative transaction management
8.7. Do you need an application server for transaction management?
8.8. AppServer-specific integration
8.8.1. BEA WebLogic
8.8.2. IBM WebSphere
8.9. Common problems
8.9.1. Use of the wrong transaction manager for a specific DataSource
8.9.2. Spurious AppServer warnings about the transaction or DataSource no longer being active
9. Source Level Metadata Support
9.1. Source-level metadata
9.2. Spring's metadata support
9.3. Integration with Jakarta Commons Attributes
9.4. Metadata and Spring AOP autoproxying
9.4.1. Fundamentals
9.4.2. Declarative transaction management
9.4.3. Pooling
9.4.4. Custom metadata
9.5. Using attributes to minimize MVC web tier configuration
9.6. Other uses of metadata attributes
9.7. Adding support for additional metadata APIs
10. DAO support
10.1. Introduction
10.2. Consistent Exception Hierarchy
10.3. Consistent Abstract Classes for DAO Support
11. Data Access using JDBC
11.1. Introduction
11.2. Using the JDBC Core classes to control basic JDBC processing and error handling
11.2.1. JdbcTemplate
11.2.2. DataSource
11.2.3. SQLExceptionTranslator
11.2.4. Executing Statements
11.2.5. Running Queries
11.2.6. Updating the database
11.3. Controlling how we connect to the database
11.3.1. DataSourceUtils
11.3.2. SmartDataSource
11.3.3. AbstractDataSource
11.3.4. SingleConnectionDataSource
11.3.5. DriverManagerDataSource
11.3.6. TransactionAwareDataSourceProxy
11.3.7. DataSourceTransactionManager
11.4. Modeling JDBC operations as Java objects
11.4.1. SqlQuery
11.4.2. MappingSqlQuery
11.4.3. SqlUpdate
11.4.4. StoredProcedure
11.4.5. SqlFunction
12. Data Access using O/R Mappers
12.1. Introduction
12.2. Hibernate
12.2.1. Resource management
12.2.2. SessionFactory setup in a Spring application context
12.2.3. Inversion of Control: HibernateTemplate and HibernateCallback
12.2.4. Implementing Spring-based DAOs without callbacks
12.2.5. Implementing DAOs based on plain Hibernate3 API
12.2.6. Programmatic transaction demarcation
12.2.7. Declarative transaction demarcation
12.2.8. Transaction management strategies
12.2.9. Container resources versus local resources
12.2.10. Spurious AppServer warnings about the transaction or DataSource no longer being active
12.3. JDO
12.3.1. PersistenceManagerFactory setup
12.3.2. JdoTemplate and JdoDaoSupport
12.3.3. Implementing DAOs based on plain JDO API
12.3.4. Transaction management
12.3.5. JdoDialect
12.4. Oracle TopLink
12.4.1. SessionFactory abstraction
12.4.2. TopLinkTemplate and TopLinkDaoSupport
12.4.3. Implementing DAOs based on plain TopLink API
12.4.4. Transaction management
12.5. Apache OJB
12.5.1. OJB setup in a Spring environment
12.5.2. PersistenceBrokerTemplate and PersistenceBrokerDaoSupport
12.5.3. Transaction management
12.6. iBATIS SQL Maps
12.6.1. Overview and differences between iBATIS 1.x and 2.x
12.6.2. iBATIS SQL Maps 1.x
12.6.2.1. Setting up the SqlMap
12.6.2.2. Using SqlMapTemplate and SqlMapDaoSupport
12.6.3. iBATIS SQL Maps 2.x
12.6.3.1. Setting up the SqlMapClient
12.6.3.2. Using SqlMapClientTemplate and SqlMapClientDaoSupport
12.6.3.3. Implementing DAOs based on plain iBATIS API
13. Web MVC framework
13.1. Introduction to the web MVC framework
13.1.1. Pluggability of other MVC implementations
13.1.2. Features of Spring MVC
13.2. The DispatcherServlet
13.3. Controllers
13.3.1. AbstractController and WebContentGenerator
13.3.2. Other simple controllers
13.3.3. The MultiActionController
13.3.4. CommandControllers
13.4. Handler mappings
13.4.1. BeanNameUrlHandlerMapping
13.4.2. SimpleUrlHandlerMapping
13.4.3. Adding HandlerInterceptors
13.5. Views and resolving them
13.5.1. ViewResolvers
13.5.2. Chaining ViewResolvers
13.5.3. Redirecting to views
13.5.3.1. RedirectView
13.5.3.2. The redirect: prefix
13.5.3.3. The forward: prefix
13.6. Using locales
13.6.1. AcceptHeaderLocaleResolver
13.6.2. CookieLocaleResolver
13.6.3. SessionLocaleResolver
13.6.4. LocaleChangeInterceptor
13.7. Using themes
13.7.1. Introduction
13.7.2. Defining themes
13.7.3. Theme resolvers
13.8. Spring's multipart (fileupload) support
13.8.1. Introduction
13.8.2. Using the MultipartResolver
13.8.3. Handling a fileupload in a form
13.9. Handling exceptions
14. Integrating view technologies
14.1. Introduction
14.2. JSP & JSTL
14.2.1. View resolvers
14.2.2. 'Plain-old' JSPs versus JSTL
14.2.3. Additional tags facilitating development
14.3. Tiles
14.3.1. Dependencies
14.3.2. How to integrate Tiles
14.3.2.1. InternalResourceViewResolver
14.3.2.2. ResourceBundleViewResolver
14.4. Velocity & FreeMarker
14.4.1. Dependencies
14.4.2. Context configuration
14.4.3. Creating templates
14.4.4. Advanced configuration
14.4.4.1. velocity.properties
14.4.4.2. FreeMarker
14.4.5. Bind support and form handling
14.4.5.1. the bind macros
14.4.5.2. simple binding
14.4.5.3. form input generation macros
14.4.5.4. Overriding HTML escaping and making tags XHTML compliant
14.5. XSLT
14.5.1. My First Words
14.5.1.1. Bean definitions
14.5.1.2. Standard MVC controller code
14.5.1.3. Convert the model data to XML
14.5.1.4. Defining the view properties
14.5.1.5. Document transformation
14.5.2. Summary
14.6. Document views (PDF/Excel)
14.6.1. Introduction
14.6.2. Configuration and setup
14.6.2.1. Document view definitions
14.6.2.2. Controller code
14.6.2.3. Subclassing for Excel views
14.6.2.4. Subclassing for PDF views
14.7. JasperReports
14.7.1. Dependencies
14.7.2. Configuration
14.7.2.1. Configuring the ViewResolver
14.7.2.2. Configuring the Views
14.7.2.3. About Report Files
14.7.2.4. Using JasperReportsMultiFormatView
14.7.3. Populating the ModelAndView
14.7.4. Working with Sub-Reports
14.7.4.1. Configuring Sub-Report Files
14.7.4.2. Configuring Sub-Report Data Sources
14.7.5. Configuring Exporter Parameters
15. Integrating with other web frameworks
15.1. Introduction
15.2. JavaServer Faces
15.2.1. DelegatingVariableResolver
15.2.2. FacesContextUtils
15.3. Struts
15.3.1. ContextLoaderPlugin
15.3.1.1. DelegatingRequestProcessor
15.3.1.2. DelegatingActionProxy
15.3.2. ActionSupport Classes
15.4. Tapestry
15.4.1. Architecture
15.4.2. Implementation
15.4.2.1. Sample application context
15.4.2.2. Obtaining beans in Tapestry pages
15.4.2.3. Exposing the application context to Tapestry
15.4.2.4. Component definition files
15.4.2.5. Adding abstract accessors
15.4.3. Summary
15.5. WebWork
16. Remoting and web services using Spring
16.1. Introduction
16.2. Exposing services using RMI
16.2.1. Exporting the service using the RmiServiceExporter
16.2.2. Linking in the service at the client
16.3. Using Hessian or Burlap to remotely call services via HTTP
16.3.1. Wiring up the DispatcherServlet for Hessian
16.3.2. Exposing your beans by using the HessianServiceExporter
16.3.3. Linking in the service on the client
16.3.4. Using Burlap
16.3.5. Applying HTTP basic authentication to a service exposed through Hessian or Burlap
16.4. Exposing services using HTTP invokers
16.4.1. Exposing the service object
16.4.2. Linking in the service at the client
16.5. Web Services
16.5.1. Exposing services using JAX-RPC
16.5.2. Accessing Web Services
16.5.3. Register Bean Mappings
16.5.4. Registering our own Handler
16.5.5. Exposing web services using XFire
16.6. Auto-detection is not implemented for remote interfaces
16.7. Considerations when choosing a technology
17. Accessing and implementing EJBs
17.1. Accessing EJBs
17.1.1. Concepts
17.1.2. Accessing local SLSBs
17.1.3. Accessing remote SLSBs
17.2. Using Spring convenience EJB implementation classes
18. JMS
18.1. Introduction
18.2. Domain unification
18.3. JmsTemplate
18.3.1. ConnectionFactory
18.3.2. Transaction Management
18.3.3. Destination Management
18.4. Using the JmsTemplate
18.4.1. Sending a message
18.4.2. Synchronous Receiving
18.4.3. Using Message Converters
18.4.4. SessionCallback and ProducerCallback
19. JMX Support
19.1. Introduction
19.2. Exporting your Beans to JMX
19.2.1. Creating an MBeanServer
19.2.2. Lazy-Initialized MBeans
19.2.3. Automatic Registration of MBeans
19.3. Controlling the Management Interface of Your Beans
19.3.1. The MBeanInfoAssembler Interface
19.3.2. Using Source-Level Metadata
19.3.3. Using JDK 5.0 Annotations
19.3.4. Source-Level Metadata Types
19.3.5. The AutodetectCapableMBeanInfoAssembler Interface
19.3.6. Defining Management Interfaces using Java Interfaces
19.3.7. Using MethodNameBasedMBeanInfoAssembler
19.4. Controlling the ObjectNames for your Beans
19.4.1. Reading ObjectNames from Properties
19.4.2. Using the MetadataNamingStrategy
19.5. Exporting your Beans with JSR-160 Connectors
19.5.1. Server-side Connectors
19.5.2. Client-side Connectors
19.5.3. JMX over Burlap/Hessian/SOAP
19.6. Accessing MBeans via Proxies
20. JCA CCI
20.1. Introduction
20.2. Configuring CCI
20.2.1. Connector configuration
20.2.2. ConnectionFactory configuration in Spring
20.2.3. Configuring CCI connections
20.2.4. Using a single CCI connection
20.3. Using Spring's CCI access support
20.3.1. Record conversion
20.3.2. CciTemplate
20.3.3. DAO support
20.3.4. Automatic output record generation
20.3.5. Summary
20.3.6. Using a CCI Connection and Interaction directly
20.3.7. Example for CciTemplate usage
20.4. Modeling CCI access as operation objects
20.4.1. MappingRecordOperation
20.4.2. MappingCommAreaOperation
20.4.3. Automatic output record generation
20.4.4. Summary
20.4.5. Example for MappingRecordOperation usage
20.4.6. Example for MappingCommAreaOperation usage
20.5. Transactions
21. Sending Email with Spring mail abstraction layer
21.1. Introduction
21.2. Spring mail abstraction structure
21.3. Using Spring mail abstraction
21.3.1. Pluggable MailSender implementations
21.4. Using the JavaMail MimeMessageHelper
21.4.1. Creating a simple MimeMessage and sending it
21.4.2. Sending attachments and inline resources
22. Scheduling jobs using Quartz or Timer
22.1. Introduction
22.2. Using the OpenSymphony Quartz Scheduler
22.2.1. Using the JobDetailBean
22.2.2. Using the MethodInvokingJobDetailFactoryBean
22.2.3. Wiring up jobs using triggers and the SchedulerFactoryBean
22.3. Using JDK Timer support
22.3.1. Creating custom timers
22.3.2. Using the MethodInvokingTimerTaskFactoryBean
22.3.3. Wrapping up: setting up the tasks using the TimerFactoryBean
23. Testing
23.1. Unit testing
23.2. Integration testing
23.2.1. Context management and caching
23.2.2. Dependency Injection of test class instances
23.2.3. Transaction management
23.2.4. Convenience variables
23.2.5. Example
23.2.6. Running integration tests
A. spring-beans.dtd

Preface

Developing software applications is hard enough even with good tools and technologies. Implementing applications using platforms which promise everything but turn out to be heavy-weight, hard to control and not very efficient during the development cycle makes it even harder. Spring provides a light-weight solution for building enterprise-ready applications, while still supporting the possibility of using declarative transaction management, remote access to your logic using RMI or webservices, mailing facilities and various options in persisting your data to a database. Spring provides an MVC framework, transparent ways of integrating AOP into your software and a well-structured exception hierarchy including automatic mapping from proprietary exception hierarchies.

Spring could potentially be a one-stop-shop for all your enterprise applications, however, Spring is modular, allowing you to use parts of it, without having to bring in the rest. You can use the bean container, with Struts on top, but you could also choose to just use the Hibernate integration or the JDBC abstraction layer. Spring is non-intrusive, meaning dependencies on the framework are generally none or absolutely minimal, depending on the area of use..

This document provides a reference guide to Spring's features. Since this document is still a work-in-progress, if you have any requests or comments, please post them on the user mailing list or on the forum at the SourceForge project page: http://www.sf.net/projects/springframework

Before we go on, a few words of gratitude: Chris Bauer (of the Hibernate team) prepared and adapted the DocBook-XSL software in order to be able to create Hibernate's reference guide, also allowing us to create this one. Also thanks to Russell Healy for doing an extensive and valuable review of some of the material.

Chapter 1. Introduction

1.1. Overview

Spring contains a lot of functionality and features, which are well-organized in seven modules shown in the diagram below. This section discusses each the of modules in turn.

Overview of the the Spring Framework

The Core package is the most fundamental part of the framework and provides the Dependency Injection features allowing you to manage bean container functionality. The basic concept here is the BeanFactory, which provides a factory pattern removing the need for programmatic singletons and allowing you to decouple the configuration and specification of dependencies from your actual program logic.

On top of the Core package sits the Context package, providing a way to access beans in a framework-style manner, somewhat resembling a JNDI-registry. The context package inherits its features from the beans package and adds support for text messaging using e.g. resource bundles, event-propagation, resource-loading and transparent creation of contexts by, for example, a servlet container.

The DAO package provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old java objects).

The ORM package provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis. Using the ORM package you can use all those O/R-mappers in combination with all the other features Spring offers, like simple declarative transaction management mentioned before.

Spring's AOP package provides an AOP Alliance compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code implementing functionality that should logically speaking be separated. Using source-level metadata functionality you can incorporate all kinds of behavioral information into your code, a little like .NET attributes.

Spring's Web package provides basic web-oriented integration features, such as multipart functionality, initialization of contexts using servlet listeners and a web-oriented application context. When using Spring together with WebWork or Struts, this is the package to integrate with.

Spring's Web MVC package provides a Model-View-Controller implementation for web-applications. Spring's MVC implementation is not just any implementation, it provides a clean separation between domain model code and web forms and allows you to use all the other features of the Spring Framework like validation.

1.2. Usage scenarios

With the building blocks described above you can use Spring in all sorts of scenarios, from applets up to fully-fledged enterprise applications using Spring's transaction management functionality and Web framework.

Typical full-fledged Spring web application

A typical web application using most of Spring's features. Using TransactionProxyFactoryBeans the web application is fully transactional, just as it would be when using container managed transaction as provided by Enterprise JavaBeans. All your custom business logic can be implemented using simple POJOs, managed by Spring's Dependency Injection container. Additional services such as sending email and validation, independent of the web layer enable you to choose where to execute validation rules. Spring's ORM support is integrated with Hibernate, JDO and iBatis. Using for example HibernateDaoSupport, you can re-use your existing Hibernate mappings. Form controllers seamlessly integrate the web-layer with the domain model, removing the need for ActionForms or other classes that transform HTTP parameters to values for your domain model.

Spring middle-tier using a third-party web framework

Sometimes the current circumstances do not allow you to completely switch to a different framework. Spring does not force you to use everything within it; it's not an all-or-nothing solution. Existing frontends using WebWork, Struts, Tapestry, or other UI frameworks can be integrated perfectly well with a Spring-based middle-tier, allowing you to use the transaction features that Spring offers. The only thing you need to do is wire up your business logic using an ApplicationContext and integrate your Web UI layer using a WebApplicationContext.

Remoting usage scenario

When you need to access existing code via webservices, you can use Spring's Hessian-, Burlap-, Rmi- or JaxRpcProxyFactory classes. Enabling remote access to existing application is all of a sudden not that hard anymore.

EJBs - Wrapping existing POJOs

Spring also provides an access layer and abstraction layer for Enterprise JavaBeans, enabling you to reuse your existing POJOs and wrap them in Stateless Session Beans, for use in scalable failsafe web applications, that might need declarative security.

Chapter 2. Background information

2.1. Inversion of Control / Dependency Injection

In early 2004, Martin Fowler asked the readers of his site: when talking about Inversion of Control: "the question, is what aspect of control are they inverting?". After talking about the term Inversion of Control Martin suggests renaming the pattern, or at least giving it a more self-explanatory name, and starts to use the term Dependency Injection. His article continues to explain some of the ideas behind Inversion of Control or Dependency Injection. If you need a decent insight: http://martinfowler.com/articles/injection.html.

Chapter 3. Beans, BeanFactory and the ApplicationContext

3.1. Introduction

Two of the most fundamental and important packages in Spring are the org.springframework.beans and org.springframework.context packages. Code in these packages provides the basis for Spring's Inversion of Control (alternately called Dependency Injection) features. The BeanFactory provides an advanced configuration mechanism capable of managing beans (objects) of any nature, using potentially any kind of storage facility. The ApplicationContext builds on top of the BeanFactory (it's a subclass) and adds other functionality such as easier integration with Springs AOP features, message resource handling (for use in internationalization), event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and application-layer specific contexts such as the WebApplicationContext, among other enhancements.

In short, the BeanFactory provides the configuration framework and basic functionality, while the ApplicationContext adds enhanced capabilities to it, some of them perhaps more J2EE and enterprise-centric. In general, an ApplicationContext is a complete superset of a BeanFactory, and any description of BeanFactory capabilities and behavior should be considered to apply to ApplicationContexts as well.

Users are sometimes unsure whether a BeanFactory or an ApplicationContext are best suited for use in a particular situation. Normally when building most applications in a J2EE-environment, the best option is to use the ApplicationContext, since it offers all the features of the BeanFactory and adds on to it in terms of features, while also allowing a more declarative approach to use of some functionality, which is generally desirable. The main usage scenario when you might prefer to use the BeanFactory is when memory usage is the greatest concern (such as in an applet where every last kilobyte counts), and you don't need all the features of the ApplicationContext.

This chapter covers material related to both the BeanFactory and the ApplicationContext. When mention is made only of the BeanFactory, you may always assume the text also applies to the ApplicationContext. When functionality is only available in the ApplicationContext, explicit mention is made of this.

3.2. BeanFactory and BeanDefinitions - the basics

3.2.1. The BeanFactory

The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. These dependencies are reflected in the configuration data used by the BeanFactory (although some dependencies may not be visible as configuration data, but rather be a function of programmatic interactions between beans at runtime).

A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory. (This should be qualified with the reminder that ApplicationContexts are a subclass of BeanFactory, and most users end up using XML variants of ApplicationContext).

Although for most scenarios, almost all user code managed by the BeanFactory does not have to be aware of the BeanFactory, the BeanFactory does have to be instantiated somehow. This can happen via explicit user code such as:

Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;

Note: once you have learned the basics about bean factories and applicaiton contexts, from this chapter, it will also be useful to learn about Spring's Resource abstraction, as described in Chapter 4, Abstracting Access to Low-Level Resources. The location path or paths supplied to an ApplicationContext constructor are actually resource strings, and in simple form are treated appropriately to the specific context implementation (i.e. ClassPathXmlApplicationContext treats a simple location path as a classpath location), but may also be used with special prefixes to force loading of definitions from the classpath or a URL, regardless of the actual context type. Another special prefix, classpath*:, allows all context definiton files of the same name on the classpath to be found and combined to build a context. Please see the chapter referenced above for much more information on the topic of Resources.

For many usage scenarios, user code will not have to instantiate the BeanFactory or ApplicationContext, since Spring Framework code will do it. For example, the web layer provides support code to load a Spring ApplicationContext automatically as part of the normal startup process of a J2EE web-app. This declarative process is described here:

While programmatic manipulation of BeanFactories will be described later, the following sections will concentrate on describing the configuration of BeanFactories.

A BeanFactory configuration consists of, at its most basic level, definitions of one or more beans that the BeanFactory must manage. In an XmlBeanFactory, these are configured as one or more bean elements inside a top-level beans element.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  
  <bean id="..." class="...">
    ...
  </bean>
  <bean id="..." class="...">
    ...
  </bean>

  ...

</beans>

3.2.2. The BeanDefinition

Bean definitions inside a DefaultListableBeanFactory variant (like XmlBeanFactory) are represented as BeanDefinition objects, which contain (among other information) the following details:

  • a class name: this is normally the actual implementation class of the bean being described in the bean definition. However, if the bean is to be constructed by calling a static factory method instead of using a normal constructor, this will actually be the class name of the factory class.

  • bean behavioral configuration elements, which state how the bean should behave in the container (i.e. prototype or singleton, autowiring mode, dependency checking mode, initialization and destruction methods)

  • constructor arguments and property values to set in the newly created bean. An example would be the number of connections to use in a bean that manages a connection pool (either specified as a property or as a constructor argument), or the pool size limit.

  • other beans a bean needs to do its work, i.e. collaborators (also specified as properties or as constructor arguments). These can also be called dependencies.

The concepts listed above directly translate to a set of elements the bean definition consists of. Some of these element groups are listed below, along with a link to further documentation about each of them.


Note that a bean definition is represented by the real interface org.springframework.beans.factory.config.BeanDefinition, and its various implementations (Root/ChildBeanDefinition). However, it is rare that user code works directly with BeanDefinition objects: Usually, bean definitions will be expressed in a metadata format (such as XML), which will be loaded on startup. The internal representation of such bean definitions are BeanDefinition objects in the factory.

Besides bean definitions which contain information on how to create a specific bean, a BeanFactory can also allow to register existing bean objects that have been created outside the factory (by custom code). DefaultListableBeanFactory supports this through the registerSingleton method, as defined by the org.springframework.beans.factory.config.ConfigurableBeanFactory interface. Typical applications solely work with beans defined through metadata bean definitions, though.

3.2.3. The bean class

The class attribute is normally mandatory (see Section 3.2.3.3, “Bean creation via instance factory method” and Section 3.5, “Abstract and child bean definitions” for the two exception) and is used for one of two purposes. In the much more common case where the BeanFactory itself directly creates the bean by calling its constructor (equivalent to Java code calling new), the class attribute specifies the class of the bean to be constructed. In the less common case where the BeanFactory calls a static, so-called factory method on a class to create the bean, the class attribute specifies the actual class containing the static factory method. (the type of the returned bean from the static factory method may be the same class or another class entirely, it doesn't matter).

3.2.3.1. Bean creation via constructor

When creating a bean using the constructor approach, all normal classes are usable by Spring and compatible with Spring. That is, the class being created does not need to implement any specific interfaces or be coded in a specific fashion. Just specifying the bean class should be enough. However, depending on what type of IoC you are going to use for that specific bean, you may need a default (empty) constructor.

Additionally, the BeanFactory isn't limited to just managing true JavaBeans, it is also able to manage virtually any class you want it to manage. Most people using Spring prefer to have actual JavaBeans (having just a default (no-argument) constructor and appropriate setters and getters modeled after the properties) in the BeanFactory, but it it's also possible to have more exotic non-bean-style classes in your BeanFactory. If, for example, you need to use a legacy connection pool that absolutely does not adhere to the JavaBean specification, no worries, Spring can manage it as well.

Using the XmlBeanFactory you can specify your bean class as follows:

<bean id="exampleBean"
      class="examples.ExampleBean"/>
<bean name="anotherExample"
      class="examples.ExampleBeanTwo"/> 

The mechanism for supplying (optional) arguments to the constructor, or setting properties of the object instance after it has been constructed, will be described shortly.

3.2.3.2. Bean creation via static factory method

When defining a bean which is to be created using a static factory method, along with the class attribute which specifies the class containing the static factory method, another attribute named factory-method is needed to specify the name of the factory method itself. Spring expects to be able to call this method (with an optional list of arguments as described later) and get back a live object, which from that point on is treated as if it had been created normally via a constructor. One use for such a bean definition is to call static factories in legacy code.

Following is an example of a bean definition which specifies that the bean is to be created by calling a factory-method. Note that the definition does not specify the type (class) of the returned object, only the class containing the factory method. In this example, createInstance must be a static method.

<bean id="exampleBean"
      class="examples.ExampleBean2"
      factory-method="createInstance"/>

The mechanism for supplying (optional) arguments to the factory method, or setting properties of the object instance after it has been returned from the factory, will be described shortly.

3.2.3.3. Bean creation via instance factory method

Quite similar to using a static factory method to create a bean, is the use of an instance (non-static) factory method, where a factory method of an existing bean from the factory is called to create the new bean.

To use this mechanism, the class attribute must be left empty, and the factory-bean attribute must specify the name of a bean in the current or an ancestor bean factory which contains the factory method. The factory method itself should still be set via the factory-method attribute.

Following is an example:

<!-- The factory bean, which contains a method called
     createInstance -->
<bean id="myFactoryBean"
      class="...">
  ...
</bean>
<!-- The bean to be created via the factory bean -->
<bean id="exampleBean"
      factory-bean="myFactoryBean"
      factory-method="createInstance"/>

Although the mechanisms for setting bean properties are still to be discussed, one implication of this approach is that the factory bean itself can be managed and configured via Dependency Injection, by the container.

3.2.4. The bean identifiers (id and name)

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the BeanFactory or ApplicationContext the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.

In an XmlBeanFactory (including ApplicationContext variants), you use the id or name attributes to specify the bean id(s), and at least one id must be specified in one or both of these attributes. The id attribute allows you to specify one id, and as it is marked in the XML DTD (definition document) as a real XML element ID attribute, the parser is able to do some extra validation when other elements point back to this one. As such, it is the preferred way to specify a bean id. However, the XML spec does limit the characters which are legal in XML IDs. This is usually not really a constraint, but if you have a need to use one of these characters, or want to introduce other aliases to the bean, you may also or instead specify one or more bean ids (separated by a comma (,) or semicolon (;) via the name attribute.

3.2.5. To singleton or not to singleton

Beans are defined to be deployed in one of two modes: singleton or non-singleton. (The latter is also called a prototype, although the term is used loosely as it doesn't quite fit). When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned.

The non-singleton, prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done. This is ideal for situations where for example each user needs an independent user object or something similar.

Beans are deployed in singleton mode by default, unless you specify otherwise. Keep in mind that by changing the type to non-singleton (prototype), each request for a bean will result in a newly created bean and this might not be what you actually want. So only change the mode to prototype when absolutely necessary.

In the example below, two beans are declared of which one is defined as a singleton, and the other one is a non-singleton (prototype). exampleBean is created each and every time a client asks the BeanFactory for this bean, while yetAnotherExample is only created once; a reference to the exact same instance is returned on each request for this bean.

<bean id="exampleBean"
      class="examples.ExampleBean" singleton="false"/>
<bean name="yetAnotherExample"
      class="examples.ExampleBeanTwo" singleton="true"/>

Note: when deploying a bean in the prototype mode, the lifecycle of the bean changes slightly. By definition, Spring cannot manage the complete lifecycle of a non-singleton/prototype bean, since after it is created, it is given to the client and the container does not keep track of it at all any longer. You can think of Spring's role when talking about a non-singleton/prototype bean as a replacement for the 'new' operator. Any lifecycle aspects past that point have to be handled by the client. The lifecycle of a bean in the BeanFactory is further described in Section 3.4.1, “Lifecycle interfaces”.

3.3. Properties, collaborators, autowiring and dependency checking

3.3.1. Setting bean properties and collaborators

Inversion of Control has already been referred to as Dependency Injection. The basic principle is that beans define their dependencies (i.e. the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally the inverse (hence the name Inversion of Control) of the bean instantiating or locating its dependencies on its own using direct construction of classes, or something like the Service Locator pattern. While we will not elaborate too much on the advantages of Dependency Injection, it becomes evident upon usage that code gets much cleaner and reaching a higher grade of decoupling is much easier when beans do not look up their dependencies, but are provided with them, and additionally do not even know where the dependencies are located and of what actual type they are.

As touched on in the previous paragraph, Inversion of Control/Dependency Injection exists in two major variants:

  • setter-based dependency injection is realized by calling setters on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. Beans defined in the BeanFactory that use setter-based dependency injection are true JavaBeans. Spring generally advocates usage of setter-based dependency injection, since a large number of constructor arguments can get unwieldy, especially when some properties are optional.

  • constructor-based dependency injection is realized by invoking a constructor with a number of arguments, each representing a collaborator or property. Additionally, calling a static factory method with specific arguments, to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to a static factory method similarly. Although Spring generally advocates usage of setter-based dependency injection for most situations, it does fully support the constructor-based approach as well, since you may wish to use it with pre-existing beans which provide only multi-argument constructors, and no setters. Additionally, for simpler beans, some people prefer the constructor approach as a means of ensuring beans cannot be constructed in an invalid state.

The BeanFactory supports both of these variants for injecting dependencies into beans it manages. (It in fact also supports injecting setter-based dependencies after some dependencies have already been supplied via the constructor approach.) The configuration for the dependencies comes in the form of a BeanDefinition, which is used together with JavaBeans PropertyEditors to know how to convert properties from one format to another. The actual values being passed around are done in the form of PropertyValue objects. However, most users of Spring will not be dealing with these classes directly (i.e. programmatically), but rather with an XML definition file which will be converted internally into instances of these classes, and used to load an entire BeanFactory or ApplicationContext.

Bean dependency resolution generally happens as follows:

  1. The BeanFactory is created and initialized with a configuration which describes all the beans. Most Spring users use a BeanFactory or ApplicationContext variant which supports XML format configuration files.

  2. Each bean has dependencies expressed in the form of properties, constructor arguments, or arguments to the static-factory method when that is used instead of a normal constructor. These dependencies will be provided to the bean, when the bean is actually created.

  3. Each property or constructor-arg is either an actual definition of the value to set, or a reference to another bean in the BeanFactory. In the case of the ApplicationContext, the reference can be to a bean in a parent ApplicationContext.

  4. Each property or constructor argument which is a value must be able to be converted from whatever format it was specified in, to the actual type of that property or constructor argument. By default Spring can convert a value supplied in string format to all built-in types, such as int, long, String, boolean, etc. Additionally, when talking about the XML based BeanFactory variants (including the ApplicationContext variants), these have built-in support for defining Lists, Maps, Sets, and Properties collection types. Additionally, Spring uses JavaBeans PropertyEditor definitions to be able to convert string values to other, arbitrary types. (You can provide the BeanFactory with your own PropertyEditor definitions to be able to convert your own custom types; more information about PropertyEditors and how to manually add custom ones, can be found in Section 3.9, “Registering additional custom PropertyEditors”). When a bean property is a Java Class type, Spring allows you to specify the value for that property as a string value which is the name of the class, and the ClassEditor PropertyEditor, which is built-in, will take care of converting that class name to an actual Class instance.

  5. It is important to realize that Spring validates the configuration of each bean in the BeanFactory when the BeanFactory is created, including the validation that properties which are bean references are actually referring to valid beans (i.e. the beans being referred to are also defined in the BeanFactory, or in the case of ApplicationContext, a parent context). However, the bean properties themselves are not set until the bean is actually created. For beans which are singleton and set to be pre-instantiated (such as singleton beans in an ApplicationContext), creation happens at the time that the BeanFactory is created, but otherwise this is only when the bean is requested. When a bean actually has to be created, this will potentially cause a graph of other beans to be created, as its dependencies and its dependencies' dependencies (and so on) are created and assigned.

  6. You can generally trust Spring to do the right thing. It will pick up configuration issues, including references to non-existent beans and circular dependencies, at BeanFactory load-time. It will actually set properties and resolve dependencies (i.e. create those dependencies if needed) as late as possible, which is when the bean is actually created. This does mean that a BeanFactory which has loaded correctly, can later generate an exception when you request a bean, if there is a problem creating that bean or one of its dependencies. This could happen if the bean throws an exception as a result of a missing or invalid property, for example. This potentially delayed visibility of some configuration issues is why ApplicationContext by default pre-instantiates singleton beans. At the cost of some upfront time and memory to create these beans before they are actually needed, you find out about configuration issues when the ApplicationContext is created, not later. If you wish, you can still override this default behavior and set any of these singleton beans to lazy-load (not be pre-instantiated).

Some examples:

First, an example of using the BeanFactory for setter-based dependency injection. Below is a small part of an XmlBeanFactory configuration file specifying some bean definitions. Following is the code for the actual main bean itself, showing the appropriate setters declared.

<bean id="exampleBean" class="examples.ExampleBean">
  <property name="beanOne"><ref bean="anotherExampleBean"/></property>
  <property name="beanTwo"><ref bean="yetAnotherBean"/></property>
  <property name="integerProperty"><value>1</value></property>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {
    
    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;
    
    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }
    
    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }
    
    public void setIntegerProperty(int i) {
        this.i = i;
    }    
}

As you can see, setters have been declared to match against the properties specified in the XML file. (The properties from the XML file, directly relate to the PropertyValues object from the RootBeanDefinition)

Now, an example of using the BeanFactory for IoC type 3 (constructor-based dependency injection). Below is a snippet from an XML configuration that specifies constructor arguments and the actual bean code, showing the constructor:

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
  <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
  <constructor-arg type="int"><value>1</value></constructor-arg>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;
    
    public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

As you can see, the constructor arguments specified in the bean definition will be used to pass in as arguments to the constructor of the ExampleBean.

Now consider a variant of this where instead of using a constructor, Spring is told to call a static factory method to return an instance of the object.:

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
  <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
  <constructor-arg><value>1</value></constructor-arg>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    ...

    // a private constructor
    private ExampleBean(...) {
      ...
    }
    
    // a static factory method
    // the arguments to this method can be considered the dependencies of the bean that
    // is returned, regardless of how those arguments are actually used.
    public static ExampleBean createInstance(
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        ExampleBean eb = new ExampleBean(...);
        // some other operations
        ...
        return eb;
    }
}

Note that arguments to the static factory method are supplied via constructor-arg elements, exactly the same as if a constructor had actually been used. These arguments are optional. Also, it is important to realize that the type of the class being returned by the factory method does not have to be of the same type as the class which contains the static factory method, although in this example it is. An instance (non-static) factory method, mentioned previously, would be used in an essentially identical fashion (aside from the use of the factory-bean attribute instead of the class attribute), so will not be detailed here.

3.3.2. Constructor Argument Resolution

Constructor argument resolution matching occurs using the argument's type. When another bean is referenced, the type is known, and matching can occur. When a simple type is used, such as <value>true<value>, Spring cannot determine the type of the value, and so cannot match by type without help. Consider the following class, which is used for the following two sections:

package examples;

public class ExampleBean {

    private int years;             //No. of years to the calculate the Ultimate Answer
    private String ultimateAnswer; //The Answer to Life, the Universe, and Everything

    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }
}

3.3.2.1. Constructor Argument Type Matching

The above scenario can use type matching with simple types by explicitly specifying the type of the constructor argument using the type attribute. For example:

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg type="int"><value>7500000</value></constructor-arg>
  <constructor-arg type="java.lang.String"><value>42</value></constructor-arg>
</bean> 

3.3.2.2. Constructor Argument Index

Constructor arguments can have their index specified explicitly by use of the index attribute. For example:

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg index="0"><value>7500000</value></constructor-arg>
  <constructor-arg index="1"><value>42</value></constructor-arg>
</bean> 

As well as solving the ambiguity problem of multiple simple values, specifying an index also solves the problem of ambiguity where a constructor may have two arguments of the same type. Note that the index is 0 based.

Specifying a constructor argument index is the preferred way of performing constructor IoC.

3.3.3. Bean properties and constructor arguments detailed

As mentioned in the previous section, bean properties and constructor arguments can be defined as either references to other managed beans (collaborators), or values defined inline. The XmlBeanFactory supports a number of sub-element types within its property and constructor-arg elements for this purpose.

3.3.3.1. The value element

The value element specifies a property or constructor argument as a human-readable string representation. As mentioned in detail previously, JavaBeans PropertyEditors are used to convert these string values from a java.lang.String to the actual property or argument type.

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <!-- results in a setDriverClassName(String) call -->
  <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
    <value>jdbc:mysql://localhost:3306/mydb</value>
  </property>
  <property name="username">
    <value>root</value>
  </property>
</bean>

3.3.3.2. The null element

The null element is used to handle null values. Spring treats empty arguments for properties and the like as empty Strings. The following XmlBeanFactory configuration:

<bean class="ExampleBean">
  <property name="email"><value></value></property>
</bean>        

results in the email property being set to "", equivalent to the java code: exampleBean.setEmail(""). The special <null> element may be used to indicate a null value, so that:

<bean class="ExampleBean">
  <property name="email"><null/></property>
</bean>        

is equivalent to the java code: exampleBean.setEmail(null).

3.3.3.3. The collection elements

The list, set, map, and props elements allow properties and arguments of Java type List, Set, Map, and Properties, respectively, to be defined and set.

<bean id="moreComplexObject" class="example.ComplexObject">
  <!-- results in a setPeople(java.util.Properties) call -->
  <property name="people">
    <props>
      <prop key="HarryPotter">The magic property</prop>
      <prop key="JerrySeinfeld">The funny property</prop>
    </props>
  </property>
  <!-- results in a setSomeList(java.util.List) call -->
  <property name="someList">
    <list>
      <value>a list element followed by a reference</value>
      <ref bean="myDataSource"/>
    </list>
  </property>
  <!-- results in a setSomeMap(java.util.Map) call -->
  <property name="someMap">
    <map>
      <entry>
        <key><value>yup an entry</value></key>
        <value>just some string</value>
      </entry>
      <entry>
        <key><value>yup a ref</value></key>
        <ref bean="myDataSource"/>
      </entry>
    </map>
  </property>
  <!-- results in a setSomeSet(java.util.Set) call -->
  <property name="someSet">
    <set>
      <value>just some string</value>
      <ref bean="myDataSource"/>
    </set>
  </property>
</bean>

Note that the value of a map key or value, or a set value, can also again be any of the elements:

(bean | ref | idref | list | set | map | props | value | null)

3.3.3.4. Inner bean definitions via nested bean elements

A bean element inside the property element is used to define a bean value inline, instead of referring to a bean defined elsewhere in the BeanFactory. The inline bean definition does not need to have any id defined.

<bean id="outer" class="...">
  <!-- Instead of using a reference to target, just use an inner bean -->
  <property name="target">
    <bean class="com.mycompany.PersonImpl">
      <property name="name"><value>Tony</value></property>
      <property name="age"><value>51</value></property>
    </bean>
  </property>
</bean>

Note that the singleton flag and any id attribute are effectively ignored. Inner beans are anonymous prototypes.

3.3.3.5. The idref element

An idref element is simply a shorthand and error-proof way to set a property to the String id or name of another bean in the container.

<bean id="theTargetBean" class="..."/>

<bean id="theClientBean" class="...">
  <property name="targetName">
    <idref bean="theTargetBean"/>
  </property>
</bean>

This is exactly equivalent at runtime to the following fragment:

<bean id="theTargetBean" class="...">
</bean>

<bean id="theClientBean" class="...">
  <property name="targetName">
    <value>theTargetBean</value>
  </property>
</bean>

The main reason the first form is preferable to the second is that using the idref tag will allow Spring to validate at deployment time that the other bean actually exists. In the second variation, the class who's targetName property is forced to do its own validation, and that will only happen when that class is actually instantiated by Spring, possibly long after the container is actually deployed.

Additionally, if the bean being referred to is in the same actual XML file, and the bean name is the bean id, the local attribute may be used, which will allow the XML parser itself to validate the bean name even earlier, at XML document parse time.

  <property name="targetName">
    <idref local="theTargetBean"/>
  </property>

3.3.3.6. The ref element

The ref element is the final element allowed inside a property definition element. It is used to set the value of the specified property to be a reference to another bean managed by the container, a collaborator, so to speak. As mentioned in a previous section, the referred-to bean is considered to be a dependency of the bean who's property is being set, and will be initialized on demand as needed (if it is a singleton bean it may have already been initialized by the container) before the property is set. All references are ultimately just a reference to another object, but there are 3 variations on how the id/name of the other object may be specified, which determines how scoping and validation is handled.

Specifying the target bean by using the bean attribute of the ref tag is the most general form, and will allow creating a reference to any bean in the same BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. The value of the bean attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean.

  <ref bean="someBean"/>

Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.

  <ref local="someBean"/>

Specifying the target bean by using the parent attribute allows a reference to be created to a bean which is in a parent BeanFactory (or ApplicationContext) of the current BeanFactory (or ApplicationContext). The value of the parent attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean, and the target bean must be in a parent BeanFactory or ApplicationContext to the current one. The main use of this bean reference variant is when there is a need to wrap an existing bean in a parent context with some sort of proxy (which may have the same name as the parent), and needs the original object so it may wrap it.

  <ref parent="someBean"/>

3.3.3.7. Value and Ref shortcut forms

It is so common to need to configure a value or a bean reference, that there exist some shortcut forms which are less verbose than using the full value and ref elements. The property, constructor-arg, and entry elements all support a value attribute which may be used instead of embedding a full value element. Therefore, the following:

<property name="myProperty">
  <value>hello</value>
</property
<constructor-arg>
  <value>hello</value>
</constructor-arg>
<entry key="myKey">
  <value>hello</value>
</entry>

are equivalent to:

<property name="myProperty" value="hello"/>
<constructor-arg value="hello"/>
<entry key="myKey" value="hello"/>

In general, when typing definitions by hand, you will probably prefer to use the less verbose shortcut form.

The property and constructor-arg elements support a similar shortcut ref attribute which may be used instead of a full nested ref element. Therefore, the following:

<property name="myProperty">
  <ref bean="myBean">
</property
<constructor-arg>
  <ref bean="myBean">
</constructor-arg>

are equivalent to:

<property name="myProperty" ref="myBean"/>
<constructor-arg ref="myBean"/>

Note however that the shortcut form is equivalent to a <ref bean="xxx"> element, there is no shortcut for <ref local="xxx">. To enforce a strict local ref, you must use the long form.

Finally, the entry element allows a shortcut form to specify the key and/or value of the map, in the form of the key / key-ref and value / value-ref attributes. Therefore, the following:

<entry>
  <key><ref bean="myKeyBean"/></key>
  <ref bean="myValueBean"/>
</entry>

is equivalent to:

<entry key-ref="myKeyBean" value-ref="myValueBean"/>

Again, the shortcut form is equivalent to a <ref bean="xxx"> element; there is no shortcut for <ref local="xxx">.

3.3.3.8. Compound property names

Note that compound or nested property names are perfectly legal when setting bean properties, as long as all components of the path except the final property name are non-null. For example, in this bean definition:

<bean id="foo" class="foo.Bar">
  <property name="fred.bob.sammy" value="123"/>
</bean>

the foo bean has a fred property which has a bob property, which has a sammy property, and that final sammy property is being set to a scalar value of 123. In order for this to work, the fred property of foo, and the bob property of fred must both be non-null after the bean is constructed, or a null-pointer exception will be thrown.

3.3.4. Method Injection

For most users, the majority of the beans in the container will be singletons. When a singleton bean needs to collaborate with (use) another singleton bean, or a non-singleton bean needs to collaborate with another non-singleton bean, the typical and common approach of handling this dependency by defining one bean to be a property of the other, is quite adequate. There is however a problem when the bean lifecycles are different. Consider a singleton bean A which needs to use a non-singleton (prototype) bean B, perhaps on each method invocation on A. The container will only create the singleton bean A once, and thus only get the opportunity to set its properties once. There is no opportunity for the container to provide bean A with a new instance of bean B every time one is needed.

One solution to this problem is to forgo some inversion of control. Bean A can be aware of the container (as described here) by implementing BeanFactoryAware, and use programmatic means (as described here) to ask the container via a getBean("B") call for (a new) bean B every time it needs it. This is generally not a desirable solution since the bean code is then aware of and coupled to Spring.

Method Injection, an advanced feature of the BeanFactory, allows this use case to be handled in a clean fashion, along with some other scenarios.

3.3.4.1. Lookup method Injection

Lookup method injection refers to the ability of the container to override abstract or concrete methods on managed beans in the container, to return the result of looking up another named bean in the container. The lookup will typically be of a non-singleton bean as per the scenario described above (although it can also be a singleton). Spring implements this th