Aliuk
Aliuk

Reputation: 1359

Spring Aware interfaces order

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

Answers (1)

Dovmo
Dovmo

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 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 BeanPostProcessors
  • InitializingBean's afterPropertiesSet
  • a custom init-method definition
  • postProcessAfterInitialization methods of BeanPostProcessors

You'll notice that's not the full list. Some (i.e. ImportAware) get dynamically added to the list of PostProcessors 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

Related Questions