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

Spring Bean的生命周期詳細(xì)介紹

 更新時(shí)間:2016年09月23日 09:24:21   作者:Chandler Qian  
這篇文章主要介紹了Spring Bean的生命周期的相關(guān)資料,需要的朋友可以參考下

Spring作為當(dāng)前Java最流行、最強(qiáng)大的輕量級(jí)框架,受到了程序員的熱烈歡迎。準(zhǔn)確的了解Spring Bean的生命周期是非常必要的。我們通常使用ApplicationContext作為Spring容器。這里,我們講的也是 ApplicationContext中Bean的生命周期。而實(shí)際上BeanFactory也是差不多的,只不過(guò)處理器需要手動(dòng)注冊(cè)。

一、生命周期流程圖:

  Spring Bean的完整生命周期從創(chuàng)建Spring容器開(kāi)始,直到最終Spring容器銷毀Bean,這其中包含了一系列關(guān)鍵點(diǎn)。

 

若容器注冊(cè)了以上各種接口,程序那么將會(huì)按照以上的流程進(jìn)行。下面將仔細(xì)講解各接口作用。

二、各種接口方法分類

Bean的完整生命周期經(jīng)歷了各種方法調(diào)用,這些方法可以劃分為以下幾類:

1、Bean自身的方法 ?。骸 ∵@個(gè)包括了Bean本身調(diào)用的方法和通過(guò)配置文件中<bean>的init-method和destroy-method指定的方法

2、Bean級(jí)生命周期接口方法 ?。骸 ∵@個(gè)包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean這些接口的方法

3、容器級(jí)生命周期接口方法 ?。骸 ∵@個(gè)包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 這兩個(gè)接口實(shí)現(xiàn),一般稱它們的實(shí)現(xiàn)類為“后處理器”。

4、工廠后處理器接口方法 ?。骸 ∵@個(gè)包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工廠后處理器  接口的方法。工廠后處理器也是容器級(jí)的。在應(yīng)用上下文裝配配置文件之后立即調(diào)用?! ?/p>

三、演示

我們用一個(gè)簡(jiǎn)單的Spring Bean來(lái)演示一下Spring Bean的生命周期。

1、首先是一個(gè)簡(jiǎn)單的Spring Bean,調(diào)用Bean自身的方法和Bean級(jí)生命周期接口方法,為了方便演示,它實(shí)現(xiàn)了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean這4個(gè)接口,同時(shí)有2個(gè)方法,對(duì)應(yīng)配置文件中<bean>的init-method和destroy-method。如下:

package springBeanTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author qsk
 */
public class Person implements BeanFactoryAware, BeanNameAware,
    InitializingBean, DisposableBean {

  private String name;
  private String address;
  private int phone;

  private BeanFactory beanFactory;
  private String beanName;

  public Person() {
    System.out.println("【構(gòu)造器】調(diào)用Person的構(gòu)造器實(shí)例化");
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    System.out.println("【注入屬性】注入屬性name");
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    System.out.println("【注入屬性】注入屬性address");
    this.address = address;
  }

  public int getPhone() {
    return phone;
  }

  public void setPhone(int phone) {
    System.out.println("【注入屬性】注入屬性phone");
    this.phone = phone;
  }

  @Override
  public String toString() {
    return "Person [address=" + address + ", name=" + name + ", phone="
        + phone + "]";
  }

  // 這是BeanFactoryAware接口方法
  @Override
  public void setBeanFactory(BeanFactory arg0) throws BeansException {
    System.out
        .println("【BeanFactoryAware接口】調(diào)用BeanFactoryAware.setBeanFactory()");
    this.beanFactory = arg0;
  }

  // 這是BeanNameAware接口方法
  @Override
  public void setBeanName(String arg0) {
    System.out.println("【BeanNameAware接口】調(diào)用BeanNameAware.setBeanName()");
    this.beanName = arg0;
  }

  // 這是InitializingBean接口方法
  @Override
  public void afterPropertiesSet() throws Exception {
    System.out
        .println("【InitializingBean接口】調(diào)用InitializingBean.afterPropertiesSet()");
  }

  // 這是DiposibleBean接口方法
  @Override
  public void destroy() throws Exception {
    System.out.println("【DiposibleBean接口】調(diào)用DiposibleBean.destory()");
  }

  // 通過(guò)<bean>的init-method屬性指定的初始化方法
  public void myInit() {
    System.out.println("【init-method】調(diào)用<bean>的init-method屬性指定的初始化方法");
  }

  // 通過(guò)<bean>的destroy-method屬性指定的初始化方法
  public void myDestory() {
    System.out.println("【destroy-method】調(diào)用<bean>的destroy-method屬性指定的初始化方法");
  }
}

 2、接下來(lái)是演示BeanPostProcessor接口的方法,如下:

package springBeanTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

  public MyBeanPostProcessor() {
    super();
    System.out.println("這是BeanPostProcessor實(shí)現(xiàn)類構(gòu)造器?。?);
    // TODO Auto-generated constructor stub
  }

  @Override
  public Object postProcessAfterInitialization(Object arg0, String arg1)
      throws BeansException {
    System.out
    .println("BeanPostProcessor接口方法postProcessAfterInitialization對(duì)屬性進(jìn)行更改!");
    return arg0;
  }

  @Override
  public Object postProcessBeforeInitialization(Object arg0, String arg1)
      throws BeansException {
    System.out
    .println("BeanPostProcessor接口方法postProcessBeforeInitialization對(duì)屬性進(jìn)行更改!");
    return arg0;
  }
}

如上,BeanPostProcessor接口包括2個(gè)方法postProcessAfterInitialization和postProcessBeforeInitialization,這兩個(gè)方法的第一個(gè)參數(shù)都是要處理的Bean對(duì)象,第二個(gè)參數(shù)都是Bean的name。返回值也都是要處理的Bean對(duì)象。這里要注意。 

3、InstantiationAwareBeanPostProcessor 接口本質(zhì)是BeanPostProcessor的子接口,一般我們繼承Spring為其提供的適配器類InstantiationAwareBeanPostProcessor Adapter來(lái)使用它,如下:

package springBeanTest;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

public class MyInstantiationAwareBeanPostProcessor extends
    InstantiationAwareBeanPostProcessorAdapter {

  public MyInstantiationAwareBeanPostProcessor() {
    super();
    System.out
        .println("這是InstantiationAwareBeanPostProcessorAdapter實(shí)現(xiàn)類構(gòu)造器??!");
  }

  // 接口方法、實(shí)例化Bean之前調(diào)用
  @Override
  public Object postProcessBeforeInstantiation(Class beanClass,
      String beanName) throws BeansException {
    System.out
        .println("InstantiationAwareBeanPostProcessor調(diào)用postProcessBeforeInstantiation方法");
    return null;
  }

  // 接口方法、實(shí)例化Bean之后調(diào)用
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName)
      throws BeansException {
    System.out
        .println("InstantiationAwareBeanPostProcessor調(diào)用postProcessAfterInitialization方法");
    return bean;
  }

  // 接口方法、設(shè)置某個(gè)屬性時(shí)調(diào)用
  @Override
  public PropertyValues postProcessPropertyValues(PropertyValues pvs,
      PropertyDescriptor[] pds, Object bean, String beanName)
      throws BeansException {
    System.out
        .println("InstantiationAwareBeanPostProcessor調(diào)用postProcessPropertyValues方法");
    return pvs;
  }
}

這個(gè)有3個(gè)方法,其中第二個(gè)方法postProcessAfterInitialization就是重寫了BeanPostProcessor的方法。第三個(gè)方法postProcessPropertyValues用來(lái)操作屬性,返回值也應(yīng)該是PropertyValues對(duì)象。

4、演示工廠后處理器接口方法,如下:

package springBeanTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

  public MyBeanFactoryPostProcessor() {
    super();
    System.out.println("這是BeanFactoryPostProcessor實(shí)現(xiàn)類構(gòu)造器?。?);
  }

  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
      throws BeansException {
    System.out
        .println("BeanFactoryPostProcessor調(diào)用postProcessBeanFactory方法");
    BeanDefinition bd = arg0.getBeanDefinition("person");
    bd.getPropertyValues().addPropertyValue("phone", "110");
  }

}

 5、配置文件如下beans.xml,很簡(jiǎn)單,使用ApplicationContext,處理器不用手動(dòng)注冊(cè):

