(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()
public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }
BeanClassLoaderAware#setBeanClassLoader(ClassLoader classLoader)
public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }
BeanFactoryAware#setBeanFactory(BeanFactory factory)
public class MyBean implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) { System.out.println("BeanFactory Set!"); } }
<置> Set the embedded value parser EnvironmentAware#setEnvironment(Environment environment)
(
public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }
<置> 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
public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }
BeanPostProcessor#postProcessAfterInitialization()
.
)
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction()
Allows to clean up before the destroy
DisposableBean
If bean is implemented, destroy()
will be called.
DisposableBean
destroy()
public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }
call before bean destroyed . @PreDestroy
DisposableBean
<定> Custom public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }
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!