程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring Bean的性命周期具體引見

Spring Bean的性命周期具體引見

編輯:關於JAVA

Spring Bean的性命周期具體引見。本站提示廣大學習愛好者:(Spring Bean的性命周期具體引見)文章只能為提供參考,不一定能成為您想要的結果。以下是Spring Bean的性命周期具體引見正文


Spring作為以後Java最風行、最壯大的輕量級框架,遭到了法式員的熱鬧迎接。精確的懂得Spring Bean的性命周期長短常需要的。我們平日應用ApplicationContext作為Spring容器。這裡,我們講的也是 ApplicationContext中Bean的性命周期。而現實上BeanFactory也是差不多的,只不外處置器須要手動注冊。

1、性命周期流程圖:

  Spring Bean的完全性命周期從創立Spring容器開端,直到終究Spring容器燒毀Bean,這個中包括了一系列症結點。

 

若容器注冊了以上各類接口,法式那末將會依照以上的流程停止。上面將細心講授各接口感化。

2、各類接口辦法分類

Bean的完全性命周期閱歷了各類辦法挪用,這些辦法可以劃分為以下幾類:

1、Bean本身的辦法  :  這個包含了Bean自己挪用的辦法和經由過程設置裝備擺設文件中<bean>的init-method和destroy-method指定的辦法

2、Bean級性命周期接口辦法  :  這個包含了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean這些接口的辦法

3、容器級性命周期接口辦法  :  這個包含了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 這兩個接話柄現,普通稱它們的完成類為“後處置器”。

4、工場後處置器接口辦法  :  這個包含了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等異常有效的工場後處置器  接口的辦法。工場後處置器也是容器級的。在運用高低文拆卸設置裝備擺設文件以後立刻挪用。  

3、演示

我們用一個簡略的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 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完成類結構器!!
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 hierarchy
InstantiationAwareBeanPostProcessor挪用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 性命周期的材料整頓,後續持續彌補相干材料,感謝年夜家對本站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved