public interface BeanFactory
ListableBeanFactory and
ConfigurableBeanFactory
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
postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
destroy
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
FACTORY_BEAN_PREFIX
工厂Bean的前缀
Used to dereference a
FactoryBean instance and distinguish it from
beans created by the FactoryBean. |
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
containsBean(String name)
判断我们的容器中是否包含了当前的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对象
|
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.
|
static final String FACTORY_BEAN_PREFIX
FactoryBean instance and distinguish it from
beans created by the FactoryBean. For example, if the bean named
myJndiObject is a FactoryBean, getting &myJndiObject
will return the factory, not the instance returned by the factory.Object getBean(String name) throws BeansException
name - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeansException - if the bean could not be obtained<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException
name - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)requiredType - 去容器中获取Bean的class类型 可以是实现的接口和父类NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeanNotOfRequiredTypeException - 容器中没有指定class类型的beanBeansException - bean还没有被创建Object getBean(String name, Object... args) throws BeansException
name - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)args - 这个是用于指定传入获取bean的构造函数的参数,通过传入该参数,那么就可以明确的知道去调用哪个构造函数是实例化对象NoSuchBeanDefinitionException - 抛出的异常,容器中没有该bean的定义BeanDefinitionStoreException - 主要用于多例模式下的构造器创建????暂时没理解BeansException - bean还没有被创建<T> T getBean(Class<T> requiredType) throws BeansException
requiredType - class对象需要的类型NoSuchBeanDefinitionException - 没有对应class的bean定义NoUniqueBeanDefinitionException - 找到多个匹配的BeansException - 创建bean的实例ListableBeanFactory<T> T getBean(Class<T> requiredType, Object... args) throws BeansException
requiredType - 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 createdboolean containsBean(String name)
name - the name of the bean to queryboolean isSingleton(String name) throws NoSuchBeanDefinitionException
name - the name of the bean to queryNoSuchBeanDefinitionException - if there is no bean with the given namegetBean(java.lang.String),
isPrototype(java.lang.String)boolean isPrototype(String name) throws NoSuchBeanDefinitionException
name - the name of the bean to queryNoSuchBeanDefinitionException - if there is no bean with the given namegetBean(java.lang.String),
isSingleton(java.lang.String)boolean isTypeMatch(String name, org.springframework.core.ResolvableType typeToMatch) throws NoSuchBeanDefinitionException
name - 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 namegetBean(java.lang.String),
getType(java.lang.String)boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException
name - 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 namegetBean(java.lang.String),
getType(java.lang.String)@Nullable Class<?> getType(String name) throws NoSuchBeanDefinitionException
name - the name of the bean to querynull if not determinableNoSuchBeanDefinitionException - if there is no bean with the given namegetBean(java.lang.String),
isTypeMatch(java.lang.String, org.springframework.core.ResolvableType)String[] getAliases(String name)
Will ask the parent factory if the bean cannot be found in this factory instance.
name - the bean name to check for aliasesgetBean(java.lang.String)