亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Spring Bean的生命周期

2019-11-14 09:59:30
字體:
來源:轉載
供稿:網友

sPRing作為當前java最流行、最強大的輕量級框架,受到了程序員的熱烈歡迎。準確的了解Spring Bean的生命周期是非常必要的。我們通常使用applicationContext作為Spring容器。這里,我們講的也是 ApplicationContext中Bean的生命周期。而實際上BeanFactory也是差不多的,只不過處理器需要手動注冊。

 ,謝謝。

一、生命周期流程圖:

  Spring Bean的完整生命周期從創建Spring容器開始,直到最終Spring容器銷毀Bean,這其中包含了一系列關鍵點。

 

若容器注冊了以上各種接口,程序那么將會按照以上的流程進行。下面將仔細講解各接口作用。

 

二、各種接口方法分類

Bean的完整生命周期經歷了各種方法調用,這些方法可以劃分為以下幾類:

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

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

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

4、工廠后處理器接口方法 ?。骸 ∵@個包括了BeanFactoryPostProcessor等等非常有用的工廠后處理器接口的方法。工廠后處理器也是容器級的。在應用上下文裝配配置文件之后立即調用。

  

三、演示

我們用一個簡單的Spring Bean來演示一下Spring Bean的生命周期。

1、首先是一個簡單的Spring Bean,調用Bean自身的方法和Bean級生命周期接口方法,為了方便演示,它實現了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean這4個接口,同時有2個方法,對應配置文件中<bean>的init-method和destroy-method。如下:

[java] view plain copy print?<span style="font-family:SimSun;font-size:14px;">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("【構造器】調用Person的構造器實例化");      }        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接口】調用BeanFactoryAware.setBeanFactory()");          this.beanFactory = arg0;      }        // 這是BeanNameAware接口方法      @Override      public void setBeanName(String arg0) {          System.out.println("【BeanNameAware接口】調用BeanNameAware.setBeanName()");          this.beanName = arg0;      }        // 這是InitializingBean接口方法      @Override      public void afterPropertiesSet() throws Exception {          System.out                  .println("【InitializingBean接口】調用InitializingBean.afterPropertiesSet()");      }        // 這是DiposibleBean接口方法      @Override      public void destroy() throws Exception {          System.out.println("【DiposibleBean接口】調用DiposibleBean.destory()");      }        // 通過<bean>的init-method屬性指定的初始化方法      public void myInit() {          System.out.println("【init-method】調用<bean>的init-method屬性指定的初始化方法");      }        // 通過<bean>的destroy-method屬性指定的初始化方法      public void myDestory() {          System.out.println("【destroy-method】調用<bean>的destroy-method屬性指定的初始化方法");      }  }</span>  

2、接下來是演示BeanPostProcessor接口的方法,如下:

[java] view plain copy print?<span style="font-family:SimSun;font-size:14px;">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實現類構造器??!");          // TODO Auto-generated constructor stub      }        @Override      public Object postProcessAfterInitialization(Object arg0, String arg1)              throws BeansException {          System.out                  .println("BeanPostProcessor接口方法postProcessAfterInitialization對屬性進行更改!");          return arg0;      }        @Override      public Object postProcessBeforeInitialization(Object arg0, String arg1)              throws BeansException {          System.out                  .println("BeanPostProcessor接口方法postProcessBeforeInitialization對屬性進行更改!");          return arg0;      }  }</span>  

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

 

3、InstantiationAwareBeanPostProcessor接口本質是BeanPostProcessor的子接口,一般我們繼承Spring為其提供的適配器類InstantiationAwareBeanPostProcessorAdapter來使用它,如下:

[java] view plain copy print?<span style="font-family:SimSun;font-size:14px;">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實現類構造器??!");      }        // 接口方法、實例化Bean之前調用      @Override      public Object postProcessBeforeInstantiation(Class beanClass,              String beanName) throws BeansException {          System.out                  .println("InstantiationAwareBeanPostProcessor調用postProcessBeforeInstantiation方法");          return null;      }        // 接口方法、實例化Bean之后調用      @Override      public Object postProcessAfterInitialization(Object bean, String beanName)              throws BeansException {          System.out                  .println("InstantiationAwareBeanPostProcessor調用postProcessAfterInitialization方法");          return bean;      }        // 接口方法、設置某個屬性時調用      @Override      public PropertyValues postProcessPropertyValues(PropertyValues pvs,              PropertyDescriptor[] pds, Object bean, String beanName)              throws BeansException {          System.out                  .println("InstantiationAwareBeanPostProcessor調用postProcessPropertyValues方法");          return pvs;      }  }</span>  

