1. Spring Bean实例化
1. 通过BeanName获取Class对象(resolveBeanClass(mbd, beanName);)
2. 处理lookup标签(mbdToUse.prepareMethodOverrides();)// Prepare method overrides.
3. 短路Bean的创建过程 Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
Class<?> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
4. 实例化对象(doCreateBean(beanName, mbdToUse, args);)
- 实例化对象(instanceWrapper = createBeanInstance(beanName, mbd, args))
- 使用FactoryMethod实例化(instantiateUsingFactoryMethod(beanName, mbd, args);)
- 解析过构造参数或FactoryMethod,才使用构造参数实例化(autowireConstructor(beanName, mbd, null, null);)
- 使用策略实例化(instantiateBean(beanName, mbd);)InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
- if (bd.getMethodOverrides().isEmpty()) {
- // 反射初始化
- return BeanUtils.instantiateClass(constructorToUse);
- } else {
- // Must generate CGLIB subclass.
- return instantiateWithMethodInjection(bd, beanName, owner);
- }
- 提前曝光实例(addSingletonFactory),循环依赖使用
- 填充属性(populateBean(beanName, mbd, instanceWrapper);)
- 短路属性注入 bp instanceof InstantiationAwareBeanPostProcessor / !ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)if (bp instanceof InstantiationAwareBeanPostProcessor) {
- InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
- if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
- continueWithPropertyPopulation = false;
- break;
- }
- }
- 属性注入,按照名称(autowireByName(beanName, mbd, bw, newPvs);)或类型(autowireByType(beanName, mbd, bw, newPvs);)注入
- 短路if (bp instanceof InstantiationAwareBeanPostProcessor) {
- InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
- pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
- if (pvs == null) {
- return;
- }
- }
- 应用属性(applyPropertyValues(beanName, mbd, bw, pvs);Spel表达式解析)
- 初始化Bean(initializeBean(beanName, exposedObject, mbd);)
- 执行Aware接口自动注入,包括BeanNameAware、BeanFactoryAware、BeanClassLoaderAware(invokeAwareMethods(beanName, bean);)
- 短路bean后续BeanPostProcessor执行。postProcessBeforeInitializationresult = beanProcessor.postProcessBeforeInitialization(result, beanName);
- if (result == null) {
- return result;
- }
- 执行init初始化方法(invokeInitMethods(beanName, wrappedBean, mbd);)
- ((InitializingBean) bean).afterPropertiesSet();
- invokeCustomInitMethod(beanName, bean, mbd);
- 短路bean后续BeanPostProcessor执行。wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
- result = beanProcessor.postProcessAfterInitialization(result, beanName);
- if (result == null) {
- return result;
- }
- }
5. 初始化依赖的Bean
6. Register bean as disposable.(registerDisposableBeanIfNecessary(beanName, bean, mbd);)
相关推荐
主要介绍了Spring bean对象实例化实现过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Bean的实例化 在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例...
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
我们有两个类LinuxCondition 和WindowsCondition 。两个类都实现了Condtin接口,重载的方法返回一个基于操作系统类型的布尔值。 下面我们定义两个bean,一个符合条件另外一个不符合条件:
spring核心:iOC Aop IOC:inverson of control(控制反转): 就是对对象控制权的转移,从程序代码本身反转到 外部容器中,通过外部容器实现对象的创建,属性 ...创建spring的配置文件,编写bean 4.在测试类中测试
通过反射实例化得到对应的实例化对象,放置在map中(map是键值对,可根据id获取值)(遍历list获取对应的class属性,利用class。formName(class).newIntance()) * 4.通过id获取指定的实例化对象 * */ 底层工厂...
bean实例化三种方式:用构造器来实例化 、使用 静态工厂方法实例化 、使用实例工厂方法实例化 。总结,我们会选择第一种方式,因为spring的存在就是要消除工厂模式,因为工厂本身就会在每次调用时new出对象,只是把...
Spring IOC容器Bean对象实例化模拟
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的 依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程(将依赖关系注入到对象中) ,spring的...
可以使用<bean lazy-init=""/>延缓实例化 可以使用延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和prototype d.初始化:<bean ...
可以使用<bean lazy-init=""/>延缓实例化 可以使用延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和prototype d.初始化:<bean ...
文章目录一、概念和历史二、hellospring三、IOC创建对象的方式四、Spring配置五、set注入–属性注入六、c命名和p命名空间注入七 、bean的作用域 一、概念和历史 Spring的形成,最初来自Rod Jahnson所著的一本很有...
可以使用<bean lazy-init=""/>延缓实例化 可以使用延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和prototype d.初始化:<bean ...
实例化完成的Bean对象存放在singletonObjects当中。 FileSystemXmlApplicationContext是加载磁盘里具体位置的xml文件。不同环境下会出现不同的容器,比如导入spring-web包时,会有web环境下的容器。 (4)基于xml
java查看sun包源码 Spring 学习笔记 ...ApplicationContext:提供了更多的高级特性,在加载配置文件后就会实例化对象。是BeanFactory的子接口 BeanFactory是Spring框架的基础设施,面向Spring本身 A
◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...
3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean时机 15 Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 ...
目的 该项目测试了运行静态块和Spring bean初始化的顺序 实验:静态块可以访问自动装配的对象吗?... 由于Spring必须实例化该类(这肯定需要加载该类),然后才能在该实例上调用setter,因此静态初始化程序块已经运行。
IDEA项目源码,ssm项目包含数据库,sql文件,免费使用,分享给大家,资源共享,Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目...
◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...