1 介绍
创建并初始化spring容器中,refresh()方法中解析xml配置文件,注册容器后处理器,bean后处理器,初始化MessageSource,ApplicationEventMulticaster广播器,注册完ApplicationListener监听器后,关键一步就是创建和初始化其他非lazy-init的singleton beans。这样在容器初始化好的时候,这些singleton beans就已经创建和初始化好了,可以大大提高bean的访问效率。这个过程比较复杂,本文将详细分析整个流程。先看涉及到的关键类。
AbstractApplicationContext: 定义了spring容器初始化的大部分流程方法,子类必须遵循这个流程,但可以修改流程中的方法,典型的模板模式。bean创建的入口方法finishBeanFactoryInitialization也在这个方法中。
DefaultListableBeanFactory:一种BeanFactory容器实现,实现了ConfigurableListableBeanFactory接口
BeanDefinition:描述bean结构,对应XML中的或者注解中的@Component
AbstractBeanFactory:继承了BeanFactory容器,主要负责getBean创建Bean实例。
2 流程
2.1 finishBeanFactoryInitialization
初始化spring容器中的refresh()方法中,会调用finishBeanFactoryInitialization()方法,它是创建和初始化其他非lazy-init的singleton的bean的入口。下面从这个方法开始分析。
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 初始化conversionService类型转换bean,它可以服务于其他bean的类型转换
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// 注册字符串解析器,用来解析注解中的属性
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// 初始化LoadTimeWeaverAware bean
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// 停止使用临时的ClassLoader,
beanFactory.setTempClassLoader(null);
beanFactory.freezeConfiguration();
// 这儿才是最关键的一步,创建和初始化非lazy-init的singleton beans
beanFactory.preInstantiateSingletons();
}
finishBeanFactoryInitialization()做了初始化conversionService类型转换器等的工作,这些不是关键点。关键点在preInstantiateSingletons()方法中,它会做创建和初始化singleton bean的工作。下面接着分析
public void preInstantiateSingletons() throws BeansException {
// 获取XML配置文件解析时,解析到的所有beanname
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// 遍历所有没有标注lazy-init的singleton的beanname,创建bean
for (String beanName : beanNames) {
// 利用beanname获取BeanDefinition,在XML解析时会生成BeanDefinition对象,将XML中的各属性添加到BeanDefinition的相关标志位中,比如abstractFlag,scope等
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// 非abstract,非lazy-init的singleton bean才需要在容器初始化阶段创建
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
// 处理FactoryBean
if (isFactoryBean(beanName)) {
// 获取FactoryBean实例,FactoryBean前面会加一个&符号
final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
((SmartFactoryBean<?>) factory).isEagerInit(),
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
// 非Factorybean,直接调用getBean方法,关键所在,后续分析
else {
getBean(beanName);
}
}
}
// bean创建后,对SmartInitializingSingleton回调afterSingletonsInstantiated()方法,这儿不用太care
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
preInstantiateSingletons流程稍微复杂点,主要有
- 获取XML解析时的beanNames
- 遍历beanNames,获取BeanDefinition。对非abstract,非lazy-init的singleton bean的进行实例化
- 如果是FactoryBean,则需要判断isEagerInit,来确定是否调用getBean创建对应的bean。
- 如果不是,则直接调用getBean创建对应bean
- bean创建后,对SmartInitializingSingleton回调afterSingletonsInstantiated()方法。
2.2 getBean 创建和初始化bean实例
下面我们着重来分析bean的创建,也就是getBean()方法。
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
// beanname转换,去掉FactoryBean的&前缀,处理alias声明。细节可自行分析
final String beanName = transformedBeanName(name);
Object bean;
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
// 判断singleton bean是否已经创建好了,创建好了则直接从内存取出。
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
} else {
// 之前没创建的,则需要创建。
// 正在创建,则直接异常返回
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 检查是否有beanname对应的BeanDefinition
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// 没有找到BeanDefinition,看看parent工厂中有没有,调用parent工厂的getBean
// 获取原始的name,包含了FactoryBean前缀,&符号
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else {
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
// 找到了beanname对应的BeanDefinition,合并parent的BeanDefinition(XML中的parent属性)
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// 处理dependsOn属性
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
// 遍历所有的dependOn bean,要先注册和创建依赖的bean
for (String dep : dependsOn) {
// check是否两个bean是循环依赖,spring不能出现bean的循环依赖
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
// 注册并创建依赖的bean
registerDependentBean(dep, beanName);
getBean(dep);
}
}
// 处理scope属性
if (mbd.isSingleton()) {
// singleton, 必须保证线程安全情况下创建bean,保证单例
sharedInstance = getSingleton(beanName, () -> {
try {
// 反射创建bean实例,这个过程很复杂,稍后分析
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// 异常处理,清除掉bean
destroySingleton(beanName);
throw ex;
}
});
// 获取bean实例
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
else if (mbd.isPrototype()) {
// prototype,创建一个全新的实例
Object prototypeInstance = null;
try {
// 创建前的回调
beforePrototypeCreation(beanName);
// 反射创建bean实例,稍后详细分析
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
// 创建后的回调,清除inCreation的标志
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
else {
// 其他scope值
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
// scope属性不能接收空值
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
// check 创建的bean是否是requiredType指明的类型。如果不是,先做转换,转换不成的话只能类型不匹配抛出异常了
if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
try {
// 尝试将创建的bean转换为requiredType指明的类型
return getTypeConverter().convertIfNecessary(bean, requiredType);
} catch (TypeMismatchException ex) {
// 转换不成功,抛出异常
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
doGetBean概括了bean创建和初始化的主要流程,十分复杂,步骤主要为
- beanname转换,去掉FactoryBean的&前缀,处理alias声明
- 判断singleton bean是否已经创建好了,创建好了则直接从内存取出
- 没有创建好,则检查是否有beanname对应的BeanDefinition,没有则到parent工厂中查找,命中则使用parent工厂再次调用getBean以及doGetBean创建
- 有BeanDefinition,则合并parent属性指向的中的属性,这主要是处理bean的parent属性。子bean会继承parent bean的属性。
- 处理dependsOn属性。必须先创建好所有的依赖的bean
- 处理scope属性,如果是singleton的,则必须保证线程安全情况下创建单例。如果是prototype,则必须保证创建一个全新的bean。创建bean通过createBean()反射创建。
2.3 createBean 反射创建bean实例
下面来分析createBean()方法,这个过程也是相当复杂的。
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
RootBeanDefinition mbdToUse = mbd;
// 拷贝一个新的RootBeanDefinition供创建bean使用
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// 处理bean中定义的覆盖方法,主要是xml:lookup-method或replace-method。标记override的方法为已经加载过的,避免不必要的参数检查开销。这儿不详细展开了。
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
// 调用BeanPostProcessors bean后处理器,使得bean后处理器可以返回一个proxy bean,从而代替我们要创建的bean。回调后处理器的postProcessBeforeInstantiation()方法,如果这个方法中返回了一个bean,也就是使用了proxy,则再回调postProcessAfterInitialization()方法。之后返回这个Proxy bean即可。
try {
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
// doCreateBean创建bean实例,后面详细分析
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
return beanInstance;
}
// 各种异常,省略
...
}
createBean()方法大概步骤如下
- 拷贝一个新的RootBeanDefinition供创建bean使用
- 处理lookup-method或replace-method
- 调用BeanPostProcessors后处理器
- doCreateBean创建bean实例
下面我们重点分析doCreateBean方法
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// 创建bean实例,如果是singleton,先尝试从缓存中取,取不到则创建
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
// 反射创建bean实例,后面详细说
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
mbd.resolvedTargetType = beanType;
// 回调MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition,它可以修改bean属性
if (beanType != null) {
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
}
// 曝光单例对象的引用,主要是为了解决单例间的循环依赖问题,以及依赖的bean比较复杂时的初始化性能问题
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// 初始化bean,后面详细介绍
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
// 省略异常处理
// 单例曝光对象的处理,不用太在意
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
// 抛出异常,省略代码
}
}
}
}
// 注册bean为可销毁的bean,bean销毁时,会回调destroy-method
if (bean != null) {
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
}
return exposedObject;
}
doCreateBean方法主要流程为
- createBeanInstance() 创建bean实例
- 回调postProcessMergedBeanDefinition(), 可以修改bean属性
- initializeBean() 初始化bean实例,包括后处理器的调用,init-method的调用等
- 注册bean为可销毁的,这样在bean销毁时,就可以回调到destroy-method.
2.3.1 createBeanInstance 反射创建bean实例
我们先分析如何创建bean实例的。
个protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// 先创建class对象,反射的套路。利用bean的class属性进行反射,所以class属性一定要是bean的实现类
Class<?> beanClass = resolveBeanClass(mbd, beanName);
// class如果不是public的,则抛出异常。因为没法进行实例化
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
//
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
// 使用FactoryBean的factory-method来创建,支持静态工厂和实例工厂
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// 无参数情况时,创建bean。调用无参构造方法
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
// autoWire创建 自动装配
return autowireConstructor(beanName, mbd, null, null);
}
else {
// 普通创建
return instantiateBean(beanName, mbd);
}
}
// 有参数情况时,创建bean。先利用参数个数,类型等,确定最精确匹配的构造方法。
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
// 有参数时,又没获取到构造方法,则只能调用无参构造方法来创建实例了(兜底方法)
return instantiateBean(beanName, mbd);
}
instantiateBean,使用无参构造方法,反射创建bean实例代码如下
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
try {
Object beanInstance;
final BeanFactory parent = this;
if (System.getSecurityManager() != null) {
beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
getInstantiationStrategy().instantiate(mbd, beanName, parent),
getAccessControlContext());
}
else {
// 创建实例,关键点,其他都不用care
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
}
}
这个方法没什么要注意的,关键点在instantiate方法
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
// Don't override the class with CGLIB if no overrides.
if (bd.getMethodOverrides().isEmpty()) {
Constructor<?> constructorToUse;
// 保证线程安全情况下,获取Constructor
synchronized (bd.constructorArgumentLock) {
// 获取构造方法或factory-method
constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
// BeanDefinition中如果没有Constructor或者factory-method,则直接使用默认无参构造方法。
final Class<?> clazz = bd.getBeanClass();
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
if (System.getSecurityManager() != null) {
constructorToUse = AccessController.doPrivileged(
(PrivilegedExceptionAction<Constructor<?>>) () ->
clazz.getDeclaredConstructor());
}
else {
// 获取默认无参构造方法
constructorToUse = clazz.getDeclaredConstructor();
}
bd.resolvedConstructorOrFactoryMethod = constructorToUse;
}
catch (Throwable ex) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
// 使用上一步得到的Constructor,反射获取bean实例
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// Must generate CGLIB subclass.
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
instantiate方法主要做两件事
- 确定Constructor或者factory-method
- 利用Constructor,反射创建bean实例。
分析到这儿Bean的创建就结束了,这个过程实在是太复杂了!
2.3.2 initializeBean 初始化bean实例
bean创建完后,容器会对它进行初始化,包括后处理的调用,init-method的调用等。请看下面详解。
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
// 回调各种aware method,如BeanNameAware, BeanFactoryAware等
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
// 回调beanPostProcessor的postProcessBeforeInitialization()方法
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
// init-method 和 postProcessAfterInitialization
if (wrappedBean != null) {
try {
// 回调init-method
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
// 回调beanPostProcessor的postProcessAfterInitialization()方法
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
}
return wrappedBean;
}
由此可见,initializeBean(),也就是bean的初始化流程为
- 回调各种aware method,如BeanNameAware,将容器中相关引用注入到bean中,供bean使用
- 回调beanPostProcessor的postProcessBeforeInitialization(), 后处理器的初始化前置调用
- 回调init-method, 注解和XML中都可以声明
- 回调beanPostProcessor的postProcessAfterInitialization()方法,后处理器的初始化后置调用。
从这个流程,我们也能清晰的分析出容器后处理器两个方法的调用时机。分析源码可以大大加深我们对spring API的理解。
3 总结
Bean实例的创建和初始化流程还是十分复杂的。从源码中可以清晰的分析出spring bean的各种特性。如factory-method, BeanPostProcessor等。有助于我们spring bean行为的理解。所以分析源码还是十分值得的。