這個有3個方法,其中第二個方法postProcessAfterInitialization就是重寫了BeanPostProcessor的方法。第三個方法postProcessPropertyValues用來操作屬性,返回值也應該是PropertyValues對象。

 

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

[java] view plain copy print?<span style="font-family:SimSun;font-size:14px;">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實現類構造器!!");      }        @Override      public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)              throws BeansException {          System.out                  .println("BeanFactoryPostProcessor調用postProcessBeanFactory方法");          BeanDefinition bd = arg0.getBeanDefinition("person");          bd.getPropertyValues().addPropertyValue("phone", "110");      }    }</span>  

 

5、配置文件如下beans.xml,很簡單,使用ApplicationContext,處理器不用手動注冊:

[html] view plain copy print?<span style="font-family:SimSun;font-size:14px;"><?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></span>  

6、下面測試一下:

[java] view plain copy print?<span style="font-family:SimSun;font-size:14px;">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("現在開始初始化容器");            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("現在開始關閉容器!");          ((ClassPathXmlApplicationContext) factory).registerShutdownHook();      }  }</span>  

關閉容器使用的是實際是AbstractApplicationContext的鉤子方法。

我們來看一下結果:

現在開始初始化容器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 hierarchy2014-5-18 15:46:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [springBeanTest/beans.xml]這是BeanFactoryPostProcessor實現類構造器?。eanFactoryPostProcessor調用postProcessBeanFactory方法這是BeanPostProcessor實現類構造器??!這是InstantiationAwareBeanPostProcessorAdapter實現類構造器??!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 hierarchyInstantiationAwareBeanPostProcessor調用postProcessBeforeInstantiation方法【構造器】調用Person的構造器實例化InstantiationAwareBeanPostProcessor調用postProcessPropertyValues方法【注入屬性】注入屬性address【注入屬性】注入屬性name【注入屬性】注入屬性phone【BeanNameAware接口】調用BeanNameAware.setBeanName()【BeanFactoryAware接口】調用BeanFactoryAware.setBeanFactory()BeanPostProcessor接口方法postProcessBeforeInitialization對屬性進行更改!【InitializingBean接口】調用InitializingBean.afterPropertiesSet()【init-method】調用<bean>的init-method屬性指定的初始化方法BeanPostProcessor接口方法postProcessAfterInitialization對屬性進行更改!InstantiationAwareBeanPostProcessor調用postProcessAfterInitialization方法容器初始化成功Person [address=廣州, name=張三, phone=110]現在開始關閉容器!【DiposibleBean接口】調用DiposibleBean.destory()【destroy-method】調用<bean>的destroy-method屬性指定的初始化方法

 

