Reputation: 1359
Supossing a bean that implements all the Aware interfaces in https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/Aware.html
Do those interfaces get called always in a particular order in the life cycle?
The interfaces I'm talking about are: ApplicationContextAware, ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, BootstrapContextAware, EmbeddedValueResolverAware, EnvironmentAware, ImportAware, LoadTimeWeaverAware, MessageSourceAware, NotificationPublisherAware, ResourceLoaderAware, SchedulerContextAware, ServletConfigAware and ServletContextAware.
Upvotes: 0
Views: 465
Reputation: 8739
See 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
You'll notice that's not the full list. Some (i.e. ImportAware
) get dynamically added to the list of PostProcessor
s based on how their parent Configuration contexts are imported. That being the case, you can assume they are at the end of the list, and if you need more specifics, you can trace those in the debugger.
Upvotes: 2