1.bean实例化大体流程
循环遍历beanDefinitionNames
refresh()————finishBeanFactoryInitialization(beanFactory)————
deCreateBean————createBeanInsatnce
if(mbd.getFactoryMethodName != null) instantiateUsingFactoryMethod (factoryMethod) factoryMethod实例化
determineConstrunctsFromBeanPostProcessors(构造函数 分为有参构造函数和无参构造函数) 构造函数实例化
applyMergedBeanDefinitionPostProcessors IOC入口
收集注解 AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor
addSingletonFactory 添加三级缓存,目的是解决循环依赖
populateBean IOC流程
initializeBean
2.factory-method实例化其实就是反射调用factory-method,创建一个对象,封装成BeanWrapperImpl对象 new BeanWrapperImpl();
3.bean实例化什么情况才能在缓存拿到数据?
单例的、并且是非懒加载的、并且是非抽象的
因为单例的、并且是非懒加载的、并且是非抽象的,Spring容器启动时就已经实例化,
refresh————finishBeanFactoryInitialization(beanFactory)——
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
所以context.getBean(beanName)后面就有可能去缓存拿
3.bean实例化四种情况 AbstractAutowireCapableBeanFactory类的createBeanInstance方法
a.实例化factoryMethod方法对应的实例
b.实例化带有@Autowired注解的有参构造函数
c.实例化带有@Autowired注解的无参构造函数
d.实例化无参构造函数
bean的四种实例化:AbstractAutowireCapableBeanFactory类的createBeanInstance()
1、factory-method
<bean id="factoryBean" class="com.lhc.bean.FactoryBean"></bean>
<bean id="factoryMethodBean" factory-bean="factoryBean" factory-method="createfactoryMethod"></bean>
@Bean