|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.factory.support.StaticListableBeanFactory
public class StaticListableBeanFactory
Static factory that allows to register existing singleton instances programmatically. Does not have support for prototype beans and aliases.
Serves as example for a simple implementation of the ListableBeanFactory interface, managing existing bean instances rather than creating new ones based on bean definitions.
For a full-fledged bean factory based on bean definitions, have a look at DefaultListableBeanFactory.
DefaultListableBeanFactory
Field Summary |
---|
Fields inherited from interface org.springframework.beans.factory.BeanFactory |
---|
FACTORY_BEAN_PREFIX |
Constructor Summary | |
---|---|
StaticListableBeanFactory()
|
Method Summary | |
---|---|
void |
addBean(String name,
Object bean)
Add a new singleton bean. |
boolean |
containsBean(String name)
Does this bean factory contain a bean definition with the given name? |
boolean |
containsBeanDefinition(String name)
Check if this bean factory contains a bean definition with the given name. |
String[] |
getAliases(String name)
Return the aliases for the given bean name, if defined. |
Object |
getBean(String name)
Return an instance, which may be shared or independent, of the given bean name. |
Object |
getBean(String name,
Class requiredType)
Return an instance (possibly shared or independent) of the given bean name. |
int |
getBeanDefinitionCount()
Return the number of beans defined in the factory. |
String[] |
getBeanDefinitionNames()
Return the names of all beans defined in this factory. |
String[] |
getBeanDefinitionNames(Class type)
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. |
String[] |
getBeanNamesForType(Class type)
Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType
in the case of FactoryBeans. |
String[] |
getBeanNamesForType(Class type,
boolean includePrototypes,
boolean includeFactoryBeans)
Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType
in the case of FactoryBeans. |
Map |
getBeansOfType(Class type)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. |
Map |
getBeansOfType(Class type,
boolean includePrototypes,
boolean includeFactoryBeans)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. |
Class |
getType(String name)
Determine the type of the bean with the given name. |
boolean |
isSingleton(String name)
Is this bean a singleton? |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public StaticListableBeanFactory()
Method Detail |
---|
public void addBean(String name, Object bean)
name
- the name of the beanbean
- the bean instancepublic Object getBean(String name) throws BeansException
BeanFactory
Callers may retain references to returned objects in the case of Singleton beans.
This method delegates to the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to return
NoSuchBeanDefinitionException
- if there is no bean definition
with the specified name
BeansException
- if the bean could not be obtainedpublic Object getBean(String name, Class requiredType) throws BeansException
BeanFactory
Behaves the same as getBean(String), but provides a measure of type safety by throwing a Spring BeansException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).
getBean
in interface BeanFactory
name
- the name of the bean to returnrequiredType
- type the bean must match. Can be an interface or superclass
of the actual class, or null
for any match. For example, if the value is
Object.class, this method will succeed whatever the class of the returned instance.
null
)
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(String name)
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
containsBean
in interface BeanFactory
name
- the name of the bean to query
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
isSingleton
in interface BeanFactory
name
- the name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given namepublic Class getType(String name) throws NoSuchBeanDefinitionException
BeanFactory
getType
in interface BeanFactory
name
- the name of the bean to query
null
if not determinable
NoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
FactoryBean.getObjectType()
public String[] getAliases(String name)
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
getAliases
in interface BeanFactory
name
- the bean name to check for aliases
public boolean containsBeanDefinition(String name)
ListableBeanFactory
Does not consider any hierarchy this factory may participate in.
Use containsBean
to check ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
containsBeanDefinition
in interface ListableBeanFactory
name
- the name of the bean to look for
BeanFactory.containsBean(java.lang.String)
public int getBeanDefinitionCount()
ListableBeanFactory
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' countBeansIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionCount
in interface ListableBeanFactory
BeanFactoryUtils.countBeansIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)
public String[] getBeanDefinitionNames()
ListableBeanFactory
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory)
public String[] getBeanDefinitionNames(Class type)
ListableBeanFactory
Does not consider objects created by FactoryBeans but rather the
FactoryBean classes themselves, avoiding instantiation of any beans. Use
getBeanNamesForType
to match objects created by FactoryBeans.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean names
ListableBeanFactory.getBeanNamesForType(java.lang.Class)
,
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory, Class)
public String[] getBeanNamesForType(Class type)
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeanNamesForType matches all kinds of beans, be it
singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeanNamesOfType(type, true, true)
.
getBeanNamesForType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean names
FactoryBean.getObjectType()
,
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean includeFactoryBeans)
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans if the "includeFactoryBeans" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "includeFactoryBeans" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean). $ *
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
getBeanNamesForType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean namesincludePrototypes
- whether to include prototype beans too
or just singletons (also applies to FactoryBeans)includeFactoryBeans
- whether to include objects created by
FactoryBeans (or by factory methods with a "factory-bean" reference)
too, or just conventional beans. Note that FactoryBeans need to be
initialized to determine their type: So be aware that passing in "true"
for this flag will initialize FactoryBeans (and "factory-bean" references).
FactoryBean.getObjectType()
,
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
public Map getBeansOfType(Class type) throws BeansException
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beansOfTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeansOfType matches all kinds of beans, be it
singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeansOfType(type, true, true)
.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all concrete beans
BeansException
- if a bean could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans) throws BeansException
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans if the "includeFactoryBeans" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "includeFactoryBeans" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beansOfTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all concrete beansincludePrototypes
- whether to include prototype beans too
or just singletons (also applies to FactoryBeans)includeFactoryBeans
- whether to include objects created by
FactoryBeans (or by factory methods with a "factory-bean" reference)
too, or just conventional beans. Note that FactoryBeans need to be
initialized to determine their type: So be aware that passing in "true"
for this flag will initialize FactoryBeans (and "factory-bean" references).
BeansException
- if a bean could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |