BeanFactory 初始化方法及其标准顺序

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

源码摘抄

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 初始化方法及其标准顺序为:

  1. BeanNameAware.setBeanName
    1. 在创建此bean的 BeanFactory 中设置bean的名称。
    2. 在填充常规bean属性之后 但在 InitializingBean.afterPropertiesSet()类的init回调 或 自定义 init-method之前调用。
  2. BeanClassLoaderAware.setBeanClassLoader
    1. 允许bean知道 bean 的回调 ClassLoader class loader ;
    2. 也就是说,当前 bean 工厂使用的类加载器来加载bean类。
  3. BeanFactoryAware.setBeanFactory
    1. 将拥有的工厂提供给 Bean 实例的回调。
    2. 在填充常规 bean 属性之后但在初始化回调之前调用,例如 InitializingBean.afterPropertiesSet()自定义的 init-method
    3. bean 可以立即在工厂中调用方法。
  4. EnvironmentAware.setEnvironment
    1. 设置运行该组件的 环境
  5. EmbeddedValueResolverAware.setEmbeddedValueResolver
    1. 通过 ApplicationContextAware / BeanFactoryAware 接口,这可以替代完整的ConfigurableBeanFactory 依赖项。
    2. 设置 StringValueResolver 以用于解析嵌入式定义值。
  6. ResourceLoaderAware.setResourceLoader (仅在应用程序上下文中运行时适用)
    1. 设置运行该对象的 ResourceLoader
    2. 这可能是 ResourcePatternResolver,可以通过 `instanceof ResourcePatternResolver 进行检查。
    3. 另请参见 ResourcePatternUtils.getResourcePatternResolver 方法。
    4. 在填充正常的 bean 属性之后但在初始化回调之前调用,例如在 InitializingBean. afterPropertiesSet 或 自定义的init方法 上。
    5. ApplicationContextAware.setApplicationContext 之前调用。
  7. ApplicationEventPublisherAware.setApplicationEventPublisher (仅在应用程序上下文中运行时适用)
    1. 设置此对象在其中运行的 ApplicationEventPublisher
    2. 在填充正常的 bean 属性之后,但在 InitializingBean.afterPropertiesSet自定义init-method 之类的 init 回调之前调用。
    3. ApplicationContextAware.setApplicationContext 之前调用。
  8. MessageSourceAware.setMessageSource (仅适用于在应用程序上下文中运行)
    1. 设置此对象在其中运行的MessageSource。
    2. 在填充正常的 bean 属性之后,但在InitializingBean.afterPropertiesSet自定义init-method之类的 init 回调 之前调用。
    3. ApplicationContextAware.setApplicationContext之前调用。
  9. ApplicationContextAware.setApplicationContext(仅适用于在应用程序上下文中运行)
    1. 设置该对象在其中运行的ApplicationContext
    2. 通常,此调用将用于初始化该对象。
    3. 在填充正常的 bean 属性之后 ,但在org.springframework.beans.factory.InitializingBean.afterPropertiesSet()之类的init回调 或 自定义init-method之前调用。
    4. 如果适用,在ResourceLoaderAware.setResourceLoader , ApplicationEventPublisherAware.setApplicationEventPublisherMessageSourceAware之后调用。
  10. ServletContextAware.setServletContext (仅适用于在以下环境中运行Web应用程序上下文)
    1. 设置运行该对象的ServletContext
    2. 在填充正常的bean属性之后,但在nitializingBean. afterPropertiesSet 之类的init 回调 或 自定义init-method之前调用。
    3. ApplicationContextAware.setApplicationContext 之后调用。
  11. BeanPostProcessors.postProcessBeforeInitialization方法
    12.InitializingBean.afterPropertiesSet
    1. 由包含的BeanFactory 设置了所有 bean 属性并满足BeanFactoryAwareApplicationContextAware等之后调用。
    2. 此方法允许 bean 实例对其总体配置进行验证。
    3. 设置所有 bean 属性后进行最后的初始化。
  12. 自定义 init-method 定义 (a custom init-method definition)
  13. BeanPostProcessors.postProcessAfterInitialization

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值