源码摘抄
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
/**
* 用于访问Spring bean容器的根接口。
*
* 这是bean容器的基本客户端视图。 其他接口,例如{@link ListableBeanFactory}和 {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} 可用于特定目的。
*
* 该接口由包含多个bean定义的对象实现,每个bean定义均由String名称唯一标识。根据bean的定义,工厂将返回一个所包含对象的独立实例(原型设计模式),或者返回一个共享实例(对于Singleton设计模式而言是一个更好的替代,其中实例是单例在工厂范围内)。将返回哪种类型的实例取决于bean工厂的配置:API是相同的。从Spring 2.0开始,根据具体的应用程序上下文,可以使用更多范围(例如,网络环境中的“ request”和“ session”范围)。
*
* 这种方法的重点是BeanFactory是应用程序组件的中央注册表,并且集中了应用程序组件的配置(例如,不再需要单个对象读取属性文件)。有关此方法的好处的讨论,请参见“一对一的J2EE专家设计和开发”的第4章和第11章。
*
* 请注意,通常最好依赖于依赖注入(“推送”配置)通过设置器或构造函数配置应用程序对象,而不是使用任何形式的“拉”配置(例如BeanFactory查找)。使用此BeanFactory接口及其子接口可以实现Spring的依赖注入功能。
*
* 通常,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>
*
* <p>On shutdown of a bean factory, the following lifecycle methods apply:
* <ol>
* <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
* <li>DisposableBean's {@code destroy}
* <li>a custom destroy-method definition
* </ol>
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @since 13 April 2001
* @see BeanNameAware#setBeanName
* @see BeanClassLoaderAware#setBeanClassLoader
* @see BeanFactoryAware#setBeanFactory
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher
* @see org.springframework.context.MessageSourceAware#setMessageSource
* @see org.springframework.context.ApplicationContextAware#setApplicationContext
* @see org.springframework.web.context.ServletContextAware#setServletContext
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
* @see InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization
* @see DisposableBean#destroy
* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName
*/
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
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;
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
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;
@Nullable
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
@Nullable
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);
}
BeanFactory 初始化方法及其标准顺序为:
BeanNameAware.setBeanName- 在创建此bean的
BeanFactory中设置bean的名称。 - 在填充常规bean属性之后 但在
InitializingBean.afterPropertiesSet()类的init回调 或自定义 init-method之前调用。
- 在创建此bean的
BeanClassLoaderAware.setBeanClassLoader- 允许bean知道 bean 的回调
ClassLoader class loader; - 也就是说,当前 bean 工厂使用的类加载器来加载bean类。
- 允许bean知道 bean 的回调
BeanFactoryAware.setBeanFactory- 将拥有的工厂提供给 Bean 实例的回调。
- 在填充常规 bean 属性之后但在初始化回调之前调用,例如
InitializingBean.afterPropertiesSet()或自定义的 init-method。 - bean 可以立即在工厂中调用方法。
EnvironmentAware.setEnvironment- 设置运行该组件的 环境
EmbeddedValueResolverAware.setEmbeddedValueResolver- 通过
ApplicationContextAware/BeanFactoryAware接口,这可以替代完整的ConfigurableBeanFactory依赖项。 - 设置
StringValueResolver以用于解析嵌入式定义值。
- 通过
ResourceLoaderAware.setResourceLoader(仅在应用程序上下文中运行时适用)- 设置运行该对象的
ResourceLoader。 - 这可能是
ResourcePatternResolver,可以通过 `instanceof ResourcePatternResolver 进行检查。 - 另请参见
ResourcePatternUtils.getResourcePatternResolver方法。 - 在填充正常的 bean 属性之后但在初始化回调之前调用,例如在 InitializingBean. afterPropertiesSet 或 自定义的init方法 上。
- 在
ApplicationContextAware.setApplicationContext之前调用。
- 设置运行该对象的
ApplicationEventPublisherAware.setApplicationEventPublisher(仅在应用程序上下文中运行时适用)- 设置此对象在其中运行的
ApplicationEventPublisher。 - 在填充正常的 bean 属性之后,但在
InitializingBean.afterPropertiesSet或自定义init-method之类的 init 回调之前调用。 - 在
ApplicationContextAware.setApplicationContext之前调用。
- 设置此对象在其中运行的
MessageSourceAware.setMessageSource(仅适用于在应用程序上下文中运行)- 设置此对象在其中运行的MessageSource。
- 在填充正常的 bean 属性之后,但在
InitializingBean.afterPropertiesSet或自定义init-method之类的 init 回调 之前调用。 - 在
ApplicationContextAware.setApplicationContext之前调用。
ApplicationContextAware.setApplicationContext(仅适用于在应用程序上下文中运行)- 设置该对象在其中运行的
ApplicationContext。 - 通常,此调用将用于初始化该对象。
- 在填充正常的 bean 属性之后 ,但在
org.springframework.beans.factory.InitializingBean.afterPropertiesSet()之类的init回调 或自定义init-method之前调用。 - 如果适用,在
ResourceLoaderAware.setResourceLoader,ApplicationEventPublisherAware.setApplicationEventPublisher和MessageSourceAware之后调用。
- 设置该对象在其中运行的
ServletContextAware.setServletContext(仅适用于在以下环境中运行Web应用程序上下文)- 设置运行该对象的
ServletContext。 - 在填充正常的bean属性之后,但在
nitializingBean. afterPropertiesSet之类的init 回调 或自定义init-method之前调用。 - 在
ApplicationContextAware.setApplicationContext之后调用。
- 设置运行该对象的
BeanPostProcessors.postProcessBeforeInitialization方法
12.InitializingBean.afterPropertiesSet- 由包含的
BeanFactory设置了所有 bean 属性并满足BeanFactoryAware,ApplicationContextAware等之后调用。 - 此方法允许 bean 实例对其总体配置进行验证。
- 设置所有 bean 属性后进行最后的初始化。
- 由包含的
自定义 init-method定义 (a custom init-method definition)BeanPostProcessors.postProcessAfterInitialization

本文介绍了Spring框架中的核心接口BeanFactory,它是应用程序组件的中央注册表,支持依赖注入,并概述了Bean的生命周期及其回调方法。
607

被折叠的 条评论
为什么被折叠?



