先贴代码:
Life类
package com.open.bean;
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;
public class Life implements BeanFactoryAware, BeanNameAware,
        InitializingBean, DisposableBean 
{
    
private String msg;
    
public Life() {
        System.out.println(
"msg="+msg);
        System.out.println(
"构造函数");
    }

    
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(
"setBeanFactory");
    }

    
public void setBeanName(String name) {
        System.out.println(
"setBeanName");
    }

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

    
public Object postProcessBeforeInitialization(Object bean, String beanName)
            
throws BeansException {
        System.out.println(
"postProcessBeforeInitialization");
        
return null;
    }

    
public Object postProcessAfterInitialization(Object bean, String beanName)
            
throws BeansException {
        System.out.println(
"postProcessAfterInitialization");
        
return null;
    }

    
public void afterPropertiesSet() throws Exception {
        System.out.println(
"afterPropertiesSet");
    }

    
public void destroy() throws Exception {
        System.out.println(
"destroy");
    }

    
public String getMsg() {
        
return msg;
    }

    
public void setMsg(String msg) {
        
this.msg = msg;
    }

}
BeanPostProcessorImp类
package com.open.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanPostProcessorImp implements BeanPostProcessor {
    
public Object postProcessBeforeInitialization(Object bean, String beanName)
            
throws BeansException {
        System.out.println(
"postProcessBeforeInitialization");
        
return bean;
    }

    
public Object postProcessAfterInitialization(Object bean, String beanName)
            
throws BeansException {
        System.out.println(
"postProcessAfterInitialization");
        
return bean;
    }

}
BeanCounter类
package com.open.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class BeanCounter implements BeanFactoryPostProcessor {
    
public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) 
throws BeansException {
        System.out.println(
"类的数量="+beanFactory.getBeanDefinitionCount());
    }

}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
<bean id="life" name="life_name" class="com.open.bean.Life" 
        init
-method="init">
        
<property name="msg" value="lifexxxx"/>
    
</bean>    
    
<bean id="processor" class="com.open.bean.BeanPostProcessorImp"/>
    
<bean id="counter" class="com.open.bean.BeanCounter"/>
</beans>
测试类
package com.open.bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    
public static void main(String[] args) {
        ClassPathXmlApplicationContext cx
=
            
new ClassPathXmlApplicationContext("bean.xml");
         Life life
=(Life)cx.getBean("life");     
    }

}
    
输出结果
类的数量=3
msg
=null
构造函数
setBeanName
setBeanFactory
postProcessBeforeInitialization
afterPropertiesSet
初始化
postProcessAfterInitialization
解释:
调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法
-->实例化,调用构造函数-->设置属性值
-->调用BeanNameAware的setBeanName()方法
-->调用BeanFactoryAware接口的setBeanFactory
-->调用BeanPostProcessor接口的postProcessBeforeInitialization()
-->调用InitializingBean接口的afterPropertiesSet()
-->调用bean.xml中制定的init-method指定init()方法
-->调用BeanPostProcessor接口的postProcessAfterInitialization()
容器关闭
-->DisposableBean接口的destroy()
-->调用bean.xml中制定的destroy-method指定的go()方法