整套初始化方法及其标准顺序如下:

    • BeanNameAware’s setBeanName

    在创建这个bean的bean工厂中设置bean的名称。在填充普通bean属性之后但在初始化回调(如InitializingBean)之前调用。AfterPropertieSet()或自定义初始化方法。

    • BeanClassLoaderAware’s setBeanClassLoader

    将bean类装入器提供给bean实例的回调。在普通bean属性填充之后但在初始化回调之前调用,例如 InitializingBean的InitializingBean。AfterPropertieSet()方法或自定义初始化方法。

    • BeanFactoryAware’s setBeanFactory

    为bean实例提供所属工厂的回调。在普通bean属性填充之后但在初始化回调(如InitializingBean)之前调用。AfterPropertieSet()或自定义初始化方法。

    • EnvironmentAware’s setEnvironment

    设置此组件运行的环境。

    • EmbeddedValueResolverAware’s setEmbeddedValueResolver

    将StringValueResolver设置为用于解析嵌入定义值。

    • ResourceLoaderAware’s setResourceLoader (only applicable when running in an application context)
    • ApplicationEventPublisherAware’s setApplicationEventPublisher (only applicable when running in an application context)
    • MessageSourceAware’s setMessageSource (only applicable when running in an application context)
    • ApplicationContextAware’s setApplicationContext (only applicable when running in an application context)

    设置运行此对象的ApplicationContext。通常,此调用将用于初始化对象。在填充普通bean属性之后,但在初始化回调(org.springframework.beans.factory.InitializingBean.afterPropertiesSet() )或自定义 init-method。在ResourceLoaderWare之后调用。setResourceLoader,ApplicationEventPublisherAware。设置ApplicationEventPublisher和MessageSourceAware(如果适用)。

    • ServletContextAware’s setServletContext (only applicable when running in a web application context)
    • postProcessBeforeInitialization methods of BeanPostProcessors

    在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义init方法)之前,将此BeanPostProcessor应用于给定的新bean实例。bean将已经填充了属性值。返回的bean实例可能是原始实例的包装器。
    默认实现按原样返回给定的bean。

    • InitializingBean’s afterPropertiesSet

    由包含BeanFactory的程序在设置了所有bean属性并满足BeanFactoryAware、ApplicationContextAware等条件后调用。此方法允许bean实例在设置了所有bean属性后,对其整体配置和最终初始化进行验证。

    • a custom init-method definition
    • postProcessAfterInitialization methods of BeanPostProcessors

    在任何bean初始化回调(如InitializingBean的AfterPropertieSet或自定义init方法)之后,将此BeanPostProcessor应用于给定的新bean实例。bean将已经填充了属性值。返回的bean实例可能是原始实例的包装器。对于FactoryBean,将为FactoryBean实例和FactoryBean创建的对象调用此回调(从Spring 2.0开始)。后处理器可以通过FactoryBean检查的相应bean实例来决定是应用于FactoryBean还是应用于已创建的对象,还是同时应用于两者。此后处理回调也将在后处理回调后被触发。postProcessBeforeInstantiation方法,与所有其他BeanPostProcessor回调不同。
    默认实现按原样返回给定的bean。

    • On shutdown of a bean factory, the following lifecycle methods apply:
      • postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
      • DisposableBean’s destroy
      • a custom destroy-method definition