This is the basic client view of a bean container;
further interfaces such as ListableBeanFactory and
org.
are available for specific purposes.
This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or a single shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance will be returned depends on the bean factory configuration: the API is the same. Since Spring 2.0, further scopes are available depending on the concrete application context (e.g. "request" and "session" scopes in a web environment).
The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files, for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and Development" for a discussion of the benefits of this approach.
Note that it is generally better to rely on Dependency Injection ("push" configuration) to configure application objects through setters or constructors, rather than use any form of "pull" configuration like a BeanFactory lookup. Spring's Dependency Injection functionality is implemented using this BeanFactory interface and its subinterfaces.
Normally a BeanFactory will load bean definitions stored in a configuration
source (such as an XML document), and use the org.springframework.beans
package to configure the beans. However, an implementation could simply return
Java objects it creates as necessary directly in Java code. There are no
constraints on how the definitions could be stored: LDAP, RDBMS, XML,
properties file, etc. Implementations are encouraged to support references
amongst beans (Dependency Injection).
In contrast to the methods in ListableBeanFactory, all of the
operations in this interface will also check parent factories if this is a
HierarchicalBeanFactory. If a bean is not found in this factory instance,
the immediate parent factory will be asked. Beans in this factory instance
are supposed to override beans of the same name in any parent factory.
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
setBeanName
setBeanClassLoader
setBeanFactory
setEnvironment
setEmbeddedValueResolver
setResourceLoader
(only applicable when running in an application context)
setApplicationEventPublisher
(only applicable when running in an application context)
setMessageSource
(only applicable when running in an application context)
setApplicationContext
(only applicable when running in an application context)
setServletContext
(only applicable when running in a web application context)
postProcessBeforeInitialization methods of BeanPostProcessors
afterPropertiesSet
init-method definition
postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
destroy
destroy-method definition
BeanNameAware#setBeanName, BeanClassLoaderAware#setBeanClassLoader, BeanFactoryAware#setBeanFactory, org.springframework.context.EnvironmentAware#setEnvironment , org.springframework.context.EmbeddedValueResolverAware#setEmbeddedValueResolver , org.springframework.context.ResourceLoaderAware#setResourceLoader , org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher , org.springframework.context.MessageSourceAware#setMessageSource , org.springframework.context.ApplicationContextAware#setApplicationContext , org.springframework.web.context.ServletContextAware#setServletContext , org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization , InitializingBean#afterPropertiesSet, org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName , org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization , org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor#postProcessBeforeDestruction , DisposableBean#destroy, org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName
| Modifier and Type | Field and Description |
|---|---|
| public static final String | FACTORY_BEAN_PREFIX
Used to dereference a |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: whether a bean with the given name is presentthe name of the bean to query name)Does this bean factory contain a bean definition or externally registered singleton instance with the given name? |
| public String[] | Returns: the aliases, or an empty array if nonethe bean name to check for aliases name)Return the aliases for the given bean name, if any. |
| public Object | Returns: an instance of the bean. Note that the return value will never benull but possibly a stub for
null returned from a factory method, to be checked via equals(null).
Consider using getBeanProvider(Class) for resolving optional dependencies.the name of the bean to retrieve name)Return an instance, which may be shared or independent, of the specified bean. |
| public <T> T | Returns: an instance of the bean. Note that the return value will never benull. In case of a stub for
null from a factory method having been resolved for the requested bean, a
BeanNotOfRequiredTypeException against the NullBean stub will be raised.
Consider using getBeanProvider(Class) for resolving optional dependencies.the name of the bean to retrieve name, Class<T> type the bean must match; can be an interface or superclass requiredType)Return an instance, which may be shared or independent, of the specified bean. |
| public Object | Returns: an instance of the beanthe name of the bean to retrieve name, Object... arguments to use when creating a bean instance using explicit arguments
(only applied when creating a new instance as opposed to retrieving an existing one) args)Return an instance, which may be shared or independent, of the specified bean. |
| public <T> T | |
| public <T> T | Returns: an instance of the beantype the bean must match; can be an interface or superclass requiredType, Object... arguments to use when creating a bean instance using explicit arguments
(only applied when creating a new instance as opposed to retrieving an existing one) args)Return an instance, which may be shared or independent, of the specified bean. |
| public <T> ObjectProvider | Returns: a corresponding provider handletype the bean must match; can be an interface or superclass requiredType)Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
| public <T> ObjectProvider | Returns: a corresponding provider handletype the bean must match; can be a generic type declaration requiredType)Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
| public Class | |
| public Class | |
| public boolean | Returns: whether this bean will always deliver independent instancesthe name of the bean to query name)Is this bean a prototype? |
| public boolean | Returns: whether this bean corresponds to a singleton instancethe name of the bean to query name)Is this bean a shared singleton? |
| public boolean | Returns: true if the bean type matches,
false if it doesn't match or cannot be determined yetthe name of the bean to query name, ResolvableType the type to match against (as a typeToMatch)ResolvableType)Check whether the bean with the given name matches the specified type. |
| public boolean | Returns: true if the bean type matches,
false if it doesn't match or cannot be determined yetthe name of the bean to query name, Class<?> the type to match against (as a typeToMatch)Class)Check whether the bean with the given name matches the specified type. |
| FACTORY_BEAN_PREFIX | back to summary |
|---|---|
| public static final String FACTORY_BEAN_PREFIX Used to dereference a | |
| containsBean | back to summary |
|---|---|
| public boolean containsBean(String name) Does this bean factory contain a bean definition or externally registered singleton instance with the given name? If the given name is an alias, it will be translated back to the corresponding canonical bean name. If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance. If a bean definition or singleton instance matching the given name is found,
this method will return
| |
| getAliases | back to summary |
|---|---|
| public String[] getAliases(String name) Return the aliases for the given bean name, if any. All of those aliases point to the same bean when used in a If the given name is an alias, the corresponding original bean name and other aliases (if any) will be returned, with the original bean name being the first element in the array. Will ask the parent factory if the bean cannot be found in this factory instance. | |
| getBean | back to summary |
|---|---|
| public Object getBean(String name) throws BeansException Return an instance, which may be shared or independent, of the specified bean. This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans. Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| getBean | back to summary |
|---|---|
| public <T> T getBean(String name, Class<T> requiredType) throws BeansException Return an instance, which may be shared or independent, of the specified bean. Behaves the same as Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| getBean | back to summary |
|---|---|
| public Object getBean(String name, Object... args) throws BeansException Return an instance, which may be shared or independent, of the specified bean. Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition. Note that the provided arguments need to match a specific candidate constructor / factory method in the order of declared parameters.
| |
| getBean | back to summary |
|---|---|
| public <T> T getBean(Class<T> requiredType) throws BeansException Return the bean instance that uniquely matches the given object type, if any. This method goes into
| |
| getBean | back to summary |
|---|---|
| public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException Return an instance, which may be shared or independent, of the specified bean. Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition. Note that the provided arguments need to match a specific candidate constructor / factory method in the order of declared parameters. This method goes into
| |
| getBeanProvider | back to summary |
|---|---|
| public <T> ObjectProvider Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. For matching a generic type, consider
| |
| getBeanProvider | back to summary |
|---|---|
| public <T> ObjectProvider Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. This variant allows for specifying a generic type to match, similar to reflective injection points with generic type declarations in method/constructor parameters. Note that collections of beans are not supported here, in contrast to reflective
injection points. For programmatically retrieving a list of beans matching a
specific type, specify the actual bean type as an argument here and subsequently
use Also, generics matching is strict here, as per the Java assignment rules.
For lenient fallback matching with unchecked semantics (similar to the ´unchecked´
Java compiler warning), consider calling
| |
| getType | back to summary |
|---|---|
| public Class Determine the type of the bean with the given name. More specifically,
determine the type of object that For a Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| getType | back to summary |
|---|---|
| public Class Determine the type of the bean with the given name. More specifically,
determine the type of object that For a Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| isPrototype | back to summary |
|---|---|
| public boolean isPrototype(String name) throws NoSuchBeanDefinitionException Is this bean a prototype? That is, will Note This method returning Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| isSingleton | back to summary |
|---|---|
| public boolean isSingleton(String name) throws NoSuchBeanDefinitionException Is this bean a shared singleton? That is, will Note This method returning Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| isTypeMatch | back to summary |
|---|---|
| public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException Check whether the bean with the given name matches the specified type.
More specifically, check whether a Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |
| isTypeMatch | back to summary |
|---|---|
| public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException Check whether the bean with the given name matches the specified type.
More specifically, check whether a Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
| |