|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Interface to be implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. An independent instance of any of these objects can be obtained (the Prototype design pattern), or a single shared instance can be obtained (a superior alternative to the Singleton design pattern). Which type of instance will be returned depends on the bean factory configuration - the API is the same. The Singleton approach is much more useful and more common in practice.
The point of this approach is that the BeanFactory is a central registry of application components, and centralizes the configuring 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.
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, to either Singletons or Prototypes.
In contrast to the methods in ListableBeanFactory, all of the methods 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 is asked. Beans in this factory instance are supposed to override beans of the same name in any parent factory.
Bean factories are supposed to support the standard bean lifecycle interfaces
as far as possible. The maximum set of initialization methods and their standard
order is:
1. BeanNameAware's setBeanName
2. BeanFactoryAware's setBeanFactory
3. ApplicationContextAware's setApplicationContext (only applicable if running
in an application context)
4. postProcessBeforeInitialization methods of BeanPostProcessors
5. InitializingBean's afterPropertiesSet
6. a custom init-method definition
7. postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
1. DisposableBean's destroy
2. a custom destroy-method definition
BeanNameAware.setBeanName(java.lang.String)
,
BeanFactoryAware.setBeanFactory(org.springframework.beans.factory.BeanFactory)
,
InitializingBean.afterPropertiesSet()
,
DisposableBean.destroy()
,
BeanPostProcessor.postProcessBeforeInitialization(java.lang.Object, java.lang.String)
,
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)
,
RootBeanDefinition.getInitMethodName()
,
RootBeanDefinition.getDestroyMethodName()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
Method Summary | |
boolean |
containsBean(java.lang.String name)
Does this bean factory contain a bean with the given name? |
java.lang.String[] |
getAliases(java.lang.String name)
Return the aliases for the given bean name, if defined. |
java.lang.Object |
getBean(java.lang.String name)
Return an instance (possibly shared or independent) of the given bean name. |
java.lang.Object |
getBean(java.lang.String name,
java.lang.Class requiredType)
Return an instance (possibly shared or independent) of the given bean name. |
boolean |
isSingleton(java.lang.String name)
Is this bean a singleton? That is, will getBean() always return the same object? |
Method Detail |
public java.lang.Object getBean(java.lang.String name) throws BeansException
Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.
Will ask the parent factory if the bean cannot be found in this factory instance.
name
- name of the bean to return
NoSuchBeanDefinitionException
- if there's no such bean definition
BeansException
- if the bean could not be createdpublic java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType) throws BeansException
Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.
Will ask the parent factory if the bean cannot be found in this factory instance.
name
- name of the bean to returnrequiredType
- type the bean may match. Can be an interface or superclass
of the actual class. For example, if the value is Object.class, this method will
succeed whatever the class of the returned instance.
BeanNotOfRequiredTypeException
- if the bean is not of the required type
NoSuchBeanDefinitionException
- if there's no such bean definition
BeansException
- if the bean could not be createdpublic boolean containsBean(java.lang.String name)
Will ask the parent factory if the bean cannot be found in this factory instance.
name
- name of the bean to query
public boolean isSingleton(java.lang.String name) throws NoSuchBeanDefinitionException
Will ask the parent factory if the bean cannot be found in this factory instance.
name
- name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given namepublic java.lang.String[] getAliases(java.lang.String name) throws NoSuchBeanDefinitionException
Will ask the parent factory if the bean cannot be found in this factory instance.
name
- the bean name to check for aliases
NoSuchBeanDefinitionException
- if there's no such bean definition
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |