Bean 的生命周期
1.根据XML中定义的Bean,调用无参构造方法。
2.设置Bean的属性。
3.检查是否实现各种Aware接口,依次是 BeanNameAware, BeanFactoryAware, ApplicationContextAware。若有,则调用相关接口的方法, 即:setBeanName(String beanName),setBeanFactory(BeanFactory beanFactory),setApplicationContext(ApplicationContext applicationContext)
4.检查是否实现各种BeanPostProcessor. 先调用其postProcessBeforeInitialization,更改Bean的属性。
5.检查是否实现Initializating接口,若有,调用其afterPropertiesSet()方法。
6.检查XML中Bean是否有init-method方法,若有,调用之。
7.检查是否实现各种BeanPostProcessor. 先调用其postProcessAfterInitialization,更改Bean的属性。
8.应用程序可以使用这个Bean
9.检查是否实现DisposableBean接口,若有,调用其destory方法。
10.检查XML中Bean 是否有destroy-method,若有,调用之。
下面的例子剔除各种BeanPostProcessor, 看看效果。
package com.spring.ioc.springBeanTest.lifeCycle;
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;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author qsk
*/
public class Person implements BeanNameAware,
BeanFactoryAware,
ApplicationContextAware,
InitializingBean,
DisposableBean {
private String name;
private String address;
private int phone;
private String beanName;
private BeanFactory beanFactory;
private ApplicationContext applicationContext;
public Person() {
System.out.println("【构造器】调用Person的构造器实例化");
}
public Person(String name, String address, int phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
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 + "]";
}
// 这是BeanNameAware接口方法
@Override
public void setBeanName(String arg0) {
System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
this.beanName = arg0;
}
// 这是BeanFactoryAware接口方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
this.beanFactory = arg0;
}
// 这是ApplicationContext接口方法
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
System.out.println("【ApplicationContextAware】 调用setApplicationContext方法" + applicationContext);
}
// 这是InitializingBean接口方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
}
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
}
// 这是DiposibleBean接口方法
@Override
public void destroy() throws Exception {
System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
}
package com.spring.ioc.springBeanTest.lifeCycle;
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("beanLifeCyclebeans.xml");
System.out.println("容器初始化成功");
System.out.println("现在开始关闭容器!");
((ClassPathXmlApplicationContext) factory).registerShutdownHook();
}
}
<?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="person2" class="com.spring.ioc.springBeanTest.lifeCycle.Person" init-method="myInit"
destroy-method="myDestory" >
<property name="name" value="zz"></property>
</bean>
</beans>
结果如下:
Bean 作用域
singleton:定义bean的范围为每个spring容器一个实例(默认值);容器启动初始化就同时自动创建了一个bean的对象。
prototype:定义bean可以被多次实例化(使用一次就创建一次);创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
有例为证,Person类和测试类还是上面的代码,XML更改如下:
<?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="person2" class="com.spring.ioc.springBeanTest.lifeCycle.Person" init-method="myInit"
destroy-method="myDestory" >
<property name="name" value="zz"></property>
</bean>
<bean id="person3" class="com.spring.ioc.springBeanTest.lifeCycle.Person" init-method="myInit"
destroy-method="myDestory" scope="prototype" >
<property name="name" value="zz3"></property>
</bean>
</beans>
效果如下:
可以看到定义scope为prototype的person3并没有在容器加载的时候被调用,仅仅是singleton的实例,在容器加载的时候被调用。
更改测试类如下:
package com.spring.ioc.springBeanTest.lifeCycle;
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("beanLifeCyclebeans.xml");
System.out.println("容器初始化成功");
// 得到Person,并使用
System.out.println();
Person person3 = factory.getBean("person3", Person.class);
System.out.println(person3);
System.out.println("现在开始关闭容器!");
((ClassPathXmlApplicationContext) factory).registerShutdownHook();
}
}
效果如下:
可以看到,仅仅在显示getBean调用时,prototype的person3才会真正初始。
接下来,继续修改Person类 ,
package com.spring.ioc.springBeanTest.lifeCycle;
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("beanLifeCyclebeans.xml");
System.out.println("容器初始化成功");
// 得到Person,并使用
Person person2 = factory.getBean("person2", Person.class);
System.out.println(person2);
System.out.println();
Person person22 = factory.getBean("person2", Person.class);
System.out.println(person22);
System.out.println();
Person person3 = factory.getBean("person3", Person.class);
System.out.println(person3);
System.out.println();
Person person32 = factory.getBean("person3", Person.class);
System.out.println(person32);
System.out.println("\nSingleton ");
System.out.println(person2 == person22);
System.out.println("\nPrototype");
System.out.println(person3 == person32);
System.out.println("现在开始关闭容器!");
((ClassPathXmlApplicationContext) factory).registerShutdownHook();
}
}
效果如下:
Referrence: