Home >Java >javaTutorial >spring-: spring-bean-lifecycle-execution-order
This article comprehensively analyzes the life cycle of Spring Bean, which covers all related life cycle interfaces and methods covering the BeanFactory interface to fully present the operating process of Bean.
(instantiated to destruction)
Spring from the
configuration file (@Configuration), XML file or component scan (@Component)BeanDefinitionRegistry#registerBeanDefinition()
The constructive function is instantiated
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation()
(.
This happened before before the injection BeanNameAware#setBeanName(String name)
.
BeanNameAware
in the context of the application. setBeanName()
<code class="language-java">public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }</code>If bean
BeanClassLoaderAware#setBeanClassLoader(ClassLoader classLoader)
<code class="language-java">public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }</code>by programming.
BeanFactoryAware#setBeanFactory(BeanFactory factory)
<code class="language-java">public class MyBean implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) { System.out.println("BeanFactory Set!"); } }</code>
Set the embedded value parser EnvironmentAware#setEnvironment(Environment environment)
(
<code class="language-java">public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }</code>
Set the resource loader () ResourceLoaderAware#setResourceLoader(ResourceLoader loader)
)
ApplicationEventPublisherAware#setApplicationEventPublisher(ApplicationEventPublisher publisher)
If bean needs
ContextRefreshedEvent
If bean needs Internationalization (I18N) to support MessageSourceAware#setMessageSource(MessageSource source)
, Spring will inject MessageSource.
If bean needs to access the entire Spring ApplicationContext , this method will be called. ApplicationContextAware#setApplicationContext(ApplicationContext ctx)
, used to access the Web context information . ServletContextAware#setServletContext(ServletContext ctx)
modified bean.
BeanPostProcessor#postProcessBeforeInitialization()
Custom
InitializingBean
afterPropertiesSet()
@PostConstruct
@Bean
) init-method
<code class="language-java">public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }</code>
BeanPostProcessor#postProcessAfterInitialization()
.
)
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction()
Allows to clean up before the destroy
DisposableBean
If bean is implemented, destroy()
will be called.
DisposableBean
destroy()
<code class="language-java">public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }</code>
call before bean destroyed . @PreDestroy
DisposableBean
Custom <code class="language-java">public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }</code>
destroy-method
@Bean
Three, the complete process summary (final order)
instantiated bean ()
(new Bean()
(setBeanName()
(BeanNameAware
) setBeanClassLoader()
) BeanClassLoaderAware
setBeanFactory()
BeanFactoryAware
(setEnvironment()
(EnvironmentAware
) setEmbeddedValueResolver()
) EmbeddedValueResolverAware
setResourceLoader()
ResourceLoaderAware
/ setApplicationEventPublisher()
(ApplicationEventPublisherAware
) setMessageSource()
MessageSourceAware
setApplicationContext()
? Bean's destruction stage ApplicationContextAware
setServletContext()
(ServletContextAware
) postProcessBeforeInitialization()
) BeanPostProcessor
@PostConstruct
afterPropertiesSet()
postProcessAfterInitialization()
More information about interface and methods, please visit: BeanPostProcessor
https://www.php.cn/link/6759d0996526ddc8e27aa550F0B806B1 The above is the detailed content of spring-: spring-bean-lifecycle-execution-order. For more information, please follow other related articles on the PHP Chinese website!