通過例子來說明Spring Bean的生命周期
通過一個例子來說明spring bean的生命周期,在這裡僅以普通的BeanFactory來說明。
整個IOC的過程分以下幾步:1、xml定位與載入; 2、beanDefinitions的解析與生成;3、getBean(這裡分為bean的實例化、依賴注入、初始化三個部分組成)。明白了上面的幾點,再看源碼會覺得簡單些。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <bean id="hellobean" class="com.spring.ioc.demo.HelloBean" init-method="init"> <property name="name" value="gaofla"></property> </bean></beans>
/** * * define bean * */public class HelloBean { private String name; // 構造函數 public HelloBean() { System.out.println("hello constructor"); } static { System.out.println("hello static block"); } public void init() { System.out.println("hello init"); } public void say() { System.out.println("hello " + name); } public String getName() { return name; } public void setName(String name) { System.out.println("set name value =" + name); this.name = name; }}
import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;/** * * define BeanPostProcessor * */public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("BeanPostProcessor,對象" + beanName + "調用初始化方法之前的數據: " + bean.toString()); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("BeanPostProcessor,對象" + beanName + "調用初始化方法之後的數據:" + bean.toString()); return bean; }}
import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.support.AbstractBeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;public class SpringIocDemo { public static void main(String args[]) { // 讀取xml配置文件,Resource介面主要與文件打交道 ClassPathResource resource = new ClassPathResource("spring.xml"); // XmlBeanFactory = DefaultListableBeanFactory + XmlBeanDefinitionReader // 調用XmlBeanDefinitionReader的loadBeanDefinitions()方法,此時只是解釋成BeanDefinition, // 並載入到beanDefinitionMap中 XmlBeanFactory factory = new XmlBeanFactory(resource); MyBeanPostProcessor beanPostProcessor = new MyBeanPostProcessor(); ((AbstractBeanFactory) factory).addBeanPostProcessor(beanPostProcessor); // 到這裡還沒有實例成bean,普通的BeanFactory不管你是否設置lazy-init屬性,都不會實例化bean,只有在getBean時才會實例化 // 先執行靜態代碼塊-->構造函數--->set注入方法--->BeanPostProcessor // beforeInit()方法--->init()方法--->BeanPostProcessor afterInit() // --->調用bean中的方法 // 先createBean()創建bean,然後populateBean()依賴注入,initializeBean()調用beanPostProcessor及init()方法 HelloBean helloBean = (HelloBean) factory.getBean("hellobean"); // 調用bean的方法 helloBean.say(); }}
輸出結果如下:
hello static block
hello constructorset name value =gaoflaBeanPostProcessor,對象hellobean調用初始化方法之前的數據: com.spring.ioc.demo.HelloBean@7426dfa5hello init
BeanPostProcessor,對象hellobean調用初始化方法之後的數據:com.spring.ioc.demo.HelloBean@7426dfa5hello gaofla推薦閱讀:
※Spring AOP,IOC,MVC的基本原理
※為什麼要用spring的IOC和AOP?用了IOC和AOP的優點缺點是什麼