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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過程解析
這篇文章主要介紹了JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
springcloud引入spring-cloud-starter-openfeign失敗的解決
這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解在Spring-Boot中實現(xiàn)通用Auth認證的幾種方式
這篇文章主要介紹了詳解在Spring-Boot中實現(xiàn)通用Auth認證的幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Springboot @Transactional使用時需注意的幾個問題記錄
本文詳細介紹了Spring Boot中使用`@Transactional`注解進行事務(wù)管理的多個方面,包括事務(wù)的隔離級別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個類中調(diào)用事務(wù)方法時可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01
SpringCloud LoadBalancer自定義負載均衡器使用解析
LoadBalancerClient 是 SpringCloud 提供的一種負載均衡客戶端,Ribbon 負載均衡組件內(nèi)部也是集成了 LoadBalancerClient 來實現(xiàn)負載均衡,本文給大家深入解析 LoadBalancerClient 接口源碼,感興趣的朋友跟隨小編一起看看吧2023-04-04
分布式調(diào)度XXL-Job整合Springboot2.X實戰(zhàn)操作過程(推薦)
這篇文章主要介紹了分布式調(diào)度XXL-Job整合Springboot2.X實戰(zhàn)操作,包括定時任務(wù)的使用場景和常見的定時任務(wù),通過本文學(xué)習(xí)幫助大家該選擇哪個分布式任務(wù)調(diào)度平臺,對此文感興趣的朋友一起看看吧2022-04-04
基于springBoot配置文件properties和yml中數(shù)組的寫法
這篇文章主要介紹了springBoot配置文件properties和yml中數(shù)組的寫法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

