1. 程式人生 > >Spring bean的生命周期詳解

Spring bean的生命周期詳解

root bsp ram odi www rop ssp package etag

bean的生命周期
1.實例化bean 即new
2.按照spring上下文對實例化的bean進行配置 即填充屬性,也就是IOC/DI(控制反轉,依賴註入)
3.如果這個bean實現了BeanNameAware接口,Spring會調用它實現的setBeanName()方法,參數是bean的ID,即Spring將bean的ID傳遞給setBeanName()方法。(讓bean知道自己是誰,即自己的ID)
4.如果bean實現了BeanFactoryAware接口,Spring將調用setBeanFactory(BeanFactory factory)方法,將BeanFactory容器實例傳入;(即知道bean自己屬於哪個工廠)
5.如果Bean實現了ApplicationContextAware接口,Spring將調用setApplicationContext(ApplicationContext context)方法,傳入Spring上下文。
6.如果Bean實現了BeanPostProcessor接口,將會調用postProcessBeforeInialization(Object obj,String s)方法。BeanPostProcessor經常被用作是Bean內容的更改。
7.如果這個Bean在Spring配置文件中配置了init-method屬性會自動調用其配置的初始化方法。
8.如果這個Bean實現了BeanPostProcessor接口,將會調用postAfterInitialization(Object obj,String s)方法
備註:當以上工作完成後就可以使用這個Bean了,這個bean是single的,一般調用同一個ID的bean會是在內容地址相同的實例
9.這個Bean會一直留在應用上下文中(ApplicationContext),直到該應用上下文被銷毀。
10.如果這個Bean實現了DisposableBean接口,會調用destroy()方法;如果Bean在Spring配置中配置了destroy-method屬性,會自動調用其配置的銷毀方法。

在Spring框架中,bean的定義,從編寫到配置再到最終的getbean調用,框架都有相應的實現規則,具體如下所述。

bean的定義:

 1 package com.spring.beans;
 2 
 3 import javax.ejb.Init;
 4 
 5 import org.springframework.beans.factory.InitializingBean;
 6 
 7 public class HelloBean implements InitializingBean {
 8 
 9     public HelloBean() {
10         System.out.println("構造方法");
11 } 12 13 private String name; 14 private String nullTest; 15 16 private int age; 17 18 public String getNullTest() { 19 return nullTest; 20 } 21 22 public void setNullTest(String nullTest) { 23 this.nullTest = nullTest; 24 } 25 26 public String getName() {
27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public int getAge() { 35 return age; 36 } 37 38 public void setAge(int age) { 39 this.age = age; 40 } 41 42 public void prints(String str) { 43 System.out.println(str + ":hahahah"); 44 } 45 46 public void init() { 47 System.out.println("init"); 48 } 49 50 @Override 51 public void afterPropertiesSet() throws Exception { 52 // TODO Auto-generated method stub 53 System.out.println("initializing"); 54 } 55 }

BeanPostProcessor定義:

 1 package com.spring.test;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.config.BeanPostProcessor;
 5 
 6 public class BeanPostProcessor_Imp implements BeanPostProcessor {
 7 
 8     @Override
 9     public Object postProcessAfterInitialization(Object arg0, String arg1)
10             throws BeansException {
11         System.out.println("執行後");
12         return arg0;
13     }
14 
15     @Override
16     public Object postProcessBeforeInitialization(Object arg0, String arg1)
17             throws BeansException {
18         System.out.println("執行前");
19         return arg0;
20     }
21 
22 }

測試類的定義:

 1 package com.spring.test;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 import org.springframework.core.io.FileSystemResource;
 8 import org.springframework.core.io.Resource;
 9 
10 import com.spring.beans.BigBearBean;
11 import com.spring.beans.HelloBean;
12 import com.spring.beans.List_Map_Bean;
13 import com.spring.beans.smallBearBean;
14 import com.spring.interfaces.Animal;
15 
16 public class HelloTest {
17     public static void main(String[] args) {
18         ApplicationContext ctx = new ClassPathXmlApplicationContext(
19                 "hellotest.xml");
20         HelloBean HB = (HelloBean) ctx.getBean("hello");
21         HB.prints("王濤");
22         System.out.println(HB.getName() + "\n------------");
23     }
24 
25 }

配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
 8         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10 
11 
12     <bean id="hello" class="com.spring.beans.HelloBean" init-method="init">
13         <property name="name">
14             <value><![CDATA[<wb<t>]]></value>
15         </property>
16         <property name="age" value="23" />
17         <property name="nullTest">
18             <value></value>
19         </property>
20     </bean>
21     <bean class="com.spring.test.BeanPostProcessor_Imp"></bean>
22 </beans>

運行結果:

1 15:38:12,269 INFO  [context.support.ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61a48515: startup date [Fri Jun 23 15:38:12 CST 2017]; root of context hierarchy
2 15:38:12,343 INFO  [factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [hellotest.xml]
3 15:38:12,588 INFO  [factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25ff3700: defining beans [hello,com.spring.test.BeanPostProcessor_Imp#0]; root of factory hierarchy
4 構造方法
5 執行前
6 initializing
7 init
8 執行後
9 王濤:hahahah

Spring bean的生命周期詳解