Spring源码分析-Bean生命周期查找与注册(2)

Spring源码分析系列

Spring源码分析-启动流程浅析
Spring源码分析-BeanDefinition
Spring源码分析-Bean管理查找与注册(1)
Spring源码分析-Bean管理查找与注册(2)
Spring源码分析-Bean管理循环依赖和三级缓存
Spring源码分析-Bean生命周期概述
Spring源码分析-Bean生命周期createBean



前言

本篇博客将继续分析,bean获取与注册流程


一、getBean

spring一共提供三个getBean重载方法,具体声明如下

public Object getSingleton(String beanName)
protected Object getSingleton(String beanName, boolean allowEarlyReference)
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory)

前两个只用于查找,第三个可以用于注册

1.1、查找

下面的代码注释很详细,分别从一级缓存,二级缓存,三级缓存中进行查找,直到找到为止

@Override
@Nullable
public Object getSingleton(String beanName) {
	return getSingleton(beanName, true); //调用重载方法
}

@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    // Quick check for existing instance without full singleton lock
    // 不加锁的原因是快速查找
    Object singletonObject = this.singletonObjects.get(beanName); //一级缓存获取实例
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        singletonObject = this.earlySingletonObjects.get(beanName); //二级缓存
        if (singletonObject == null && allowEarlyReference) {
            synchronized (this.singletonObjects) {
                // Consistent creation of early reference within full singleton lock
                singletonObject = this.singletonObjects.get(beanName);//一级缓存
                if (singletonObject == null) {
                    singletonObject = this.earlySingletonObjects.get(beanName);//二级缓存
                    if (singletonObject == null) {
                        ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); //三级缓存,获取ObjectFactory,实际上是一个lambda表达式
                        if (singletonFactory != null) {
                            singletonObject = singletonFactory.getObject();//获取对象
								//为什么要将三级缓存中移除并且放到二级缓存中: 避免重复创建对象
								//为什么不移动到一级缓存中:因为当前对象还没有完成初始化                            this.earlySingletonObjects.put(beanName, singletonObject);//放到二级缓存中
                            this.singletonFactories.remove(beanName);//从三级缓存中删除
                        }
                    }
                }
            }
        }
    }
    return singletonObject;
}

1.2、注册

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {//加锁操作
        Object singletonObject = this.singletonObjects.get(beanName); //从一级缓存中获取bean对象
        if (singletonObject == null) {//当前bean不存在
            if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction " +
                        "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
            }
            beforeSingletonCreation(beanName);//标志当前beanName是在正在创建,本质上是插入到set集合中
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {
                singletonObject = singletonFactory.getObject(); //本质上是执行lambda表达式,创建对象,当返回后bean对象创建完成
                newSingleton = true; //单例
            }
            catch (IllegalStateException ex) {
                // Has the singleton object implicitly appeared in the meantime ->
                // if yes, proceed with it since the exception indicates that state.
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    throw ex;
                }
            }
            catch (BeanCreationException ex) {
                if (recordSuppressedExceptions) {
                    for (Exception suppressedException : this.suppressedExceptions) {
                        ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            }
            finally {
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = null;
                }
                afterSingletonCreation(beanName);//将当前正在创建的bean从set集合中删除
            }
            if (newSingleton) {
                addSingleton(beanName, singletonObject);//放到一级缓存中
            }
        }
        return singletonObject;
    }
}

注意方法的第二个参数是一个lambda表达式,lambda表达式内容如下:

sharedInstance = getSingleton(beanName, () -> {
	try {
		return createBean(beanName, mbd, args); //创建bean对象,底层通过反射方式
	}
	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对象,要将bean插入到一级缓存,并且从二级,三级缓存中移除掉

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); //插入到set中
	}
}

二、getObjectForBeanInstance

当我们从缓存中获取到bean对象,这个bean对象可能是FactoryBean,并不是我们实际要用的对象

2.1、源码

protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName, 
                                    @Nullable RootBeanDefinition mbd) {

    // Don't let calling code try to dereference the factory if the bean isn't a factory.
    // 判断是否是间接引用的FactoryBean, 名字是以&开始
    if (BeanFactoryUtils.isFactoryDereference(name)) {
        if (beanInstance instanceof NullBean) {
            return beanInstance;
        }
        if (!(beanInstance instanceof FactoryBean)) {
            throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
        }
        if (mbd != null) {
            mbd.isFactoryBean = true;
        }
        return beanInstance;
    }

    // Now we have the bean instance, which may be a normal bean or a FactoryBean.
    // If it's a FactoryBean, we use it to create a bean instance, unless the
    // caller actually wants a reference to the factory.
    if (!(beanInstance instanceof FactoryBean)) {//说明当前对象没有实现FactoryBean接口,所以可以直接返回给应用使用
        return beanInstance;
    }
    //进入下面分支表示一定是FactoryBean
    Object object = null;
    if (mbd != null) {//创建FactoryBean对象本身时,会进入
        mbd.isFactoryBean = true;
    }
    else {//第一次查缓存,对象肯定不存在,如果是单例模式,第一次创建成功后保存到缓存中,以后在获取就不会创建对象
        object = getCachedObjectForFactoryBean(beanName); //从缓存factoryBeanObjectCache中取出
    }
    if (object == null) {//表示缓存没有,则创建对象
        // Return bean instance from factory.
        FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
        // Caches object obtained from FactoryBean if it is a singleton.
        if (mbd == null && containsBeanDefinition(beanName)) {
            mbd = getMergedLocalBeanDefinition(beanName);
        }
        boolean synthetic = (mbd != null && mbd.isSynthetic());
        object = getObjectFromFactoryBean(factory, beanName, !synthetic); //调用FactoryBean接口中的getObject返回真实实例对象
    }
    return object;
}

2.2、例子

spring代码大部分接口都自己的抽象类,我们可以直接继承抽象类,并将其注入到spring ioc容器中

@Component
public class FactoryBeanUser extends AbstractFactoryBean<User> {
    @Override
    public Class<?> getObjectType() {
        return User.class;
    }

    @Override
    protected User createInstance() throws Exception {
        User user = new User();//创建对象
        user.setUsername("xuxb");
        return user;
    }
}

上面创建一个FactoryBean并且注入到spring容器中,接下来获取一下这个bean:

public static void main(String[] args) {
     try {
         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.worker");
         Object o1 = context.getBean("factoryBeanUser");
         System.out.println("o1="+o1);
         Object o2 = context.getBean("factoryBeanUser");
         System.out.println("o2="+o2);
         context.registerShutdownHook();
     } catch (Exception e) {
         System.out.println(e);
     }
 }

输出内容

> Task :spring-study-01:WApp.main()
o1=com.worker.User@10a035a0
o2=com.worker.User@10a035a0
BUILD SUCCESSFUL in 6s

从上面内容可知
1、注入的类型的是FactoryBeanUser,而得到的类型是User
2、每次获取bean对象都是同一个(地址相同),第一次创建成功后就缓存起来了


三、总结

本篇博客,介绍了getBean方法处理逻辑,它是spring框架中一个核心代码,理解它有助于理解spring对bean的管理方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值