最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring中Bean的生命周期和作用域及實現(xiàn)方式

 更新時間:2017年03月14日 09:06:48   投稿:daisy  
這篇文章主要給大家介紹了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接口。那么這些接口在整個生命周期階段都起到什么作用?后面我們一一介紹。

實例化前

當Bean全部屬性設置完畢后,往往需要執(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í)行構造方法");
 }
 
 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("調用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í)行結果

實現(xiàn)InitializingBean接口

實現(xiàn)InitializingBean接口會實現(xiàn)afterPropertiesSet方法,這個方法會自動調用。但是這個方式是侵入性的。一般情況下,不建議使用。

實現(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í)行構造方法");
 }
 
 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("調用init方法進行成員變量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("調用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>

結果:

銷毀

同樣,上圖中表示來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í)行構造方法");
 }
 
 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("調用init方法進行成員變量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("調用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");
 }
}

結果:

實現(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í)行構造方法");
 }
 
 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("調用init方法進行成員變量的初始化");
 this.name = NAME;
 this.age = AGE;
 System.out.println("初始化完成");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("調用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 該作用域將 bean 的定義的限制在每一個 Spring IoC 容器中的一個單一實例(默認)。
prototype 該作用域將單一 bean 的定義限制在任意數(shù)量的對象實例。
request  該作用域將 bean 的定義限制為 HTTP 請求。只在 web-aware Spring ApplicationContext 的上下文中有效。
session 該作用域將 bean 的定義限制為 HTTP 會話。 只在web-aware Spring ApplicationContext的上下文中有效。
global-session 該作用域將 bean 的定義限制為全局 HTTP 會話。只在 web-aware Spring ApplicationContext 的上下文中有效。

配置示例

<bean id="..." class="..." scope="singleton">
</bean>

使用方法注入協(xié)調作用域不同的Bean

正常情況下,如果singleton作用域依賴singleton作用域。即每次獲取到的都是一樣的對象。同理,prototype作用域依賴prototype作用域,每次獲取到的都是新的對象。但是,如果singleton依賴prototype作用域,那么每次獲取到的singleton中的prototype都是第一次創(chuàng)建的prototype。如何協(xié)調這種關系。保證每次獲取到的都是正確的呢。

對于這種情況,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()+"毫秒");
 }
}

結果:

從上面的結果圖看出,以前的方式生成的對象每次都是相同的。通過lookup方式注入每次是不同的??梢越鉀Q這種問題。但是有沒有更簡單的方式,感覺這種方式優(yōu)點麻煩。

讓Bean感知Spring容器

實現(xiàn)BeanNameAware,自定設置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);
 }
}

結果

總結

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • 關于Java中常見的負載均衡算法

    關于Java中常見的負載均衡算法

    這篇文章主要介紹了關于Java中常見的負載均衡算法,負載平衡是一種電子計算機技術,用來在多個計算機、網絡連接、CPU、磁盤驅動器或其他資源中分配負載,以達到優(yōu)化資源使用、最大化吞吐率、最小化響應時間、同時避免過載的目的,需要的朋友可以參考下
    2023-08-08
  • JAVA面試題String產生了幾個對象

    JAVA面試題String產生了幾個對象

    這篇文章主要介紹了JAVA面試題 String s = new String("xyz");產生了幾個對象?,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Redisson分布式鎖的原理和代碼實例

    Redisson分布式鎖的原理和代碼實例

    這篇文章主要介紹了Redisson分布式鎖的原理和代碼實例,在分布式系統(tǒng)中,鎖機制是非常重要的,Redisson是一個基于Redis的Java應用程序,常常被應用作為分布式鎖的解決方案,需要的朋友可以參考下
    2024-01-01
  • IntelliJ IDEA2025創(chuàng)建SpringBoot項目的實現(xiàn)步驟

    IntelliJ IDEA2025創(chuàng)建SpringBoot項目的實現(xiàn)步驟

    本文主要介紹了IntelliJ IDEA2025創(chuàng)建SpringBoot項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-07-07
  • 帶大家認識Java語法之泛型與通配符

    帶大家認識Java語法之泛型與通配符

    使用泛型的目的是利用Java編譯機制,在編譯過程中幫我們檢測代碼中不規(guī)范的有可能導致程序錯誤的代碼,下面這篇文章主要給大家介紹了關于Java泛型與通配符的相關資料,需要的朋友可以參考下
    2022-03-03
  • java生成申請單序列號的實現(xiàn)方法

    java生成申請單序列號的實現(xiàn)方法

    申請單序列號一般要求根據(jù)一定的規(guī)則生成后幾位連續(xù)的字符串,下面是我項目中使用的生成序列號的代碼,其中用到了鎖機制,有需要的朋友可以參考一下
    2014-01-01
  • MyBatis中resultType和resultMap的用法及說明

    MyBatis中resultType和resultMap的用法及說明

    MyBatis通過resultType和resultMap將SQL查詢結果映射為Java對象,resultType適用于簡單類型或完全匹配的Java類,resultMap則用于處理列名和屬性名不一致的情況
    2025-12-12
  • 通過實例解析java String不可變性

    通過實例解析java String不可變性

    這篇文章主要介紹了通過實例解析java String不可變性,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 解決Nacos成功啟動但是無法訪問 (Connection refused)

    解決Nacos成功啟動但是無法訪問 (Connection refused)

    這篇文章主要介紹了解決Nacos成功啟動但是無法訪問 (Connection refused)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java中super和this的用法詳解

    Java中super和this的用法詳解

    這篇文章主要介紹了Java中super和this的用法詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08

最新評論

睢宁县| 恩平市| 鄂托克旗| 通城县| 防城港市| 桐柏县| 汉沽区| 定安县| 司法| 新兴县| 桐柏县| 准格尔旗| 内江市| 永嘉县| 西盟| 吉首市| 博湖县| 康定县| 甘德县| 浦东新区| 桑植县| 长白| 东兰县| 焉耆| 长春市| 茌平县| 辉县市| 乐山市| 琼海市| 贵阳市| 南华县| 永平县| 潼南县| 庄浪县| 密山市| 同江市| 怀远县| 惠州市| 东海县| 平潭县| 尚义县|