/***   ————————如果覺得本博文還行,別忘了推薦一下哦,謝謝!*   作者:錢書康*   歡迎轉載,請保留此段聲明。*   出處:http://www.49028c.com/zrtqsk/ */


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美性猛交xxx| 色中色综合影院手机版在线观看| 亚洲精品99久久久久| 日韩欧美国产一区二区| 国产精品免费在线免费| 97精品视频在线| 国产在线拍揄自揄视频不卡99| 欧美一级黄色网| 色偷偷偷亚洲综合网另类| 久久久国产视频| 狠狠干狠狠久久| 欧美精品videos另类日本| 欧美性xxxx极品高清hd直播| 欧美亚洲另类视频| 色老头一区二区三区在线观看| 亚洲欧美在线磁力| 亚洲欧洲激情在线| 青青草原成人在线视频| 亚洲影视中文字幕| 国产色视频一区| 国内外成人免费激情在线视频网站| 一区二区欧美久久| 日韩在线视频免费观看| 久久综合国产精品台湾中文娱乐网| 久久久久久尹人网香蕉| 国产精品视频在线播放| 欧美一级片久久久久久久| 亚洲欧洲高清在线| 日韩在线观看免费| 国产视频观看一区| 一道本无吗dⅴd在线播放一区| 亚洲高清一区二| 亚洲风情亚aⅴ在线发布| 91中文字幕在线观看| 国产成人亚洲综合91精品| 亚洲精品色婷婷福利天堂| 欧美精品在线免费观看| 亚洲精品国产精品国自产观看浪潮| 成人av电影天堂| 欧美在线一区二区三区四| 日韩美女写真福利在线观看| 亚洲一区二区三区四区视频| 欧美日韩色婷婷| 日本成人免费在线| 久久免费精品视频| 久久五月天色综合| 国产主播喷水一区二区| 精品偷拍各种wc美女嘘嘘| 欧美性资源免费| 日韩在线观看免费| 精品国内产的精品视频在线观看| 日韩精品一区二区视频| 国产精品露脸av在线| 日韩中文在线视频| 久久天天躁狠狠躁夜夜躁| www.亚洲天堂| 在线观看精品自拍私拍| 亚洲欧美日韩精品久久亚洲区| 亚洲成年人影院在线| 亚洲图片制服诱惑| 国产精品羞羞答答| 96精品久久久久中文字幕| 国产精品福利久久久| 国产z一区二区三区| 欧美xxxx做受欧美.88| 久久全国免费视频| 亚洲女人被黑人巨大进入| 久久人人爽亚洲精品天堂| 欧美野外猛男的大粗鳮| 久久色在线播放| 久久99青青精品免费观看| 成人精品福利视频| 最近日韩中文字幕中文| 91国内产香蕉| 91网在线免费观看| 日韩在线视频观看正片免费网站| 欧美视频在线视频| 96pao国产成视频永久免费| 亚洲欧美国产精品| 久久免费视频在线| 欧美一区二区三区免费观看| 亚洲色图av在线| 在线电影av不卡网址| 欧美日韩在线视频一区二区| 亚洲a区在线视频| 久久精品中文字幕电影| 久久久久久久一区二区| 亚洲欧美另类国产| 久久久久99精品久久久久| 欧美日韩国产成人在线观看| 黑人狂躁日本妞一区二区三区| 日韩中文字幕在线视频播放| 永久免费精品影视网站| 国产精品爽爽爽爽爽爽在线观看| 亚洲第一黄色网| 日韩精品在线播放| 亚洲桃花岛网站| 亚洲欧美日本伦理| 91亚洲精品久久久久久久久久久久| 亚洲91av视频| 亚洲变态欧美另类捆绑| 亚洲国产91色在线| 国内精品久久久久影院 日本资源| 国产欧美韩国高清| 国产精品成av人在线视午夜片| 成人精品视频久久久久| 欧美大人香蕉在线| 性色av香蕉一区二区| 亚洲欧美国产va在线影院| 国产午夜精品美女视频明星a级| 久久精品人人做人人爽| 国产亚洲视频在线观看| 狠狠爱在线视频一区| 成人乱人伦精品视频在线观看| 久久久免费精品视频| 中文字幕一区二区精品| 国产精品欧美一区二区| 91老司机精品视频| 久热精品视频在线观看一区| 97精品一区二区视频在线观看| 欧美俄罗斯乱妇| 国产精品久久久久久久久久99| 日韩中文字幕在线观看| 国产91色在线|免| 色综合色综合网色综合| 欧美最猛性xxxxx亚洲精品| 黄网站色欧美视频| 国产精品扒开腿爽爽爽视频| 欧美成人黑人xx视频免费观看| 欧美日韩国产123| 北条麻妃99精品青青久久| 97香蕉久久夜色精品国产| 一区二区三区回区在观看免费视频| 成人黄色在线免费| 91精品久久久久久| 国产精品久久综合av爱欲tv| 日韩国产精品一区| 亚洲成人性视频| 日韩小视频在线观看| 亚洲激情成人网| 亚洲iv一区二区三区| 亚洲成色777777女色窝| 色偷偷av一区二区三区乱| 最近2019中文字幕大全第二页| 久久不射电影网| 成人字幕网zmw| 久久精品一本久久99精品| 国产99久久精品一区二区 夜夜躁日日躁| 欧美精品videofree1080p| 中国人与牲禽动交精品| 亚洲一区二区自拍| 国产在线精品成人一区二区三区| 日韩最新在线视频| 国产精品一区二区久久久久| 亚洲人在线观看| 亚洲第一中文字幕在线观看| 欧美精品www在线观看| 国产精品白丝jk喷水视频一区| 日韩一区二区在线视频| 九九久久精品一区| 欧美性猛交xxxx免费看久久久| 久久免费视频在线| 午夜精品一区二区三区在线| 久久综合国产精品台湾中文娱乐网|