Spring 源码之--getBean完整流程

begin with : new AnnotationConfigApplicationContext(MainConfig.class);

 

==> org.springframework.context.annotation.AnnotationConfigApplicationContext 

public AnnotationConfigApplicationContext(Class... annotatedClasses)

--> refresh(); 

==> org.springframework.context.support.AbstractApplicationContext

public void refresh() throws BeansException, IllegalStateException 

--> finishBeanFactoryInitialization(beanFactory);

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)

--> beanFactory.preInstantiateSingletons();

==>org.springframework.beans.factory.support.AbstractBeanFactory

public void preInstantiateSingletons() throws BeansException

-->this.getBean(beanName);

public Object getBean(String name) throws BeansException

-->return doGetBean(name, null, null, false);

传入的参数:

String name, bean的名称(可能是别名)

final Class requiredType, 需要从容器中获取的类型

final Object[] args, 传入的构造器参数,通过这个参数指定具体使用哪个构造器

boolean typeCheckOnly 标识当前bean是不是作检查标记

protected T doGetBean(final String name, @Nullable final Class requiredType,@Nullable final Object[] args, boolean typeCheckOnly)

--> final String beanName = transformedBeanName(name);

传入的名称可能是别名,在这里作别名解析

-->Object sharedInstance = getSingleton(beanName);

去缓存中获取对象(三级缓存)

singletonObjects 单例缓存池,用于保存创建完成的对象

第一步:先去单例缓存池中获取,看是否有创建好的对象,有直接返回对象.

第二步:在单例缓存池singletonObjects中没有找到对象, 且singletonsCurrentlyInCreation标记了当前bean是否正在创建,从map里面去找,找到了就说明正在创建

第三步:去二级缓存earlySingletonObjects 中获取,若没有获取到,就要去三级缓存中获取

第四步:singletonFactories 去三级缓存中获取,那么什么时候放入到容器中的了?

在调用CreateBeanInstance 之后,populateBean之前放进去的.(以ObjectFactory形式放进去的)

==> if sharedInstance is null ==> 

-->bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);

创建的对象可能不是我们想要的也有可能是FactoryBean,在这里我们需要处理工厂bean

判断当前bean是不是工厂bean如果是这里直接返回

-->return bean

==>else 

-->BeanFactory parentBeanFactory = getParentBeanFactory();

获取父工厂

-->containsBeanDefinition(beanName)

父容器不为空,当前的容器又没有包含该bean,有且一种情况就是default

这里的逻辑分支就是,当前容器有父容器,且当前容器不包含当前bean定义,那么就会走父容器创建bean的流程

-->final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

合并Bean的定义,存在父bean是一个抽象bean,专门用来给子bean做继承的

-->checkMergedBeanDefinition(mbd, beanName, args);

校验当前的bean定义,若当前的bean定义是abstract的话直接抛出异常

-->registerDependentBean(dep, beanName);

解析依赖的bean,并且注册依赖,

比如InstancA创建该对象 必须要依赖InstanceB的话,那么就会首先创建B对象

sharedInstance = getSingleton(beanName, () -> {}

if (mbd.isSingleton()) {
	sharedInstance = getSingleton(beanName, () -> {
	try {
		return createBean(beanName, mbd, args);
	}
	catch (BeansException ex) {
		// Explicitly remove instance from singleton cache: It might have been put there
		// eagerly by the creation process, to allow for circular reference resolution.
		// Also remove any beans that received a temporary reference to the bean.
		destroySingleton(beanName);
		throw ex;
	}
	});
	bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

根据Bean定义的scope来创建Bean

==>org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

public Object getSingleton(String beanName, ObjectFactory singletonFactory)

beforeSingletonCreation(beanName);

标识当前创建对象放入到一个标识singletonsCurrentlyInCreation对象中

singletonObject = singletonFactory.getObject();

通过ObjeccFacotry.getObject()方法触发createBean的流程, 如上的匿名类中的createBean()

return createBean(beanName, mbd, args);

==>org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)

Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

就是在这里给后置处理器一个机会来返回代理对象,但是在这里并没有创建代理

对象,仅仅是把切面信息解析出来缓存好

Object beanInstance = doCreateBean(beanName, mbdToUse, args);

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)

instanceWrapper = createBeanInstance(beanName, mbd, args);

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)

return instantiateUsingFactoryMethod(beanName, mbd, args);

通过工厂方法来创建Bean实例 @Bean的形式配置的

return autowireConstructor(beanName, mbd, null, null);

在构造函数上标识了@Autowired的注入方法 调用构造方法来创建

return instantiateBean(beanName, mbd);

调用无参的构造器来创建对象

@componet @Service 。。。

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)

addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));

把刚刚创建好的对象 属性还没有赋值的对象通过ObjectFactory的形式加入到缓存中,用来解决循环依赖

/**
 * Add the given singleton factory for building the specified singleton
 * if necessary.
 * 

To be called for eager registration of singletons, e.g. to be able to * resolve circular references. * @param beanName the name of the bean * @param singletonFactory the factory for the singleton object */ protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }

populateBean(beanName, mbd, instanceWrapper);

给创建对象的属性赋值

exposedObject = initializeBean(beanName, exposedObject, mbd);

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd)

invokeAwareMethods(beanName, bean);

回调XXXAware接口

wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);

调用后置处理啊的before方法

invokeInitMethods(beanName, wrappedBean, mbd);

调用init方法

wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

调用后置处理器的after方法

 ==>org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

public Object getSingleton(String beanName, ObjectFactory singletonFactory)

afterSingletonCreation(beanName);

当前的bean已经创建好了 把当前的bean给从singletonsCurrentlyInCreation移除掉

addSingleton(beanName, singletonObject);

把创建好的对象放进到缓存池,情况二三级缓存

/**
 * Add the given singleton object to the singleton cache of this factory.
 * 

To be called for eager registration of singletons. * @param beanName the name of the bean * @param singletonObject the singleton object */ protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }

==>org.springframework.beans.factory.support.AbstractBeanFactory

protected T doGetBean(final String name, @Nullable final Class requiredType,
      @Nullable final Object[] args, boolean typeCheckOnly)

bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);

创建的对象可能不是我们想要的也有可能是FactoryBean,在这里我们需要处理工厂bean

return bean

你可能感兴趣的:(spring,ioc,spring,ioc,源码解析,getBean流程)