Bean的生命周期

来源:互联网 发布:辽宁12选5最大遗漏数据 编辑:程序博客网 时间:2024/05/03 05:11
1,Bean的生命周期:
  (1)实例化(必须的)构造函数构造对象
  (2)装配(可选的)为属性赋值
  (3)回调(可选的)(容器-控制类和组件-回调类)
  (4)初始化(init-method=" ")
    (5)就绪
    (6)销毁(destroy-method=" ")

Spring中Bean的生命周期 

在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生
一个新的对象
使用Singleton模式产生单一实例,对单线程的程序说并不会有什么问题,但对于多线程的程序,就必须注意安全(Thread-safe)的议题,防止多个线程
同时存取共享资源所引发的数据不同步问题。
然而在spring中 可以设定每次从BeanFactory或ApplicationContext指定别名并取得Bean时都产生一个新的实例:例如:
<bean singleton="false">
在spring中,singleton属性默认是true,只有设定为false,则每次指定别名取得的Bean时都会产生一个新的实例
一个Bean从创建到销毁,如果是用BeanFactory来生成,管理Bean的话,会经历几个执行阶段:
    1:Bean的建立:
          有BeanFactory读取Bean定义文件,并生成各个Bean实例
    2:属性注入:
          执行相关的Bean属性依赖注入
    3:BeanNameAware的setBeanName():
          如果Bean类有实现org.springframework.beans.BeanNameAware接口,则执行它的setBeanName()方法
    4:BeanFactoryAware的setBeanFactory():
          如果Bean类有实现org.springframework.beans.factory.BeanFactoryAware接口,则执行它的setBeanFactory()方法
    5:BeanPostProcessors的ProcessBeforeInitialization()
          如果任何的org.springframework.beans.factory.config.BeanPostProcessors实例与Bean实例相关。则执行BeanPostProcessors实例
          的processBeforeInitialization()方法
    6:initializingBean的afterPropertiesSet():
          如果Bean类已实现org.springframework.beans.factory.InitializingBean接口,则执行他的afterProPertiesSet()方法
    7:Bean定义文件中定义init-method:
          可以在Bean定义文件中使用"init-method"属性设定方法名称例如:
          <bean calss="onlyfun.caterpillar.HelloBean" init-method="initBean">
          如果有以上设置的话,则执行到这个阶段,就会执行initBean()方法
    8:BeanPostProcessors的ProcessaAfterInitialization()
          如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessors实例的ProcessaAfterInitialization()方法
    9:DisposableBean的destroy()
          在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法
    10:Bean定义文件中定义destroy-method
          在容器关闭时,可以在Bean定义文件中使用"destroy-method"属性设定方法名称,例如:
          <bean destroy-method="destroyBean">
          如果有以上设定的话,则进行至这个阶段时,就会执行destroyBean()方法,如果是使用ApplicationContext来生成并管理Bean的话
          则稍有不同,使用ApplicationContext来生成及管理Bean实例的话,在执行BeanFactoryAware的setBeanFactory()阶段后,若Bean
          类上有实现org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才执行
          BeanPostProcessors的ProcessBeforeInitialization()及之后的流程 

<!-- 测试bean对象生存周期管理 -->
<bean id = "examObj1" class="day01.ExampleBean" init-method="init" destroy-method="destroy"/>
<!-- 原型的对象destory-method无效! -->
<bean id="examObj2" scope="prototype" class="day01.ExampleBean" init-method="init" destroy-method="destroy"/>

创建类ExampleBean
public class ExampleBean implements Serializable{
String n;
public ExampleBean(){
n="Tom";
System.out.println("Call ExampleBean()");
}

public String getName(){
return n;
}

public void setName(String n){
this.n = n;
}

public String toString(){
return "n="+n;
}

public void init(){
System.out.println("call init()");
}

public void destroy(){
System.out.println("call destroy()");
}
}
测试
Test
public class Test{
public static void main(String [] args){
System.out.println(1.....);
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(2.....);
}
}
Spring bean生命周期的运行过程练习
创建beantest类
public class beantest implements BeanNameAware, BeanFactoryAware,
InitializingBean, DisposableBean, BeanPostProcessor,
BeanFactoryPostProcessor {
private String name;


public void setName(String name) {
System.out.println("2.设置相关的属性...");
this.name = name;
}


public beantest() {
System.out.println("1.构造方法...");
}


private void init() {
System.out.println("7.init初始化...");
}


@Override
public void setBeanName(String name) {
System.out.println("3.设置bean的名字:" + name + "...");
}


@Override
public void setBeanFactory(BeanFactory factory) {
System.out.println("4.创建bean的Factory...");
}


@Override
public void afterPropertiesSet() {
System.out.println("6.预处理它是processor的before后执行,和processor拥有的功能");
}


@Override
public void destroy() throws Exception {
System.out.println("9.销毁");
}


public void destory() {
System.out.println("10.自定义销毁");
}


@Override
public Object postProcessAfterInitialization(Object bean,String beanName)throws BeansException{
System.out.println("8." +"BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName+
"   bean处理器:bean创建之后也可以称之为后预处理");
if(bean instanceof beantest){
System.out.println("bean instanceof BeanTest监控到bean,业务处理");
}
return bean;
}


@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("5." +"BeanPostProcessor interface postProcessBeforeInitialization method :" + beanName+
"   bean处理器:bean创建之前");
return bean;
}


@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("--->开始BeanFactoryPostProcessorTest");
String[] names = beanFactory.getBeanDefinitionNames();
for (String name : names) {
System.out.println("definition bean name:" + name);
}
System.out.println("---> 结束BeanFactoryPostProcessorTest");
}
}
配置文件beanapplication.xml
<!-- 生命周期运行过程练习 -->
<bean id="beantest" class="Springbeantest.beantest" init-method="init" destroy-method="destory">
<property name="name" value = "值"></property>
</bean>
<bean id="OtherBean" class="Springbeantest.OtherBean"></bean>


测试testBean类
public class testBean {
public static void main(String[] args){
ApplicationContext conn = new ClassPathXmlApplicationContext("beanapplication.xml");
System.out.println("1...");

beantest bea = (beantest) conn.getBean("beantest");
OtherBean other = (OtherBean) conn.getBean("OtherBean");
//((AbstractApplicationContext) conn).registerShutdownHook();
// ((AbstractApplicationContext) conn).destroy();//需自行调销毁方法

}
}
运行结果
1.构造方法...
2.设置相关的属性...
3.设置bean的名字:beantest...
4.创建bean的Factory...
6.预处理它是processor的before后执行,和processor拥有的功能
7.init初始化...
--->开始BeanFactoryPostProcessorTest
definition bean name:beantest
definition bean name:OtherBean
---> 结束BeanFactoryPostProcessorTest
5.BeanPostProcessor interface postProcessBeforeInitialization method :OtherBean   bean处理器:bean创建之前
8.BeanPostProcessor interface postProcessAfterInitialization method; :OtherBean   bean处理器:bean创建之后也可以称之为后预处理
1...
9.销毁
10.自定义销毁


问题:BeanFactoryPostProcessor接口的方法运行有先后问题,销毁destroy方法没运行需调用

applicationContext中的Bean和Spring中相同么

0 0
原创粉丝点击