詳解Spring中Bean的生命周期和作用域及實現(xiàn)方式
前言
在applicationContext.xml中配置完bean之后,Bean的聲明周期狀態(tài)有哪些。生命周期的各個階段可以做什么。在applicationContext.xml配置bean的作用域有哪些。其中各個作用域代表的是什么。適用于什么情況。這篇文章做一個記錄。
生命周期
初始化
可以直接查看圖片,圖片來自Spring Bean Life Cycle

從上圖看出,Bean初始化完成包括9個步驟。其中一些步驟包括接口的實現(xiàn),其中包括BeanNameAware接口,BeanFactoryAware接口。ApplicationContextAware接口。BeanPostProcessor接口,InitializingBean接口。那么這些接口在整個生命周期階段都起到什么作用?后面我們一一介紹。
實例化前
當(dāng)Bean全部屬性設(shè)置完畢后,往往需要執(zhí)行一些特定的行為,Spring提供了兩種方式來實現(xiàn)此功能:
- 使用init-mothod方法
- 實現(xiàn)initializingBean接口
指定初始化方法
如下:
package com.model;
public class InitBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("執(zhí)行構(gòu)造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
}
編寫加載器
package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
InitBean bean = (InitBean) context.getBean("init");
}
}
配置Bean
注意init-method參數(shù)
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="init" class="com.model.InitBean" init-method="init"/> </beans>
執(zhí)行結(jié)果

實現(xiàn)InitializingBean接口
實現(xiàn)InitializingBean接口會實現(xiàn)afterPropertiesSet方法,這個方法會自動調(diào)用。但是這個方式是侵入性的。一般情況下,不建議使用。
實現(xiàn)afterPropertiesSet方法
package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("執(zhí)行構(gòu)造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
}
配置xml
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <bean id="init" class="com.model.InitBean" init-method="init"/> </beans>
結(jié)果:

銷毀

同樣,上圖中表示來Bean銷毀時候的過程。包括DisposableBean接口。
使用destroy-method方法
package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("執(zhí)行構(gòu)造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
public void close(){
System.out.println("bean被銷毀");
}
}
配置Bean
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <bean id="init" class="com.model.InitBean" destroy-method="close"/> </beans>
配置加載器
package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
context.registerShutdownHook();
InitBean bean = (InitBean) context.getBean("init");
}
}
結(jié)果:

實現(xiàn)DisposableBean接口
實現(xiàn)DisposableBean接口
package com.model;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean,DisposableBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("執(zhí)行構(gòu)造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("調(diào)用init方法進行成員變量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
public void close(){
System.out.println("bean被銷毀");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("bean被銷毀完成");
}
}
配置文件
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <!-- <bean id="init" class="com.model.InitBean" destroy-method="close"/> --> <bean id="init" class="com.model.InitBean"/> </beans>

