Spring Bean生命周期多个bean对象周期顺序

Spring ApplicationContext容器的Bean的生命周期有13步,而后三步容器关闭和实现接口的destroy()方法和自定义的销毁方法是不可见的。

在前九步【new ClassPathXmlApplicationContext("xx.xml"),加载容器装配bean】,每个bean对象的生命周期是并列的,按照beans.xml配置文件的配置顺序,先完成一个bean的九步,然后是下一个bean对象的九步。而第十步使用bean 【getBean("id")】bean对象都完成9步才会调用。

以下是部分代码

Person和Employee类

package top.chgl16.myspring.beanlife;

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.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 
 * @Lin 2018-5-15
 * 测试全部周期的一个bean
 *
 */
public class Person implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
	public String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		System.out.println("bean生命周期第二步, 属性注入,setXxx()方法");
	}
	
	public Person() {
		System.out.println("bean生命周期第一步, 实例化对象,调用了Person的构造函数");
	}

	public void setBeanName(String arg0) {
		// bean生命周期第三步,BeanNameAware接口,实现其setBeanName方法, arg0是Bean的name/id
		System.out.println("bean生命周期第三步,BeanNameAware接口,实现其setBeanName方法    arg0: " + arg0);
	}

	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		// bean生命周期第四步,BeanFactoryAware接口,实现其setBeanFactory方法, arg0是BeanFactory对象
		System.out.println("bean生命周期第四步,BeanFactoryAware接口,实现其setBeanFactory方法, arg0: " + arg0);
		
	}

	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		// bean生命周期第五步,ApplicationContextAware接口,实现其setApplicationContext方法, arg0是ApplicationContext对象
		System.out.println("bean生命周期第五步,ApplicationContextAware接口,实现其setApplicationContext方法, arg0: " + arg0);
		
	}

	/*
	public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization");
		return null;
	}

	public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization");
		return null;
	}
	*/
	public void init() {
		System.out.println("bean生命周期第八步,自定义的初始化方法,在配置中 init-method=\"init\"");
	}
	
	public void afterPropertiesSet() throws Exception {
		// 不具备AOP
		System.out.println("bean生命周期第七步,InitializingBean 接口");
	}

	public void destroy() throws Exception {
		// 可以关闭数据连接,等。。但是看不到显示,定制的方法可以显示
		System.out.println("bean生命周期第十二步,调用DisposableBean接口的destroy()方法");
	}
	
	// 定制的销毁方法
	public void myDestroy() {
		// 自定义的销毁方法,也是看不到的  最后一步
		System.out.println("bean生命周期第十三步,调用自定义的方法 ,在配置中 destroy-method=\\\"myDestroy\\\"");
	}
}
 
 
 
 
package top.chgl16.myspring.beanlife;

/**
 * 
 * @Lin 2018-5-15
 * 为了测试的草稿bean类
 *
 */

public class Employee {
	public String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Employee() {
		System.out.println("bean生命周期第一步, 实例化对象,调用了Employee的构造函数");
	}

}



具有AOP特殊的BeanPostProcessor

package top.chgl16.myspring.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @Lin 2018-5-15
 * 这是另外新建的一个类,来实现BeanPostProcessor接口,Before和After方法具有AOP特性,所以如此关联建立
 * afterPropertiesSet()方法是InitializingBean接口的,周期中排在Before后面,全局比较奇怪,姑还是在相应的Bean类里实现为好
 * 这个类也要配置为Bean
 */

public class MyBeanPostProcessor implements BeanPostProcessor, InitializingBean {

	public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
		System.out.println("bean生命周期第九步,postProcessAfterInitialization 被调用    arg1:" + arg0);
		System.out.println("\n");
		return arg0;    // before和after都要放回 arg0或者对象,弄人 return null, 马上空指针异常
	}

	public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
		System.out.println("bean生命周期第六步,postProcessBeforeInitialization 被调用  arg1:" + arg1);
		return arg0;   // before和after都要放回 arg0或者对象,弄人 return null, 马上空指针异常
	}

	// 这里的这个方法不会被调用,没有aop功能 
	public void afterPropertiesSet() throws Exception {		
		System.out.println("InitializingBean接口, 很奇怪在首先调用了,设置属性前");
	}

}

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" scope="singleton" init-method="init" destroy-method="myDestroy" class="top.chgl16.myspring.beanlife.Person">
        <property name="name" value="chgl16" ></property>        <!-- spring中的类都叫bean,property是给类成员初始化 -->
    </bean>
    <bean id="employee" class="top.chgl16.myspring.beanlife.Employee">
        <property name="name" value="小明员工" ></property>        
    </bean>
    <bean id="myBeanPostProcessor"  scope="prototype" class="top.chgl16.myspring.beanlife.MyBeanPostProcessor" >   <!-- BeanPostProcessor具有AOP性质 -->
    </bean>
    <bean id="myBeanNameAware" class="top.chgl16.myspring.beanlife.MyBeanNameAware" >  <!-- 测试BeanNameAware是否具有AOP性质,没有 -->
    </bean>
</beans>


输出:

五月 17, 2018 5:43:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@433c675d: startup date [Thu May 17 17:43:45 CST 2018]; root of context hierarchy
五月 17, 2018 5:43:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [top/chgl16/myspring/beanlife/beans.xml]
InitializingBean接口, 很奇怪在首先调用了,设置属性前
bean生命周期第一步, 实例化对象,调用了Person的构造函数
bean生命周期第二步, 属性注入,setXxx()方法
bean生命周期第三步,BeanNameAware接口,实现其setBeanName方法    arg0: person
bean生命周期第四步,BeanFactoryAware接口,实现其setBeanFactory方法, arg0: org.springframework.beans.factory.support.DefaultListableBeanFactory@3567135c: defining beans [person,employee,myBeanPostProcessor,myBeanNameAware]; root of factory hierarchy
bean生命周期第五步,ApplicationContextAware接口,实现其setApplicationContext方法, arg0: org.springframework.context.support.ClassPathXmlApplicationContext@433c675d: startup date [Thu May 17 17:43:45 CST 2018]; root of context hierarchy
bean生命周期第六步,postProcessBeforeInitialization 被调用  arg1:person
bean生命周期第七步,InitializingBean 接口
bean生命周期第八步,自定义的初始化方法,在配置中 init-method="init"
bean生命周期第九步,postProcessAfterInitialization 被调用    arg1:top.chgl16.myspring.beanlife.Person@6500df86


bean生命周期第一步, 实例化对象,调用了Employee的构造函数
bean生命周期第六步,postProcessBeforeInitialization 被调用  arg1:employee
bean生命周期第九步,postProcessAfterInitialization 被调用    arg1:top.chgl16.myspring.beanlife.Employee@4cf777e8


测试 MyBeanNameAware, 配置后被当做一个单独的bean而已
bean生命周期第六步,postProcessBeforeInitialization 被调用  arg1:myBeanNameAware
bean生命周期第九步,postProcessAfterInitialization 被调用    arg1:top.chgl16.myspring.beanlife.MyBeanNameAware@2f686d1f


bean生命周期第十步,Hello chgl16

猜你喜欢

转载自blog.csdn.net/chenbetter1996/article/details/80354587