日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring】Bean的生命周期

發布時間:2025/3/21 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring】Bean的生命周期 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring Bean是Spring應用中最最重要的部分了。所以來看看Spring容器在初始化一個bean的時候會做那些事情,順序是怎樣的,在容器關閉的時候,又會做哪些事情。

示例代碼

git地址:

giraffe0813

giraffeInSpring

giraffeInSpring?, up-to-date

spring版本:4.2.3.RELEASE
鑒于Spring源碼是用gradle構建的,我也決定舍棄我大maven,嘗試下洪菊推薦過的gradle。運行beanLifeCycle模塊下的junit test即可在控制臺看到如下輸出,可以清楚了解Spring容器在創建,初始化和銷毀Bean的時候依次做了那些事情。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Spring容器初始化 ===================================== 調用GiraffeService無參構造函數 GiraffeService中利用set方法設置屬性值 調用setBeanName:: Bean Name defined in context=giraffeService 調用setBeanClassLoader,ClassLoader Name = sun.misc.Launcher$AppClassLoader 調用setBeanFactory,setBeanFactory:: giraffe bean singleton=true 調用setEnvironment 調用setResourceLoader:: Resource File Name=spring-beans.xml 調用setApplicationEventPublisher 調用setApplicationContext:: Bean Definition Names=[giraffeService, org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0, com.giraffe.spring.service.GiraffeServicePostProcessor#0] 執行BeanPostProcessor的postProcessBeforeInitialization方法,beanName=giraffeService 調用PostConstruct注解標注的方法 執行InitializingBean接口的afterPropertiesSet方法 執行配置的init-method 執行BeanPostProcessor的postProcessAfterInitialization方法,beanName=giraffeService Spring容器初始化完畢 ===================================== 從容器中獲取Bean giraffe Name=李光洙 ===================================== 調用preDestroy注解標注的方法 執行DisposableBean接口的destroy方法 執行配置的destroy-method Spring容器關閉

參考文檔

life cycle management of a spring bean
Spring Bean Life Cycle

Spring Bean的生命周期

先來看看,Spring在Bean從創建到銷毀的生命周期中可能做得事情。

initialization 和 destroy

有時我們需要在Bean屬性值set好之后和Bean銷毀之前做一些事情,比如檢查Bean中某個屬性是否被正常的設置好值了。Spring框架提供了多種方法讓我們可以在Spring Bean的生命周期中執行initialization和pre-destroy方法。

1.實現InitializingBean和DisposableBean接口

這兩個接口都只包含一個方法。通過實現InitializingBean接口的afterPropertiesSet()方法可以在Bean屬性值設置好之后做一些操作,實現DisposableBean接口的destroy()方法可以在銷毀Bean之前做一些操作。

如下:

1 2 3 4 5 6 7 8 9 10 11 12 publicclass GiraffeService implementsInitializingBean,DisposableBean { ????@Override ????publicvoid afterPropertiesSet() throwsException { ????????System.out.println("執行InitializingBean接口的afterPropertiesSet方法"); ????} ????@Override ????publicvoid destroy() throwsException { ????????System.out.println("執行DisposableBean接口的destroy方法"); ????} }

這種方法比較簡單,但是不建議使用。因為這樣會將Bean的實現和Spring框架耦合在一起。

2.在bean的配置文件中指定init-method和destroy-method方法

Spring允許我們創建自己的init方法和destroy方法,只要在Bean的配置文件中指定init-method和destroy-method的值就可以在Bean初始化時和銷毀之前執行一些操作。
如下:

1 2 3 4 5 6 7 8 9 10 11 12 publicclass GiraffeService { ????//通過<bean>的destroy-method屬性指定的銷毀方法 ????publicvoid destroyMethod() throwsException { ????????System.out.println("執行配置的destroy-method"); ????} ????//通過<bean>的init-method屬性指定的初始化方法 ????publicvoid initMethod() throwsException { ????????System.out.println("執行配置的init-method"); ????} }

配置文件中的配置:

1 2 <beanname="giraffeService"class="com.giraffe.spring.service.GiraffeService"init-method="initMethod"destroy-method="destroyMethod"> </bean>

需要注意的是自定義的init-method和post-method方法可以拋異常但是不能有參數。
這種方式比較推薦,因為可以自己創建方法,無需將Bean的實現直接依賴于spring的框架。

3.使用@PostConstruct和@PreDestroy注解

除了xml配置的方式,Spring也支持用@PostConstruct和?@PreDestroy注解來指定init和destroy方法。這兩個注解均在javax.annotation包中。
為了注解可以生效,需要在配置文件中定義org.springframework.context.annotation.CommonAnnotationBeanPostProcessor或context:annotation-config
如下:

1 2 3 4 5 6 7 8 9 10 11 12 publicclass GiraffeService { ????@PostConstruct ????publicvoid initPostConstruct(){ ????????System.out.println("執行PostConstruct注解標注的方法"); ????} ????@PreDestroy ????publicvoid preDestroy(){ ????????System.out.println("執行preDestroy注解標注的方法"); ????} }

配置文件:

1 <beanclass="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

實現*Aware接口 在Bean中使用Spring框架的一些對象

有些時候我們需要在Bean的初始化中使用Spring框架自身的一些對象來執行一些操作,比如獲取ServletContext的一些參數,獲取ApplicaitionContext中的BeanDefinition的名字,獲取Bean在容器中的名字等等。為了讓Bean可以獲取到框架自身的一些對象,Spring提供了一組名為*Aware的接口。
這些接口均繼承于org.springframework.beans.factory.Aware標記接口,并提供一個將由Bean實現的set*方法,Spring通過基于setter的依賴注入方式使相應的對象可以被Bean使用。
網上說,這些接口是利用觀察者模式實現的,類似于servlet listeners,目前還不明白,不過這也不在本文的討論范圍內。
介紹一些重要的Aware接口:

  • ApplicationContextAware: 獲得ApplicationContext對象,可以用來獲取所有Bean definition的名字。
  • BeanFactoryAware:獲得BeanFactory對象,可以用來檢測Bean的作用域。
  • BeanNameAware:獲得Bean在配置文件中定義的名字。
  • ResourceLoaderAware:獲得ResourceLoader對象,可以獲得classpath中某個文件。
  • ServletContextAware:在一個MVC應用中可以獲取ServletContext對象,可以讀取context中的參數。
  • ServletConfigAware在一個MVC應用中可以獲取ServletConfig對象,可以讀取config中的參數。

如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 publicclass GiraffeService implements??ApplicationContextAware, ????????ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, ????????BeanNameAware, EnvironmentAware, ImportAware, ResourceLoaderAware{ ?????????@Override ????publicvoid setBeanClassLoader(ClassLoader classLoader) { ????????System.out.println("執行setBeanClassLoader,ClassLoader Name = " + classLoader.getClass().getName()); ????} ????@Override ????publicvoid setBeanFactory(BeanFactory beanFactory) throwsBeansException { ????????System.out.println("執行setBeanFactory,setBeanFactory:: giraffe bean singleton=" +? beanFactory.isSingleton("giraffeService")); ????} ????@Override ????publicvoid setBeanName(String s) { ????????System.out.println("執行setBeanName:: Bean Name defined in context=" ????????????????+ s); ????} ????@Override ????publicvoid setApplicationContext(ApplicationContext applicationContext) throwsBeansException { ????????System.out.println("執行setApplicationContext:: Bean Definition Names=" ????????????????+ Arrays.toString(applicationContext.getBeanDefinitionNames())); ????} ????@Override ????publicvoid setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { ????????System.out.println("執行setApplicationEventPublisher"); ????} ????@Override ????publicvoid setEnvironment(Environment environment) { ????????System.out.println("執行setEnvironment"); ????} ????@Override ????publicvoid setResourceLoader(ResourceLoader resourceLoader) { ????????Resource resource = resourceLoader.getResource("classpath:spring-beans.xml"); ????????System.out.println("執行setResourceLoader:: Resource File Name=" ????????????????+ resource.getFilename()); ????} ????@Override ????publicvoid setImportMetadata(AnnotationMetadata annotationMetadata) { ????????System.out.println("執行setImportMetadata"); ????} }

BeanPostProcessor

上面的*Aware接口是針對某個實現這些接口的Bean定制初始化的過程,
Spring同樣可以針對容器中的所有Bean,或者某些Bean定制初始化過程,只需提供一個實現BeanPostProcessor接口的類即可。 該接口中包含兩個方法,postProcessBeforeInitialization和postProcessAfterInitialization。 postProcessBeforeInitialization方法會在容器中的Bean初始化之前執行, postProcessAfterInitialization方法在容器中的Bean初始化之后執行。
如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 publicclass CustomerBeanPostProcessor implementsBeanPostProcessor { ????@Override ????publicObject postProcessBeforeInitialization(Object bean, String beanName) throwsBeansException { ????????System.out.println("執行BeanPostProcessor的postProcessBeforeInitialization方法,beanName="+ beanName); ????????returnbean; ????} ????@Override ????publicObject postProcessAfterInitialization(Object bean, String beanName) throwsBeansException { ????????System.out.println("執行BeanPostProcessor的postProcessAfterInitialization方法,beanName="+ beanName); ????????returnbean; ????} }

要將BeanPostProcessor的Bean像其他Bean一樣定義在配置文件中

1 <beanclass="com.giraffe.spring.service.CustomerBeanPostProcessor"/>

總結

所以。。。結合第一節控制臺輸出的內容,Spring Bean的生命周期是這樣紙的:

  • Bean容器找到配置文件中Spring Bean的定義。
  • Bean容器利用Java Reflection API創建一個Bean的實例。
  • 如果涉及到一些屬性值 利用set方法設置一些屬性值。
  • 如果Bean實現了BeanNameAware接口,調用setBeanName()方法,傳入Bean的名字。
  • 如果Bean實現了BeanClassLoaderAware接口,調用setBeanClassLoader()方法,傳入ClassLoader對象的實例。
  • 如果Bean實現了BeanFactoryAware接口,調用setBeanClassLoader()方法,傳入ClassLoader對象的實例。
  • 與上面的類似,如果實現了其他*Aware接口,就調用相應的方法。
  • 如果有和加載這個Bean的Spring容器相關的BeanPostProcessor對象,執行postProcessBeforeInitialization()方法
  • 如果Bean實現了InitializingBean接口,執行afterPropertiesSet()方法。
  • 如果Bean在配置文件中的定義包含init-method屬性,執行指定的方法。
  • 如果有和加載這個Bean的Spring容器相關的BeanPostProcessor對象,執行postProcessAfterInitialization()方法
  • 當要銷毀Bean的時候,如果Bean實現了DisposableBean接口,執行destroy()方法。
  • 當要銷毀Bean的時候,如果Bean在配置文件中的定義包含destroy-method屬性,執行指定的方法。

用圖表示一下(圖來源):

Spring BeanLifeCycle


原文出處:?Giraffe

from:?http://www.importnew.com/22350.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【Spring】Bean的生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。