亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年03月14日 09:06:48   投稿:daisy  
這篇文章主要給大家介紹了Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

前言

在applicationContext.xml中配置完bean之后,Bean的聲明周期狀態(tài)有哪些。生命周期的各個階段可以做什么。在applicationContext.xml配置bean的作用域有哪些。其中各個作用域代表的是什么。適用于什么情況。這篇文章做一個記錄。

生命周期

初始化

可以直接查看圖片,圖片來自Spring Bean Life Cycle

從上圖看出,Bean初始化完成包括9個步驟。其中一些步驟包括接口的實(shí)現(xiàn),其中包括BeanNameAware接口,BeanFactoryAware接口。ApplicationContextAware接口。BeanPostProcessor接口,InitializingBean接口。那么這些接口在整個生命周期階段都起到什么作用?后面我們一一介紹。

實(shí)例化前

當(dāng)Bean全部屬性設(shè)置完畢后,往往需要執(zhí)行一些特定的行為,Spring提供了兩種方式來實(shí)現(xiàn)此功能:

  • 使用init-mothod方法
  • 實(shí)現(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方法進(jìn)行成員變量的初始化");
 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é)果

實(shí)現(xiàn)InitializingBean接口

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

實(shí)現(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方法進(jìn)行成員變量的初始化");
 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方法進(jìn)行成員變量的初始化");
 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方法進(jìn)行成員變量的初始化");
 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方法進(jìn)行成員變量的初始化");
 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é)果:

實(shí)現(xiàn)DisposableBean接口

實(shí)現(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方法進(jìn)行成員變量的初始化");
 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方法進(jìn)行成員變量的初始化");
 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 容器中的一個單一實(shí)例(默認(rèn))。
prototype 該作用域?qū)我?bean 的定義限制在任意數(shù)量的對象實(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次花費(fèi)了"+stopWatch.getTotalTimeMillis()+"毫秒");
 }
}

結(jié)果:

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

讓Bean感知Spring容器

實(shí)現(xiàn)BeanNameAware,自定設(shè)置id值。

實(shí)現(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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringBoot引入Redis報(bào)org.springframework.data.redis.core.RedisTemplate類找不到錯誤問題

    SpringBoot引入Redis報(bào)org.springframework.data.redis.core.RedisT

    這篇文章主要介紹了SpringBoot引入Redis報(bào)org.springframework.data.redis.core.RedisTemplate類找不到錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Java多線程之死鎖詳解

    Java多線程之死鎖詳解

    這篇文章主要介紹了Java多線程的死鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-10-10
  • Springboot整合logback的詳細(xì)教程

    Springboot整合logback的詳細(xì)教程

    這篇文章主要介紹了Springboot整合logback的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • 線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決記錄

    線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決記錄

    系統(tǒng)相關(guān)使用人員反饋系統(tǒng)故障,這篇文章主要介紹了線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決的記錄,有需要的朋友可以借鑒參考下
    2022-03-03
  • Java 從Set里面取出有序的記錄詳解及實(shí)例

    Java 從Set里面取出有序的記錄詳解及實(shí)例

    這篇文章主要介紹了Java 從Set里面取出有序的記錄詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • java el簡介及用法

    java el簡介及用法

    EL簡介語法結(jié)構(gòu) 運(yùn)算符等資料代碼。
    2009-04-04
  • java實(shí)現(xiàn)簡單的掃雷小游戲

    java實(shí)現(xiàn)簡單的掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • springboot?集成dubbo的步驟詳解

    springboot?集成dubbo的步驟詳解

    這篇文章主要介紹了springboot?簡易集成dubbo的步驟詳解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • Java Number類原理實(shí)例解析

    Java Number類原理實(shí)例解析

    這篇文章主要介紹了Java Number類原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之漢諾塔

    Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之漢諾塔

    漢諾塔是源于印度一個古老傳說的益智玩具。大梵天創(chuàng)造世界時做了三根石柱,在一根柱子上從下往上按大小順序摞著64片黃金圓盤。大梵天命令婆羅門把圓盤從下面開始按大小順序重新擺放在另一根柱子上。并且規(guī)定,在小圓盤上不能放大圓盤,三根柱子之間一次只能移動一個圓盤
    2022-02-02

最新評論