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[email protected]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.s[email protected]25ff3700: defining beans [hello,com.spring.test.BeanPostProcessor_Imp#0]; root of factory hierarchy 4 构造方法 5 执行前 6 initializing 7 init 8 执行后 9 王涛:hahahah