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.

上述文字的图如下所示:

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

BeanPostProcessor的两步。如下图所示:

 

 

代码的目录结构如下:

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的代码如下:

 1 package com.qls.beanlife2;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.*;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.ApplicationContextAware;
 7 
 8 /**
 9  * Created by ${秦林森} on 2017/6/6.
10  */
11 public class StudentService implements BeanNameAware ,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
12     private String name;
13 
14     public String getName() {
15         return name;
16     }
17 
18     public void setName(String name) {
19         this.name = name;
20         System.out.println("2.Populate Properties");
21     }
22 
23     public StudentService() {
24         System.out.println("1.Instantiate");
25     }
26 
27     @Override
28     public void setBeanName(String name) {
29         System.out.println("3.BeanNameAware's setBeanName    the bean name is :"+name);
30     }
31 
32     @Override
33     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
34         System.out.println("4.BeanFactoryAware's setBeanFactory   the bean factory name is: "+beanFactory);
35     }
36     @Override
37     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
38         System.out.println("5.ApplicationContextAware's setApplicationContext the applicationContext is: "+applicationContext);
39     }
40 
41     @Override
42     public void afterPropertiesSet() throws Exception {
43         System.out.println("7.InitializingBean's afterPropertiesSet ");
44     }
45     public void hello(){
46         System.out.println("hello");
47     }
48 
49     @Override
50     public void destroy() throws Exception {
51         System.out.println("destroy");
52     }
53     public void myDestroy(){
54         System.out.println("my destroy");
55     }
56 }

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();*/ } }

 

posted @ 2017-06-06 22:40  技术让世界更精彩  阅读(329)  评论(0编辑  收藏  举报