Spring的生命周期(转)

来源:互联网 发布:c语言自学视频教程网站 编辑:程序博客网 时间:2024/06/09 22:46

Spring的生命周期.

 

1.       容器启动,实例化所有实现了BeanFactoyPostProcessor接口的类。他会在任何普通Bean实例化之前加载.

2.       实例化剩下的Bean,对这些Bean进行依赖注入。

3.       如果Bean有实现BeanNameAware的接口那么对这些Bean进行调用

4.       如果Bean有实现BeanFactoryAware接口的那么对这些Bean进行调用

5.       如果Bean有实现ApplicationContextAware接口的那么对这些Bean进行调用

6.       如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessBeforeInitialization方法

7.       如果Bean有实现InitializingBean接口那么对这些Bean进行调用

8.       如果Bean配置有init属性,那么调用它属性中设置的方法

9.       如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessAfterInitialization方法

10.   Bean正常是使用

11.   调用DisposableBean接口的destory方法

12.   调用Bean定义的destory方法

 

如果从大体上区分值分只为四个阶段

1.       BeanFactoyPostProcessor实例化

2.       Bean实例化,然后通过某些BeanFactoyPostProcessor来进行依赖注入

3.       BeanPostProcessor的调用.Spring内置的BeanPostProcessor负责调用Bean实现的接口: BeanNameAware, BeanFactoryAware, ApplicationContextAware等等,等这些内置的BeanPostProcessor调用完后才会调用自己配置的BeanPostProcessor

4.       Bean销毁阶段

 

 

以上是自己总结的,没研究过源码,恐有误…作参考用

以下附上验证的代码:

 

Java代码 复制代码
  1. package mislay;   
  2.   
  3. import org.springframework.beans.BeansException;   
  4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;   
  5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;   
  6.   
  7. public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {   
  8.   
  9.     @Override  
  10.     public void postProcessBeanFactory(   
  11.             ConfigurableListableBeanFactory beanFactory) throws BeansException {   
  12.         System.out.println("--------> begin BeanFactoryPostProcessorTest");   
  13.         String[] names = beanFactory.getBeanDefinitionNames();   
  14.         for (String name : names) {   
  15.             System.out.println("definition bean name:" + name);   
  16.         }   
  17.         System.out.println("<--------- end BeanFactoryPostProcessorTest");   
  18.     }   
  19.   
  20. }  
[java] view plaincopy
  1. package mislay;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;  
  5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  6.   
  7. public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {  
  8.   
  9.     @Override  
  10.     public void postProcessBeanFactory(  
  11.             ConfigurableListableBeanFactory beanFactory) throws BeansException {  
  12.         System.out.println("--------> begin BeanFactoryPostProcessorTest");  
  13.         String[] names = beanFactory.getBeanDefinitionNames();  
  14.         for (String name : names) {  
  15.             System.out.println("definition bean name:" + name);  
  16.         }  
  17.         System.out.println("<--------- end BeanFactoryPostProcessorTest");  
  18.     }  
  19.   
  20. }  

 