Spring Bean的作用域
| 作用域 | 描述 |
| singleton | 該作用域?qū)?bean 的定義的限制在每一個 Spring IoC 容器中的一個單一實例(默認)。 |
| prototype | 該作用域?qū)我?bean 的定義限制在任意數(shù)量的對象實例。 |
| request | 該作用域?qū)?bean 的定義限制為 HTTP 請求。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
| session | 該作用域?qū)?bean 的定義限制為 HTTP 會話。 只在web-aware Spring ApplicationContext的上下文中有效。 |
| global-session | 該作用域?qū)?bean 的定義限制為全局 HTTP 會話。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
配置示例
<bean id="..." class="..." scope="singleton"> </bean>
使用方法注入?yún)f(xié)調(diào)作用域不同的Bean
正常情況下,如果singleton作用域依賴singleton作用域。即每次獲取到的都是一樣的對象。同理,prototype作用域依賴prototype作用域,每次獲取到的都是新的對象。但是,如果singleton依賴prototype作用域,那么每次獲取到的singleton中的prototype都是第一次創(chuàng)建的prototype。如何協(xié)調(diào)這種關(guān)系。保證每次獲取到的都是正確的呢。
對于這種情況,Spring提供了lookup方法用來解決這種問題。
首先我們定義一個原型:
package com.model;
public class MyHelper {
public void doSomethingHelpful() {
}
}
然后通過接口注入:
package com.model;
public interface DemoBean {
MyHelper getHelper();
void somePeration();
}
配置一個單例:
package com.model;
/**
* 測試類
* @author kevin
*
*/
public abstract class AbstractLookupDemo implements DemoBean {
public abstract MyHelper getMyHelper();
@Override
public MyHelper getHelper() {
// TODO Auto-generated method stub
return getMyHelper();
}
@Override
public void somePeration() {
// TODO Auto-generated method stub
}
}
配置文件:
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helper" class="com.model.MyHelper" scope="prototype"/> <bean id="standardLookupBean" class="com.model.StandardLookupDemo"> <property name="myHelper" ref="helper"></property> </bean> <bean id = "abstractLookupBean" class="com.model.AbstractLookupDemo"> <lookup-method name="getMyHelper" bean="helper"></lookup-method> </bean> </beans>
加載配置文件:
package com.model;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lookBean.xml");
context.registerShutdownHook();
System.out.println("傳遞standardLookupBean");
test(context, "standardLookupBean");
System.out.println("傳遞AbstractLookupDemo");
test(context, "abstractLookupBean");
}
public static void test(AbstractApplicationContext context,String beanName) {
DemoBean bean = (DemoBean) context.getBean(beanName);
MyHelper helper1 = bean.getHelper();
MyHelper helper2 = bean.getHelper();
System.out.println("測試"+beanName);
System.out.println("兩個helper是否相同?"+(helper1==helper2));
StopWatch stopWatch = new StopWatch();
stopWatch.start("lookupDemo");
for (int i = 0; i < 10000; i++) {
MyHelper helper = bean.getHelper();
helper.doSomethingHelpful();
}
stopWatch.stop();
System.out.println("獲取10000次花費了"+stopWatch.getTotalTimeMillis()+"毫秒");
}
}
結(jié)果:

從上面的結(jié)果圖看出,以前的方式生成的對象每次都是相同的。通過lookup方式注入每次是不同的??梢越鉀Q這種問題。但是有沒有更簡單的方式,感覺這種方式優(yōu)點麻煩。
讓Bean感知Spring容器
實現(xiàn)BeanNameAware,自定設(shè)置id值。
實現(xiàn)BeanFactoryAware,ApplicationContextAware 感知Spring容器。獲取Spring容器。
Spring國際化支持
配置配置文件
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>message</value> </list> </property> </bean> </beans>
新建中文配置文件
message_zh_CN.properties:
hello=welcome,{0}
now=now is : {0}
新建英文配置文件
message_en_US.properties:
hello=\u4F60\u597D,{0}
now=\u73B0\u5728\u7684\u65F6\u95F4\u662F : {0}
加載配置文件
package com.model;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("globalization.xml");
String[] a = {"讀者"};
String hello = context.getMessage("hello",a, Locale.CHINA);
Object[] b = {new Date()};
String now = context.getMessage("now",b, Locale.CHINA);
System.out.println(hello);
System.out.println(now);
hello = context.getMessage("hello",a, Locale.US);
now = context.getMessage("now",b, Locale.US);
System.out.println(hello);
System.out.println(now);
}
}
結(jié)果

總結(jié)
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- 淺談Spring中Bean的作用域、生命周期
- Spring實戰(zhàn)之Bean的作用域request用法分析
- Spring實戰(zhàn)之Bean的作用域singleton和prototype用法分析
- 深入了解Spring中Bean的作用域和生命周期
- spring ioc的簡單實例及bean的作用域?qū)傩越馕?/a>
- JSP 中Spring Bean 的作用域詳解
- 簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
- Spring中Bean的作用域與生命周期詳解
- 詳解Spring中Bean的作用域與生命周期
- Spring?框架中的?Bean?作用域(Scope)使用詳解
相關(guān)文章
SpringBoot引入Redis報org.springframework.data.redis.core.RedisT
這篇文章主要介紹了SpringBoot引入Redis報org.springframework.data.redis.core.RedisTemplate類找不到錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決記錄
系統(tǒng)相關(guān)使用人員反饋系統(tǒng)故障,這篇文章主要介紹了線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決的記錄,有需要的朋友可以借鑒參考下2022-03-03
Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之漢諾塔
漢諾塔是源于印度一個古老傳說的益智玩具。大梵天創(chuàng)造世界時做了三根石柱,在一根柱子上從下往上按大小順序摞著64片黃金圓盤。大梵天命令婆羅門把圓盤從下面開始按大小順序重新擺放在另一根柱子上。并且規(guī)定,在小圓盤上不能放大圓盤,三根柱子之間一次只能移動一個圓盤2022-02-02

