spring in action 学习笔记四:bean的生命周期

 bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期。

首先介绍一下:ApplicationContext的容器的bean的生命周期:

一共13步步骤如下:

Instaniate--->Populate properties--->BeanNameAware's  setBeanName--->BeanFactoryAware's setBeanFactory-->ApplicationContextAware's setApplicationContext-->pre-Initialization BeanPostProcessor --->InitializingBean's afterPropertiesSet--->call custom init-method-->post-Initialization BeanPostProcessor-->bean is ready to use-->container is shutdown-->DisposableBean's destory-->call custom destory-method.

上述文字的图如下所示:

spring in action 学习笔记四:bean的生命周期

其次介绍一下:BeanFactory的bean的生命周期,它的生命周期只是比ApplicationContext的bean的生命周期少了三步:一步是:ApplicationContextAware,另外两步是:

BeanPostProcessor的两步。如下图所示:

spring in action 学习笔记四:bean的生命周期

代码的目录结构如下:

spring in action 学习笔记四:bean的生命周期

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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="com.qls.beanlife2.StudentService" >
<property name="name" value="熊二"/>
</bean>
<bean id="myBeanPostProcessor" class="com.qls.beanlife2.MyBeanPostProcessor"/>
</beans>

StudentService的代码如下:

 package com.qls.beanlife2;

 import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class StudentService implements BeanNameAware ,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
System.out.println("2.Populate Properties");
} public StudentService() {
System.out.println("1.Instantiate");
} @Override
public void setBeanName(String name) {
System.out.println("3.BeanNameAware's setBeanName the bean name is :"+name);
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("4.BeanFactoryAware's setBeanFactory the bean factory name is: "+beanFactory);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
System.out.println("5.ApplicationContextAware's setApplicationContext the applicationContext is: "+applicationContext);
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("7.InitializingBean's afterPropertiesSet ");
}
public void hello(){
System.out.println("hello");
} @Override
public void destroy() throws Exception {
System.out.println("destroy");
}
public void myDestroy(){
System.out.println("my destroy");
}
}

MyBeanPostProcessor的代码如下:

package com.qls.beanlife2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("6.pre-initialization BeanPostProcessor");
return beanName;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return beanName;
}
}

StudentTest的代码如下:

package com.qls.beanlife2;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class StudentTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/qls/beanlife2/beans.xml");//这个是测试ApplicationContext的bean的生命周期
/*
、//这个是测试BeanFactory的bean的生命周期。
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/qls/beanlife2/beans.xml"));
StudentService studentService= (StudentService) factory.getBean("studentService");
studentService.hello();*/
}
}
上一篇:spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象


下一篇:PowerPoint2003/2007幻灯片常用快捷键大全