spring中管理的Bean的创建流程
来源:互联网 发布:个人卡通形象设计软件 编辑:程序博客网 时间:2024/05/11 00:13
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销毁阶段
以上是自己总结的,没研究过源码,恐有误…作参考用
以下附上验证的代码:
package mislay;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {System.out.println("--------> begin BeanFactoryPostProcessorTest");String[] names = beanFactory.getBeanDefinitionNames();for (String name : names) {System.out.println("definition bean name:" + name);}System.out.println("<--------- end BeanFactoryPostProcessorTest");}}
package mislay;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class BeanPostProcessorTest implements BeanPostProcessor {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {System.out.println("call BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName);return bean;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {System.out.println("call BeanPostProcessor interface postProcessBeforeInitialization method ::" + beanName);if(bean instanceof BeanTest) {System.out.println("bean instanceof BeanTest");}return bean;}}
package mislay;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;public class BeanTest implements InitializingBean, DisposableBean,BeanNameAware,
BeanFactoryAware,ApplicationContextAware {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("call InitializingBean interface");}@Overridepublic void destroy() throws Exception {// TODO Auto-generated method stubSystem.out.println("call DisposableBean interface");}public void _init() {System.out.println("call bean init method");}public void _destory() {System.out.println("call bean destory method");}public void setSomething(Object something) {System.out.println("DI call setSomething method");}public BeanTest() {System.out.println("BeanTest create");}@Overridepublic void setBeanName(String name) {// TODO Auto-generated method stubSystem.out.println("call BeanNameAware interface name is:" + name);}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {// TODO Auto-generated method stubSystem.out.println("call BeanFactoryAware interface");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {// TODO Auto-generated method stubSystem.out.println("call ApplicationContextAware interface");}}
package mislay;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");((AbstractApplicationContext) context).registerShutdownHook();}}
//这一段可以看出首先实例化化的BeanFactoryPostProcessor--------> begin BeanFactoryPostProcessorTestdefinition bean name:mislay.BeanFactoryPostProcessorTestdefinition bean name:mislay.BeanPostProcessorTestdefinition bean name:beanTest<--------- end BeanFactoryPostProcessorTest//这一段是Bean的创建以及依赖注入BeanTest createDI call setSomething method//这一段就是内置的BeanPostProcessor调用call BeanNameAware interface name is:beanTestcall BeanFactoryAware interfacecall ApplicationContextAware interface//这里开始调用自己BeanPostProcessorcall BeanPostProcessor interface postProcessBeforeInitialization method ::beanTestbean instanceof BeanTest//穿插着对InitializingBean接口和定义的init方法的调用call InitializingBean interfacecall bean init methodcall BeanPostProcessor interface postProcessAfterInitialization method; :beanTest//上面就结束了对配置的BeanPostProcessor的调用//最后销毁call DisposableBean interfacecall bean destory method
- spring中管理的Bean的创建流程
- spring中bean的创建
- spring中bean的创建
- Spring的bean管理-注解创建对象
- Servlet中调用Spring管理的Bean
- servlet 中获取 spring 管理的 bean
- servlet中获取spring管理的bean
- servlet 中获取 spring 管理的 bean
- spring的bean管理
- Spring的bean管理
- spring的bean管理
- Spring Bean的管理
- Spring中常见的bean创建异常
- spring中bean创建的细节
- spring学习日志之三创建Bean的流程
- Spring 源码分析《Bean的获取与创建流程》
- Spring中bean工厂创建bean的三种方式
- Spring管理Bean的生命周期,Spring创建的Bean是单实例还是多实例?
- C++学习笔记 --- 指针1
- PHP安全过滤字符串 addslashes()与stripsashes()
- 诺基亚推出基于NFC技术的新游戏:与朋友赛车切西瓜玩儿纸牌
- ubuntu11.10~_~评测
- 用C语言实现参数个数可变的函数
- spring中管理的Bean的创建流程
- postfix+dovecot+saslauthd
- 性能动态库的生命周期
- Servlet细节分析
- 1300 Door Man
- 临界区
- 用Visual C++编写电子邮件程序
- 32位程序移植到64位平台前的准备工作
- c/c++下修改字符串常量