Java代码 复制代码
  1. package mislay;   
  2.   
  3. import org.springframework.beans.BeansException;   
  4. import org.springframework.beans.factory.config.BeanPostProcessor;   
  5.   
  6. public class BeanPostProcessorTest implements BeanPostProcessor {   
  7.   
  8.        
  9.     @Override  
  10.     public Object postProcessAfterInitialization(Object bean, String beanName)   
  11.             throws BeansException {   
  12.         System.out.println("call BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName);   
  13.         return bean;   
  14.     }   
  15.   
  16.     @Override  
  17.     public Object postProcessBeforeInitialization(Object bean, String beanName)   
  18.             throws BeansException {   
  19.         System.out.println("call BeanPostProcessor interface postProcessBeforeInitialization method ::" + beanName);   
  20.         if(bean instanceof BeanTest) {   
  21.             System.out.println("bean instanceof BeanTest");   
  22.         }   
  23.         return bean;   
  24.     }   
  25.   
  26. }  
[java] view plaincopy
  1. package mislay;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanPostProcessor;  
  5.   
  6. public class BeanPostProcessorTest implements BeanPostProcessor {  
  7.   
  8.       
  9.     @Override  
  10.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  11.             throws BeansException {  
  12.         System.out.println("call BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName);  
  13.         return bean;  
  14.     }  
  15.   
  16.     @Override  
  17.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  18.             throws BeansException {  
  19.         System.out.println("call BeanPostProcessor interface postProcessBeforeInitialization method ::" + beanName);  
  20.         if(bean instanceof BeanTest) {  
  21.             System.out.println("bean instanceof BeanTest");  
  22.         }  
  23.         return bean;  
  24.     }  
  25.   
  26. }  

 

Java代码 复制代码
  1. package mislay;   
  2.   
  3. import org.springframework.beans.BeansException;   
  4. import org.springframework.beans.factory.BeanFactory;   
  5. import org.springframework.beans.factory.BeanFactoryAware;   
  6. import org.springframework.beans.factory.BeanNameAware;   
  7. import org.springframework.beans.factory.DisposableBean;   
  8. import org.springframework.beans.factory.InitializingBean;   
  9. import org.springframework.beans.factory.config.BeanPostProcessor;   
  10. import org.springframework.context.ApplicationContext;   
  11. import org.springframework.context.ApplicationContextAware;   
  12.   
  13. public class BeanTest implements InitializingBean, DisposableBean,BeanNameAware,BeanFactoryAware,ApplicationContextAware {   
  14.   
  15.     @Override  
  16.     public void afterPropertiesSet() throws Exception {   
  17.         System.out.println("call InitializingBean interface");   
  18.            
  19.     }   
  20.   
  21.     @Override  
  22.     public void destroy() throws Exception {   
  23.         // TODO Auto-generated method stub   
  24.         System.out.println("call DisposableBean interface");   
  25.     }   
  26.   
  27.     public void _init() {   
  28.         System.out.println("call bean init method");   
  29.     }   
  30.        
  31.     public void _destory() {   
  32.         System.out.println("call bean destory method");   
  33.     }   
  34.        
  35.     public void setSomething(Object something) {   
  36.         System.out.println("DI call setSomething method");   
  37.     }   
  38.        
  39.     public BeanTest() {   
  40.         System.out.println("BeanTest create");   
  41.     }   
  42.   
  43.     @Override  
  44.     public void setBeanName(String name) {   
  45.         // TODO Auto-generated method stub   
  46.         System.out.println("call BeanNameAware interface name is:" + name);   
  47.     }   
  48.   
  49.     @Override  
  50.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {   
  51.         // TODO Auto-generated method stub   
  52.         System.out.println("call BeanFactoryAware interface");   
  53.     }   
  54.        
  55.        
  56.     @Override  
  57.     public void setApplicationContext(ApplicationContext applicationContext)   
  58.             throws BeansException {   
  59.         // TODO Auto-generated method stub   
  60.         System.out.println("call ApplicationContextAware interface");   
  61.     }   
  62.   
  63. }  
[java] view plaincopy
  1. package mislay;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.BeanFactory;  
  5. import org.springframework.beans.factory.BeanFactoryAware;  
  6. import org.springframework.beans.factory.BeanNameAware;  
  7. import org.springframework.beans.factory.DisposableBean;  
  8. import org.springframework.beans.factory.InitializingBean;  
  9. import org.springframework.beans.factory.config.BeanPostProcessor;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.ApplicationContextAware;  
  12.   
  13. public class BeanTest implements InitializingBean, DisposableBean,BeanNameAware,BeanFactoryAware,ApplicationContextAware {  
  14.   
  15.     @Override  
  16.     public void afterPropertiesSet() throws Exception {  
  17.         System.out.println("call InitializingBean interface");  
  18.           
  19.     }  
  20.   
  21.     @Override  
  22.     public void destroy() throws Exception {  
  23.         // TODO Auto-generated method stub  
  24.         System.out.println("call DisposableBean interface");  
  25.     }  
  26.   
  27.     public void _init() {  
  28.         System.out.println("call bean init method");  
  29.     }  
  30.       
  31.     public void _destory() {  
  32.         System.out.println("call bean destory method");  
  33.     }  
  34.       
  35.     public void setSomething(Object something) {  
  36.         System.out.println("DI call setSomething method");  
  37.     }  
  38.       
  39.     public BeanTest() {  
  40.         System.out.println("BeanTest create");  
  41.     }  
  42.   
  43.     @Override  
  44.     public void setBeanName(String name) {  
  45.         // TODO Auto-generated method stub  
  46.         System.out.println("call BeanNameAware interface name is:" + name);  
  47.     }  
  48.   
  49.     @Override  
  50.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
  51.         // TODO Auto-generated method stub  
  52.         System.out.println("call BeanFactoryAware interface");  
  53.     }  
  54.       
  55.       
  56.     @Override  
  57.     public void setApplicationContext(ApplicationContext applicationContext)  
  58.             throws BeansException {  
  59.         // TODO Auto-generated method stub  
  60.         System.out.println("call ApplicationContextAware interface");  
  61.     }  
  62.   
  63. }  

 

Java代码 复制代码
  1. package mislay;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.AbstractApplicationContext;   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  6.   
  7. public class Test {   
  8.   
  9.     public static void main(String[] args) {   
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");   
  11.         ((AbstractApplicationContext) context).registerShutdownHook();   
  12.     }   
  13. }  
[java] view plaincopy
  1. package mislay;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.AbstractApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class Test {  
  8.   
  9.     public static void main(String[] args) {  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  11.         ((AbstractApplicationContext) context).registerShutdownHook();  
  12.     }  
  13. }  

 

Java代码 复制代码
  1. //下面是输出的内容   
  2.   
  3.   
  4. //这一段可以看出首先实例化化的BeanFactoryPostProcessor   
  5. --------> begin BeanFactoryPostProcessorTest   
  6. definition bean name:mislay.BeanFactoryPostProcessorTest   
  7. definition bean name:mislay.BeanPostProcessorTest   
  8. definition bean name:beanTest   
  9. <--------- end BeanFactoryPostProcessorTest   
  10.   
  11. //这一段是Bean的创建以及依赖注入   
  12. BeanTest create   
  13. DI call setSomething method   
  14.   
  15. //这一段就是内置的BeanPostProcessor调用   
  16. call BeanNameAware interface name is:beanTest   
  17. call BeanFactoryAware interface  
  18. call ApplicationContextAware interface  
  19.   
  20. //这里开始调用自己BeanPostProcessor   
  21. call BeanPostProcessor interface postProcessBeforeInitialization method ::beanTest   
  22. bean instanceof BeanTest   
  23. //穿插着对InitializingBean接口和定义的init方法的调用   
  24. call InitializingBean interface  
  25. call bean init method   
  26. call BeanPostProcessor interface postProcessAfterInitialization method; :beanTest   
  27. //上面就结束了对配置的BeanPostProcessor的调用   
  28.   
  29.   
  30. //最后销毁   
  31. call DisposableBean interface  
  32. call bean destory method  
[java] view plaincopy
  1. //下面是输出的内容  
  2.   
  3.   
  4. //这一段可以看出首先实例化化的BeanFactoryPostProcessor  
  5. --------> begin BeanFactoryPostProcessorTest  
  6. definition bean name:mislay.BeanFactoryPostProcessorTest  
  7. definition bean name:mislay.BeanPostProcessorTest  
  8. definition bean name:beanTest  
  9. <--------- end BeanFactoryPostProcessorTest  
  10.   
  11. //这一段是Bean的创建以及依赖注入  
  12. BeanTest create  
  13. DI call setSomething method  
  14.   
  15. //这一段就是内置的BeanPostProcessor调用  
  16. call BeanNameAware interface name is:beanTest  
  17. call BeanFactoryAware interface  
  18. call ApplicationContextAware interface  
  19.   
  20. //这里开始调用自己BeanPostProcessor  
  21. call BeanPostProcessor interface postProcessBeforeInitialization method ::beanTest  
  22. bean instanceof BeanTest  
  23. //穿插着对InitializingBean接口和定义的init方法的调用  
  24. call InitializingBean interface  
  25. call bean init method  
  26. call BeanPostProcessor interface postProcessAfterInitialization method; :beanTest  
  27. //上面就结束了对配置的BeanPostProcessor的调用  
  28.   
  29.   
  30. //最后销毁  
  31. call DisposableBean interface  
  32. call bean destory method 
原创粉丝点击