spring之Bean的生命周期詳解
Bean的生命周期:
Bean的定義——Bean的初始化——Bean的使用——Bean的銷毀
Bean的定義
Bean 是 spring 裝配的組件模型,一切實體類都可以配置成一個 Bean ,進而就可以在任何其他的 Bean 中使用,一個 Bean 也可以不是指定的實體類,這就是抽象 Bean 。
Bean的初始化
Spring中bean的初始化回調(diào)有兩種方法
一種是在配置文件中聲明init-method="init",然后在一個實體類中用init()方法來初始化
另一種是實現(xiàn)InitializingBean接口,覆蓋afterPropertiesSet()方法。
第一種:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="init-one" class="org.spring.test.BeanInitDemo1" init-method="init"> <property name="message" value="這里是配置文件中為message賦值"></property> </bean> </beans>
BeanInitDemo1類:
package org.spring.test; public class BeanInitDemo1 { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void init(){ this.setMessage("這里是init()方法初始化設(shè)值"); } }
測試類:
package org.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BeanInitDemo1 bid = (BeanInitDemo1) context.getBean("init-one"); System.out.println(bid.getMessage()); } }
運行結(jié)果:
這里是init()方法初始化設(shè)值
原因:init()初始化方法的調(diào)用是在配置文件的Bean初始化之后執(zhí)行的, 所以改變了配置文件中對message的賦值。
第二種:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="init-two" class="org.spring.test.BeanInitDemo2"> <property name="message" value="這里是配置文件中為message賦值"></property> </bean> </beans>
編寫B(tài)eanInitDemo2類,使其實現(xiàn)InitializingBean接口
package org.spring.test; import org.springframework.beans.factory.InitializingBean; public class BeanInitDemo2 implements InitializingBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub this.setMessage("這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值"); } }
測試:
package org.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BeanInitDemo2 bid = (BeanInitDemo2) context.getBean("init-two"); System.out.println(bid.getMessage()); } }
運行結(jié)果: 這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值
原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后執(zhí)行,所以改變了配置文件中對message的賦值
Bean的使用
Spring中有兩種使用bean的方法:
1, BeanFactory:
BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml")); factory.getBean("student");
BeanFactory是延遲加載,如果Bean的某一個屬性沒有注入,BeanFacotry加載后,直至第一次使用getBean方法才會拋出異常,也就是說當使用BeanFactory實例化對象時,配置的bean不會馬上被實例化。當你使用該bean時才會被實例化(getBean)。
2, ApplicationContext:
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
如果使用ApplicationContext,則配置的bean如果是singleton不管你用還是不用,都被實例化。ApplicationContext在初始化自身時檢驗,這樣有利于檢查所依賴屬性是否注入。ApplicationContext是BeanFactory的子類,除了具有BeanFactory的所有功能外還提供了更完整的框架功能,例如國際化,資源訪問等。所以通常情況下我們選擇使用ApplicationContext。
Bean的銷毀
Bean的銷毀和初始化一樣,都是提供了兩個方法
一是在配置文件中聲明destroy-method="cleanup",然后在類中寫一個cleanup()方法銷毀
二是實現(xiàn)DisposableBean接口,覆蓋destory()方法
第一種:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="destory-one" class="org.spring.test.BeanDestoryDemo1" destroy-method="cleanup"> <property name="message" value="這里是配置文件中為message賦值"></property> </bean> </beans>
BeanDestoryDemo1類:
package org.spring.test; public class BeanDestoryDemo1 { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void cleanup(){ System.out.println("銷毀之前可以調(diào)用一些方法"); } }
測試:
package org.spring.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DestortTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean("destory-one"); System.out.println(bdd.getMessage()); context.registerShutdownHook(); } }
運行結(jié)果:
context.registerShutdownHook()是為spring注冊關(guān)閉吊鉤,程序退出之前關(guān)閉spring容器,如果沒有
context.registerShutdownHook();將不會執(zhí)行cleanup()方法。
第二種:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="destory-two" class="org.spring.test.BeanDestoryDemo2"> <property name="message" value="這里是配置文件中為message賦值"></property> </bean> </beans>
BeanDestoryDemo2類:
package org.spring.test; import org.springframework.beans.factory.DisposableBean; public class BeanDestoryDemo2 implements DisposableBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("同樣,銷毀之前調(diào)用的方法"); } }
測試:
package org.spring.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DestortTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean("destory-two"); System.out.println(bdd.getMessage()); context.registerShutdownHook(); } }
運行結(jié)果:
Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及銷毀之前可以做一些工作,更靈活的管理Bean。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot項目如何優(yōu)雅實現(xiàn)Excel導(dǎo)入與導(dǎo)出功能
在我們平時工作中經(jīng)常會遇到要操作Excel的功能,比如導(dǎo)出個用戶信息或者訂單信息的Excel報表,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot項目中如何優(yōu)雅實現(xiàn)Excel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,需要的朋友可以參考下2022-06-06Java基礎(chǔ)之并發(fā)相關(guān)知識總結(jié)
隨著摩爾定律逐步失效,cpu單核性能達到瓶頸,并發(fā)逐漸逐漸得到廣泛應(yīng)用,因而學習了解以及使用并發(fā)就顯得十分重要,但并發(fā)相關(guān)的知識比較瑣碎,不易系統(tǒng)學習,因而本篇文章參照王寶令老師《Java并發(fā)編程》來勾勒出一張“并發(fā)全景圖”,需要的朋友可以參考下2021-05-05詳解SpringBoot中@PostMapping注解的用法
在SpringBoot中,我們經(jīng)常需要編寫RESTful Web服務(wù),以便于客戶端與服務(wù)器之間的通信,@PostMapping注解可以讓我們更方便地編寫POST請求處理方法,在本文中,我們將介紹@PostMapping注解的作用、原理,以及如何在SpringBoot應(yīng)用程序中使用它2023-06-06基于Mybatis實現(xiàn)動態(tài)數(shù)據(jù)源切換的示例代碼
在當今的互聯(lián)網(wǎng)應(yīng)用中,微服務(wù)大行其道,隨著業(yè)務(wù)的發(fā)展和擴展,單一的數(shù)據(jù)庫無法滿足日益增長的數(shù)據(jù)需求,本文將基于 JDK17 + Spring Boot 3 和 MyBatis 框架實現(xiàn)動態(tài)切換數(shù)據(jù)源功能,需要的朋友可以參考下2024-09-09SpringBoot返回統(tǒng)一的JSON標準格式實現(xiàn)步驟
這篇文章主要介紹了SpringBoot返回統(tǒng)一的JSON標準格式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08