<?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" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

  <bean id="beanPostProcessor" class="springBeanTest.MyBeanPostProcessor">
  </bean>

  <bean id="instantiationAwareBeanPostProcessor" class="springBeanTest.MyInstantiationAwareBeanPostProcessor">
  </bean>

  <bean id="beanFactoryPostProcessor" class="springBeanTest.MyBeanFactoryPostProcessor">
  </bean>
  
  <bean id="person" class="springBeanTest.Person" init-method="myInit"
    destroy-method="myDestory" scope="singleton" p:name="張三" p:address="廣州"
    p:phone="15900000000" />

</beans>

6、下面測(cè)試一下:

package springBeanTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanLifeCycle {

  public static void main(String[] args) {

    System.out.println("現(xiàn)在開(kāi)始初始化容器");
    
    ApplicationContext factory = new ClassPathXmlApplicationContext("springBeanTest/beans.xml");
    System.out.println("容器初始化成功");  
    //得到Preson,并使用
    Person person = factory.getBean("person",Person.class);
    System.out.println(person);
    
    System.out.println("現(xiàn)在開(kāi)始關(guān)閉容器!");
    ((ClassPathXmlApplicationContext)factory).registerShutdownHook();
  }
}

關(guān)閉容器使用的是實(shí)際是AbstractApplicationContext的鉤子方法。

我們來(lái)看一下結(jié)果:

現(xiàn)在開(kāi)始初始化容器
2014-5-18 15:46:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a0c7c: startup date [Sun May 18 15:46:20 CST 2014]; root of context hierarchy
2014-5-18 15:46:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [springBeanTest/beans.xml]
這是BeanFactoryPostProcessor實(shí)現(xiàn)類構(gòu)造器?。?
BeanFactoryPostProcessor調(diào)用postProcessBeanFactory方法
這是BeanPostProcessor實(shí)現(xiàn)類構(gòu)造器??!
這是InstantiationAwareBeanPostProcessorAdapter實(shí)現(xiàn)類構(gòu)造器??!
2014-5-18 15:46:20 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9934d4: defining beans [beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,person]; root of factory hierarchy
InstantiationAwareBeanPostProcessor調(diào)用postProcessBeforeInstantiation方法
【構(gòu)造器】調(diào)用Person的構(gòu)造器實(shí)例化
InstantiationAwareBeanPostProcessor調(diào)用postProcessPropertyValues方法
【注入屬性】注入屬性address
【注入屬性】注入屬性name
【注入屬性】注入屬性phone
【BeanNameAware接口】調(diào)用BeanNameAware.setBeanName()
【BeanFactoryAware接口】調(diào)用BeanFactoryAware.setBeanFactory()
BeanPostProcessor接口方法postProcessBeforeInitialization對(duì)屬性進(jìn)行更改!
【InitializingBean接口】調(diào)用InitializingBean.afterPropertiesSet()
【init-method】調(diào)用<bean>的init-method屬性指定的初始化方法
BeanPostProcessor接口方法postProcessAfterInitialization對(duì)屬性進(jìn)行更改!
InstantiationAwareBeanPostProcessor調(diào)用postProcessAfterInitialization方法
容器初始化成功
Person [address=廣州, name=張三, phone=110]
現(xiàn)在開(kāi)始關(guān)閉容器!
【DiposibleBean接口】調(diào)用DiposibleBean.destory()
【destroy-method】調(diào)用<bean>的destroy-method屬性指定的初始化方法

