0
点赞
收藏
分享

微信扫一扫

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

两岁时就很帅 2021-09-30 阅读 77
日记本

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

BeanNameAware.setBeanName

在创建此bean的 BeanFactory 中设置bean的名称。

在填充常规bean属性之后 但在 InitializingBean.afterPropertiesSet()类的init回调 或 自定义 init-method之前调用。

BeanClassLoaderAware.setBeanClassLoader

允许bean知道 bean 的回调 ClassLoader class loader ;

也就是说,当前 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


举报

相关推荐

0 条评论