本文全面解析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中文網其他相關文章!