以上就是對(duì)Java Spring Bean 生命周期的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Spring條件注解沒(méi)生效該如何解決

    Spring條件注解沒(méi)生效該如何解決

    條件注解相信各位小伙伴都用過(guò),Spring?中的多環(huán)境配置?profile?底層就是通過(guò)條件注解來(lái)實(shí)現(xiàn)的,下面小編就來(lái)為大家介紹一下當(dāng)Spring條件注解沒(méi)生效時(shí)該如何解決,感興趣的可以了解下
    2023-09-09
  • Java springboot 整合 Nacos的實(shí)例代碼

    Java springboot 整合 Nacos的實(shí)例代碼

    這篇文章主要介紹了Java springboot 整合 Nacos的實(shí)例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 在Spring 中使用@Aspect 控制自定義注解的操作

    在Spring 中使用@Aspect 控制自定義注解的操作

    這篇文章主要介紹了在Spring 中使用@Aspect 控制自定義注解的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Springboot通過(guò)run啟動(dòng)web應(yīng)用的方法

    Springboot通過(guò)run啟動(dòng)web應(yīng)用的方法

    這篇文章主要介紹了Springboot通過(guò)run啟動(dòng)web應(yīng)用的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • JDBC連接MySql數(shù)據(jù)庫(kù)步驟 以及查詢、插入、刪除、更新等

    JDBC連接MySql數(shù)據(jù)庫(kù)步驟 以及查詢、插入、刪除、更新等

    這篇文章主要介紹了JDBC連接MySql數(shù)據(jù)庫(kù)步驟,以及查詢、插入、刪除、更新等十一個(gè)處理數(shù)據(jù)庫(kù)信息的功能,需要的朋友可以參考下
    2018-05-05
  • Java編程synchronized與lock的區(qū)別【推薦】

    Java編程synchronized與lock的區(qū)別【推薦】

    互聯(lián)網(wǎng)信息泛濫環(huán)境下少有的良心之作!如果您想對(duì)Java編程synchronized與lock的區(qū)別有所了解,這篇文章絕對(duì)值得!分享給大家,供需要的朋友參考。不說(shuō)了,我先學(xué)習(xí)去了。
    2017-10-10
  • mybatis-plus中更新null值的問(wèn)題解決

    mybatis-plus中更新null值的問(wèn)題解決

    本文主要介紹 mybatis-plus 中常使用的 update 相關(guān)方法的區(qū)別,以及更新 null 的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • Java使用同步方法解決銀行取錢的安全問(wèn)題案例分析

    Java使用同步方法解決銀行取錢的安全問(wèn)題案例分析

    這篇文章主要介紹了Java使用同步方法解決銀行取錢的安全問(wèn)題,結(jié)合具體案例形式分析了java同步方法實(shí)現(xiàn)多線程安全操作銀行取錢問(wèn)題,需要的朋友可以參考下
    2019-09-09
  • Java中的MultipartFile接口和File類解讀

    Java中的MultipartFile接口和File類解讀

    本文主要介紹了Java中的File類和Spring框架中的MultipartFile接口,File類提供了對(duì)文件和目錄操作的方法,如創(chuàng)建、刪除、重命名、判斷文件是否存在等,MultipartFile接口用于處理文件上傳,提供了獲取上傳文件信息和保存上傳文件的方法
    2025-02-02
  • Okhttp在SpringBoot中的應(yīng)用實(shí)戰(zhàn)記錄(太強(qiáng)了)

    Okhttp在SpringBoot中的應(yīng)用實(shí)戰(zhàn)記錄(太強(qiáng)了)

    這篇文章主要給大家介紹了關(guān)于Okhttp在SpringBoot中應(yīng)用實(shí)戰(zhàn)的相關(guān)資料,在Spring Boot中使用OkHttp主要是為了發(fā)送HTTP請(qǐng)求和處理響應(yīng),OkHttp是一個(gè)高效、易用的HTTP客戶端庫(kù),它具有簡(jiǎn)潔的API和強(qiáng)大的功能,需要的朋友可以參考下
    2023-12-12

最新評(píng)論

亚东县| 额济纳旗| 通道| 清镇市| 华阴市| 汉中市| 商水县| 巴彦淖尔市| 阆中市| 恩施市| 湾仔区| 南宁市| 东宁县| 安乡县| 西吉县| 章丘市| 志丹县| 玛多县| 洪江市| 美姑县| 营山县| 大丰市| 泸定县| 钟祥市| 疏勒县| 舟山市| 长宁区| 丽水市| 曲阳县| 历史| 镇坪县| 博爱县| 玛沁县| 泰顺县| 莆田市| 苍溪县| 江陵县| 华宁县| 霍州市| 关岭| 嵊泗县|