spring中bean初始化過程

來源:互聯網
上載者:User

在傳統的Java應用中,Bean的生命週期非常簡單。Java的關鍵詞new用來執行個體化Bean(或許他是非序列化的)。這樣就夠用了。相反,Bean 的生命週期在Spring容器中更加細緻。理解Spring Bean的生命週期非常重要,因為你或許要利用Spring提供的機會來訂製Bean的建立過程。

1. 容器尋找Bean的定義資訊並且將其執行個體化。
2.受用依賴注入,Spring按照Bean定義資訊配置Bean的所有屬性。
3.如果Bean實現了BeanNameAware介面,工廠調用Bean的setBeanName()方法傳遞Bean的ID。
4.如果Bean實現了BeanFactoryAware介面,工廠調用setBeanFactory()方法傳入工廠自身。
5.如果BeanPostProcessor和Bean關聯,那麼它們的postProcessBeforeInitialzation()方法將被調用。
6.如果Bean指定了init-method方法,它將被調用。
7.最後,如果有BeanPsotProcessor和Bean關聯,那麼它們的postProcessAfterInitialization()方法將被調用。
    到這個時候,Bean已經可以被應用系統使用了,並且將被保留在Bean Factory中知道它不再需要。有兩種方法可以把它從Bean Factory中刪除掉。
1.如果Bean實現了DisposableBean介面,destory()方法被調用。
2.如果指定了訂製的銷毀方法,就調用這個方法。
    Bean在Spring應用內容相關的生命週期與在Bean工廠中的生命週期只有一點不同,唯一不同的是,如果Bean實現了ApplicationContextAwre介面,setApplicationContext()方法被調用。

1. 容器尋找Bean的定義資訊並且將其執行個體化。
2. 受用依賴注入,Spring按照Bean定義資訊配置Bean的所有屬性。
3. 如果Bean實現了BeanNameAware介面,工廠調用Bean的setBeanName()方法傳遞Bean的ID。
4. 如果Bean實現了BeanFactoryAware介面,工廠調用setBeanFactory()方法傳入工廠自身。
5. 如果BeanPostProcessor和Bean關聯,那麼它們的postProcessBeforeInitialzation()方法將被調用。
6. 如果Bean指定了init-method方法,它將被調用。
7. 最後,如果有BeanPsotProcessor和Bean關聯,那麼它們的postProcessAfterInitialization()方法將被調用。 

 到這個時候,Bean已經可以被應用系統使用了,並且將被保留在Bean Factory中知道它不再需要。有兩種方法可以把它從Bean Factory中刪除掉。

1.如果Bean實現了DisposableBean介面,destory()方法被調用。
2.如果指定了訂製的銷毀方法,就調用這個方法。 

 Bean在Spring應用內容相關的生命週期與在Bean工廠中的生命週期只有一點不同,唯一不同的是,如果Bean實現了ApplicationContextAwre介面,setApplicationContext()方法被調用。 

 

舉例:(這裡只是示範Bean工廠的例子)
1.HelloWorld.java

Java代碼
  1. package com.spring.lifecycle;   
  2.   
  3. import org.springframework.beans.BeansException;   
  4. import org.springframework.beans.factory.BeanFactory;   
  5. import org.springframework.beans.factory.BeanFactoryAware;   
  6. import org.springframework.beans.factory.BeanNameAware;   
  7. import org.springframework.beans.factory.DisposableBean;   
  8. import org.springframework.beans.factory.InitializingBean;   
  9. import org.springframework.beans.factory.config.BeanPostProcessor;   
  10.   
  11. public class HelloWorld implements BeanNameAware,BeanFactoryAware, BeanPostProcessor,InitializingBean,DisposableBean{   
  12.   
  13.     private String hello;   
  14.   
  15.     public void setBeanName(String arg0) {   
  16.         System.out.println("調用BeanNameAware的setBeanName()..." );   
  17.     }   
  18.   
  19.     public String getHello() {   
  20.         return hello;   
  21.     }   
  22.   
  23.     public void setHello(String hello) {   
  24.         this.hello = hello;   
  25.         System.out.println("調用setHello()..." );   
  26.     }   
  27.   
  28.     public void customInit() {   
  29.         System.out.println("調用customInit()...");   
  30.     }   
  31.   
  32.     public void customDestroy() {   
  33.         System.out.println("調用customDestroy()...");   
  34.     }   
  35.   
  36.     public Object postProcessAfterInitialization(Object arg0, String arg1)   
  37.             throws BeansException {   
  38.         System.out.println("調用BeanPostProcessor的postProcessAfterInitialization()...");   
  39.         return null;   
  40.     }   
  41.   
  42.     public Object postProcessBeforeInitialization(Object arg0, String arg1)   
  43.             throws BeansException {   
  44.         System.out.println("調用BeanPostProcessor的postProcessBeforeInitialization()...");   
  45.         return null;   
  46.     }   
  47.   
  48.     public void destroy() throws Exception {   
  49.         System.out.println("調用DisposableBean的destory()...");   
  50.     }   
  51.   
  52.     public void afterPropertiesSet() throws Exception {   
  53.         System.out.println("調用InitializingBean的afterPropertiesSet()...");   
  54.     }   
  55.   
  56.     public void setBeanFactory(BeanFactory arg0) throws BeansException {   
  57.         System.out.println("調用BeanFactoryAware的setBeanFactory()...");   
  58.     }   
  59. }  

2.測試程式

Java代碼
  1. package com.spring.springtest;   
  2.   
  3. import junit.framework.TestCase;   
  4.   
  5. import org.springframework.beans.factory.BeanFactory;   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  7.   
  8. import com.spring.lifecycle.HelloWorld;   
  9.   
  10. public class TestLifeCycle extends TestCase {   
  11.     private BeanFactory bf;   
  12.   
  13.     protected void setUp() throws Exception {   
  14.         bf = new ClassPathXmlApplicationContext("applicationContext.xml");   
  15.     }   
  16.   
  17.     public void testLifeCycle() throws Exception {   
  18.         HelloWorld hello = (HelloWorld) bf.getBean("helloWorld");   
  19.         assertEquals("hello world!", hello.getHello());   
  20.         hello.destroy();   
  21.     }   
  22. }  

3.applicationContext.xml

Xml代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="helloWorld" class="com.spring.lifecycle.HelloWorld"  
  5.         init-method="customInit" destroy-method="customDestroy">  
  6.         <property name="hello" value="hello world!"></property>  
  7.     </bean>  
  8. </beans>  

4.運行結果:

引用調用setHello()...
調用BeanNameAware的setBeanName()...
調用BeanFactoryAware的setBeanFactory()...
調用InitializingBean的afterPropertiesSet()...
調用customInit()...
調用DisposableBean的destory()...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.