本文全面解析Spring Bean的生命周期,涵盖BeanFactory接口的所有相关生命周期接口及方法,完整呈现Bean的运行流程。
一、Spring Bean生命周期逻辑执行顺序 (从实例化到销毁)
加载Bean定义 (元数据处理)
BeanDefinitionRegistry#registerBeanDefinition()
Bean实例化 (对象创建)
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation()
设置Bean名称 (BeanNameAware#setBeanName(String name)
)
BeanNameAware
,Spring会调用setBeanName()
方法来告知Bean其自身的名称。<code class="language-java">public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }</code>
设置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>
设置BeanFactory (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>
设置环境 (EnvironmentAware#setEnvironment(Environment environment)
)
application.properties
中的值。<code class="language-java">public class MyBean implements EnvironmentAware { @Override public void setEnvironment(Environment environment) { System.out.println("Environment Set!"); } }</code>
设置嵌入式值解析器 (EmbeddedValueResolverAware#setEmbeddedValueResolver(StringValueResolver resolver)
)
<code class="language-java">public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean Name Set: " + name); } }</code>
设置资源加载器 (ResourceLoaderAware#setResourceLoader(ResourceLoader loader)
)
设置应用程序事件发布器 (ApplicationEventPublisherAware#setApplicationEventPublisher(ApplicationEventPublisher publisher)
)
ContextRefreshedEvent
。设置消息源 (MessageSourceAware#setMessageSource(MessageSource source)
)
设置ApplicationContext (ApplicationContextAware#setApplicationContext(ApplicationContext ctx)
)
设置ServletContext (ServletContextAware#setServletContext(ServletContext ctx)
)
初始化前的后处理 (BeanPostProcessor#postProcessBeforeInitialization()
)
自定义初始化
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()
)
Bean可供使用
当应用程序关闭时,Spring会优雅地销毁Bean。
销毁前的处理 (DestructionAwareBeanPostProcessor#postProcessBeforeDestruction()
)
DisposableBean
的 destroy()
方法
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>
自定义 @PreDestroy
方法 (首选方法)
DisposableBean
更现代,在Bean销毁之前调用。<code class="language-java">public class MyBean implements BeanClassLoaderAware { @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("ClassLoader Set!"); } }</code>
自定义 destroy-method
(如果在 @Bean
中定义)
三、完整流程总结 (最终顺序)
new Bean()
)setBeanName()
(BeanNameAware
)setBeanClassLoader()
(BeanClassLoaderAware
)setBeanFactory()
(BeanFactoryAware
)setEnvironment()
(EnvironmentAware
)setEmbeddedValueResolver()
(EmbeddedValueResolverAware
)setResourceLoader()
(ResourceLoaderAware
)setApplicationEventPublisher()
(ApplicationEventPublisherAware
)setMessageSource()
(MessageSourceAware
)setApplicationContext()
(ApplicationContextAware
)setServletContext()
(ServletContextAware
)postProcessBeforeInitialization()
(BeanPostProcessor
)@PostConstruct
/ afterPropertiesSet()
postProcessAfterInitialization()
(BeanPostProcessor
)postProcessBeforeDestruction()
(DestructionAwareBeanPostProcessor
)destroy()
(DisposableBean
)@PreDestroy
destroy-method
更多关于BeanFactory
接口及其方法的信息,请访问:https://www.php.cn/link/6759d0996526ddc8e27aa550f0b806b1
祝您编码愉快!?
以上是弹簧 - 春丝 - 剥离订单的详细内容。更多信息请关注PHP中文网其他相关文章!