用于访问Spring Bean容器的顶级接口。这是Bean容器的基本客户端视图。更进一步的接口如:ListableBeanFactory,ConfigurableBeanFactory 可用于特殊目的。这个接口是由保存多个bean定义的对象实现的,每个对象都由字符串名称唯一标识。哪种类型的实例将返回取决于bean工厂配置:API是相同的。自Spring2.0以来,根据具体的应用程序上下文(例如,web环境中的“request”和“session”范围),还可以使用其他范围。BeanFactory是应用程序组件的中央注册中心,并且集中了应用程序组件的配置(例如,不再需要单个对象读取属性文件)。
注意,通常最好依赖注入通过设置器(set)或构造函数配置应用程序对象,而不是使用任何形式的“pull”配置,如BeanFactory查找。Spring的依赖注入功能是使用这个BeaS工厂接口及其子接口实现的。
通常,BeanFactory将加载存储在配置源(例如XML文档)中的bean定义,并使用{@code org.springframework.beans}包来配置bean。但是,一个实现可以简单地直接在Java代码中返回它创建的Java对象。对于如何存储定义没有限制:LDAP、RDBMS、XML、属性文件等。鼓励实现来支持bean之间的引用(依赖注入)。与{@link ListableBeanFactory}中的方法相比,该接口中的所有操作还将检查父工厂是否为{@link HierarchicalBeanFactory}。如果在该工厂实例中未找到bean,将立即调用父工厂。这个工厂实例中的bean应该在任何父工厂中重写相同名称的bean。Bean工厂实现应该尽可能支持标准的bean生命周期接口。完整的初始化方法和标准顺序是:
* <ol>
* <li>BeanNameAware's {@code setBeanName}
* <li>BeanClassLoaderAware's {@code setBeanClassLoader}
* <li>BeanFactoryAware's {@code setBeanFactory}
* <li>EnvironmentAware's {@code setEnvironment}
* <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
* <li>ResourceLoaderAware's {@code setResourceLoader}
* (only applicable when running in an application context)
* <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
* (only applicable when running in an application context)
* <li>MessageSourceAware's {@code setMessageSource}
* (only applicable when running in an application context)
* <li>ApplicationContextAware's {@code setApplicationContext}
* (only applicable when running in an application context)
* <li>ServletContextAware's {@code setServletContext}
* (only applicable when running in a web application context)
* <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
* <li>InitializingBean's {@code afterPropertiesSet}
* <li>a custom init-method definition
* <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors
* </ol>
在bean工厂关闭时,应用下列生命周期方法
* <ol>
* <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
* <li>DisposableBean's {@code destroy}
* <li>a custom destroy-method definition
* </ol>
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);