Spring 感知接口顺序
Spring Aware interfaces order
假设一个 bean 实现了 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/Aware.html
中的所有 Aware 接口
这些接口在生命周期中是否总是以特定顺序被调用?
我说的接口是:ApplicationContextAware、ApplicationEventPublisherAware、BeanClassLoaderAware、BeanFactoryAware、BeanNameAware、BootstrapContextAware、EmbeddedValueResolverAware、EnvironmentAware、ImportAware、LoadTimeWeaverAware、MessageSourceAware、NotificationPublisherAware、ResourceLoaderAware、SchedulerContextAware、ServletConfigAware 和 ServletContext=ware。[=12=12 ]
参见the docs for BeanFactory
and the ApplicationContextAwareProcessor
:
Bean factory implementations should support the standard bean
lifecycle interfaces as far as possible. The full set of
initialization methods and their standard order is:
BeanNameAware's
setBeanName
BeanClassLoaderAware
's setBeanClassLoader
BeanFactoryAware
's setBeanFactory
EnvironmentAware
's setEnvironment
EmbeddedValueResolverAware
's setEmbeddedValueResolver
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)
ServletContextAware
's setServletContext
(only applicable when running in a web application context)
postProcessBeforeInitialization
methods of BeanPostProcessor
s
InitializingBean
's afterPropertiesSet
- a custom
init
-method definition
postProcessAfterInitialization
methods of BeanPostProcessors
您会注意到这不是完整列表。一些(即 ImportAware
)根据其父配置上下文的导入方式获得 dynamically added to the list of PostProcessor
s。在这种情况下,您可以假设它们位于列表的末尾,如果您需要更多细节,可以在调试器中跟踪它们。
假设一个 bean 实现了 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/Aware.html
中的所有 Aware 接口这些接口在生命周期中是否总是以特定顺序被调用?
我说的接口是:ApplicationContextAware、ApplicationEventPublisherAware、BeanClassLoaderAware、BeanFactoryAware、BeanNameAware、BootstrapContextAware、EmbeddedValueResolverAware、EnvironmentAware、ImportAware、LoadTimeWeaverAware、MessageSourceAware、NotificationPublisherAware、ResourceLoaderAware、SchedulerContextAware、ServletConfigAware 和 ServletContext=ware。[=12=12 ]
参见the docs for BeanFactory
and the ApplicationContextAwareProcessor
:
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
BeanNameAware's
setBeanName
BeanClassLoaderAware
's setBeanClassLoaderBeanFactoryAware
'ssetBeanFactory
EnvironmentAware
's setEnvironmentEmbeddedValueResolverAware
's setEmbeddedValueResolverResourceLoaderAware
's setResourceLoader (only applicable when running in an application context)ApplicationEventPublisherAware
'ssetApplicationEventPublisher
(only applicable when running in an application context)MessageSourceAware
'ssetMessageSource
(only applicable when running in an application context)ApplicationContextAware
'ssetApplicationContext
(only applicable when running in an application context)ServletContextAware
'ssetServletContext
(only applicable when running in a web application context)postProcessBeforeInitialization
methods ofBeanPostProcessor
sInitializingBean
's afterPropertiesSet- a custom
init
-method definitionpostProcessAfterInitialization
methods ofBeanPostProcessors
您会注意到这不是完整列表。一些(即 ImportAware
)根据其父配置上下文的导入方式获得 dynamically added to the list of PostProcessor
s。在这种情况下,您可以假设它们位于列表的末尾,如果您需要更多细节,可以在调试器中跟踪它们。