国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Spring Bean的生命周期詳細介紹

2019-11-26 13:48:06
字體:
來源:轉載
供稿:網友

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、工廠后處理器接口方法  :  這個包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工廠后處理器  接口的方法。工廠后處理器也是容器級的。在應用上下文裝配配置文件之后立即調用。  

三、演示

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

1、首先是一個簡單的Spring Bean,調用Bean自身的方法和Bean級生命周期接口方法,為了方便演示,它實現了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean這4個接口,同時有2個方法,對應配置文件中<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("【構造器】調用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屬性指定的初始化方法");  }}

 2、接下來是演示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實現類構造器!!");    // 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;  }}

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

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

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;  }}

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

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實現類構造器!!");  }  @Override  public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)      throws BeansException {    System.out        .println("BeanFactoryPostProcessor調用postProcessBeanFactory方法");    BeanDefinition bd = arg0.getBeanDefinition("person");    bd.getPropertyValues().addPropertyValue("phone", "110");  }}

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

<?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、下面測試一下:

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();  }}

關閉容器使用的是實際是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實現類構造器!!BeanFactoryPostProcessor調用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屬性指定的初始化方法

以上就是對Java Spring Bean 生命周期的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拜城县| 宜城市| 涟源市| 梅州市| 托克托县| 江阴市| 阳高县| 金沙县| 德清县| 宝应县| 衡山县| 贡觉县| 山西省| 惠安县| 延寿县| 龙井市| 南丰县| 肇东市| 马关县| 高要市| 扎鲁特旗| 巫山县| 和顺县| 合肥市| 岑巩县| 山西省| 黄平县| 曲靖市| 岗巴县| 泽普县| 黄梅县| 宁津县| 咸宁市| 东台市| 梨树县| 贵定县| 延长县| 临潭县| 南康市| 澄江县| 富顺县|