简体   繁体   中英

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.

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

You'll notice that's not the full list. Some (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM