public class StaticListableBeanFactory extends Object implements ListableBeanFactory
BeanFactory implementation
which allows to register existing singleton instances programmatically.
Does not have support for prototype beans or 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, and not implementing any extended SPI interfaces (such as
ConfigurableBeanFactory).
For a full-fledged factory based on bean definitions, have a look
at DefaultListableBeanFactory.
DefaultListableBeanFactoryFACTORY_BEAN_PREFIX| 构造器和说明 |
|---|
StaticListableBeanFactory()
Create a regular
StaticListableBeanFactory, to be populated
with singleton bean instances through addBean(java.lang.String, java.lang.Object) calls. |
StaticListableBeanFactory(Map<String,Object> beans)
Create a
StaticListableBeanFactory wrapping the given Map. |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addBean(String name,
Object bean)
Add a new singleton bean.
|
boolean |
containsBean(String name)
判断我们的容器中是否包含了当前的bean对象
|
boolean |
containsBeanDefinition(String name)
查看是否包含指定名字的Bean
不支持向上或向下查找
不支持查找非配置文件定义的单例Bean
|
<A extends Annotation> |
findAnnotationOnBean(String beanName,
Class<A> annotationType)
在指定name对应的Bean上找指定的注解,如果没有找到的话,去指定Bean的父类或者父接口上查找
|
String[] |
getAliases(String name)
获取bean的所有的别名
Will ask the parent factory if the bean cannot be found in this factory instance.
|
<T> T |
getBean(Class<T> requiredType)
通过指定的bean的类型去容器中获取对象 若容器中有多个想同类型的bean
我们通过ctx.getBean(beanType.class) 就会抛出异常
|
<T> T |
getBean(Class<T> requiredType,
Object... args)
获取bean实例
|
Object |
getBean(String name)
通过bean 去容器中获取一个bean对象
|
<T> T |
getBean(String name,
Class<T> requiredType)
通过bean 去容器中获取一个bean对象
|
Object |
getBean(String name,
Object... args)
通过bean 去容器中获取一个bean对象
|
int |
getBeanDefinitionCount()
获取容器中的所有bean定义
|
String[] |
getBeanDefinitionNames()
查看此BeanFactory中包含的Bean数量
不支持向上或向下查找
不支持查找非配置文件定义的单例Bean
|
String[] |
getBeanNamesForAnnotation(Class<? extends Annotation> annotationType)
找到所有带有指定注解类型的Bean
|
String[] |
getBeanNamesForType(Class<?> type)
返回此BeanFactory中所有指定类型的Bean的名字。
|
String[] |
getBeanNamesForType(Class<?> type,
boolean includeNonSingletons,
boolean allowEagerInit)
返回此BeanFactory中所有指定类型(或指定类型的子类型)的Bean的名字。
|
String[] |
getBeanNamesForType(org.springframework.core.ResolvableType type)
返回此BeanFactory中所包含的所有Bean定义的名称
不支持向上或向下查找
不支持查找非配置文件定义的单例Bean
|
<T> Map<String,T> |
getBeansOfType(Class<T> type)
返回此BeanFactory中所有指定类型(或指定类型的子类型)的Bean的名字。
|
<T> Map<String,T> |
getBeansOfType(Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
返回此BeanFactory中所有指定类型(或指定类型的子类型)的Bean的名字。
|
Map<String,Object> |
getBeansWithAnnotation(Class<? extends Annotation> annotationType)
找到所有带有指定注解的Bean,返回一个以Bean的name为键,其对应的Bean实例为值的Map
|
Class<?> |
getType(String name)
通过beanName获取对应bean的class类型
|
boolean |
isPrototype(String name)
是不是原型的
|
boolean |
isSingleton(String name)
判断当前的bean是不是单例的
|
boolean |
isTypeMatch(String name,
Class<?> typeToMatch)
是不是匹配的类型
|
boolean |
isTypeMatch(String name,
org.springframework.core.ResolvableType typeToMatch)
是不是匹配的类型
Will ask the parent factory if the bean cannot be found in this factory instance.
|
public StaticListableBeanFactory()
StaticListableBeanFactory, to be populated
with singleton bean instances through addBean(java.lang.String, java.lang.Object) calls.public StaticListableBeanFactory(Map<String,Object> beans)
StaticListableBeanFactory wrapping the given Map.
Note that the given Map may be pre-populated with beans;
or new, still allowing for beans to be registered via addBean(java.lang.String, java.lang.Object);
or Collections.emptyMap() for a dummy factory which
enforces operating against an empty set of beans.
beans - a Map for holding this factory's beans, with the
bean name String as key and the corresponding singleton object as valuepublic void addBean(String name, Object bean)
name - the name of the beanbean - the bean instancepublic Object getBean(String name) throws BeansException
BeanFactorygetBean 在接口中 BeanFactoryname - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeansException - if the bean could not be obtainedpublic <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException
BeanFactorygetBean 在接口中 BeanFactoryname - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)requiredType - 去容器中获取Bean的class类型 可以是实现的接口和父类NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeanNotOfRequiredTypeException - 容器中没有指定class类型的beanBeansException - bean还没有被创建public Object getBean(String name, Object... args) throws BeansException
BeanFactorygetBean 在接口中 BeanFactoryname - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)args - 这个是用于指定传入获取bean的构造函数的参数,通过传入该参数,那么就可以明确的知道去调用哪个构造函数是实例化对象NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeanDefinitionStoreException - 主要用于多例模式下的构造器创建????暂时没理解BeansException - bean还没有被创建public <T> T getBean(Class<T> requiredType) throws BeansException
BeanFactorygetBean 在接口中 BeanFactoryrequiredType - class对象需要的类型NoSuchBeanDefinitionException - 没有对应class的bean定义NoUniqueBeanDefinitionException - 找到多个匹配的BeansException - 创建bean的实例ListableBeanFactorypublic <T> T getBean(Class<T> requiredType, Object... args) throws BeansException
BeanFactorygetBean 在接口中 BeanFactoryrequiredType - bean的类型args - 构造函数的参数的类型,在实例化的过程中不需要去推断构造函数了NoSuchBeanDefinitionException - if there is no such bean definitionBeanDefinitionStoreException - if arguments have been given but
the affected bean isn't a prototypeBeansException - if the bean could not be createdpublic boolean containsBean(String name)
BeanFactorycontainsBean 在接口中 BeanFactoryname - the name of the bean to querypublic boolean isSingleton(String name) throws NoSuchBeanDefinitionException
BeanFactoryisSingleton 在接口中 BeanFactoryname - the name of the bean to queryNoSuchBeanDefinitionException - if there is no bean with the given nameBeanFactory.getBean(java.lang.String),
BeanFactory.isPrototype(java.lang.String)public boolean isPrototype(String name) throws NoSuchBeanDefinitionException
BeanFactoryisPrototype 在接口中 BeanFactoryname - the name of the bean to queryNoSuchBeanDefinitionException - if there is no bean with the given nameBeanFactory.getBean(java.lang.String),
BeanFactory.isSingleton(java.lang.String)public boolean isTypeMatch(String name, org.springframework.core.ResolvableType typeToMatch) throws NoSuchBeanDefinitionException
BeanFactoryisTypeMatch 在接口中 BeanFactoryname - the name of the bean to querytypeToMatch - the type to match against (as a ResolvableType)true if the bean type matches,
false if it doesn't match or cannot be determined yetNoSuchBeanDefinitionException - if there is no bean with the given nameBeanFactory.getBean(java.lang.String),
BeanFactory.getType(java.lang.String)public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException
BeanFactoryisTypeMatch 在接口中 BeanFactoryname - the name of the bean to querytypeToMatch - the type to match against (as a Class)true if the bean type matches,
false if it doesn't match or cannot be determined yetNoSuchBeanDefinitionException - if there is no bean with the given nameBeanFactory.getBean(java.lang.String),
BeanFactory.getType(java.lang.String)public Class<?> getType(String name) throws NoSuchBeanDefinitionException
BeanFactorygetType 在接口中 BeanFactoryname - the name of the bean to querynull if not determinableNoSuchBeanDefinitionException - if there is no bean with the given nameBeanFactory.getBean(java.lang.String),
BeanFactory.isTypeMatch(java.lang.String, org.springframework.core.ResolvableType)public String[] getAliases(String name)
BeanFactoryWill ask the parent factory if the bean cannot be found in this factory instance.
getAliases 在接口中 BeanFactoryname - the bean name to check for aliasesBeanFactory.getBean(java.lang.String)public boolean containsBeanDefinition(String name)
ListableBeanFactorycontainsBeanDefinition 在接口中 ListableBeanFactorypublic int getBeanDefinitionCount()
ListableBeanFactorygetBeanDefinitionCount 在接口中 ListableBeanFactorypublic String[] getBeanDefinitionNames()
ListableBeanFactorygetBeanDefinitionNames 在接口中 ListableBeanFactorypublic String[] getBeanNamesForType(@Nullable org.springframework.core.ResolvableType type)
ListableBeanFactorygetBeanNamesForType 在接口中 ListableBeanFactorypublic String[] getBeanNamesForType(@Nullable Class<?> type)
ListableBeanFactorygetBeanNamesForType 在接口中 ListableBeanFactorypublic String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
ListableBeanFactorygetBeanNamesForType 在接口中 ListableBeanFactorypublic <T> Map<String,T> getBeansOfType(@Nullable Class<T> type) throws BeansException
ListableBeanFactorygetBeansOfType 在接口中 ListableBeanFactoryBeansExceptionpublic <T> Map<String,T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
ListableBeanFactorygetBeansOfType 在接口中 ListableBeanFactoryBeansExceptionpublic String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType)
ListableBeanFactorygetBeanNamesForAnnotation 在接口中 ListableBeanFactorypublic Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException
ListableBeanFactorygetBeansWithAnnotation 在接口中 ListableBeanFactoryBeansException@Nullable public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException
ListableBeanFactory