前言
我们来讲一下Bean的实例化过程。这篇文章中,暂时不对Bean循环依赖的情况进行分析,因为比较复杂,会放在后面单独的文章中进行分析。
准备工作
看下面这条语句AnnotationConfigApplicationContext
类的refresh
方法开始分析,从下面这条语句开始:
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
从官方的注释可以看出,这里是用来完成所有非懒加载的bean的实例化过程。
我们先写一个简单的bean用于进行测试,其中的Dao
也是一个交给spring管理的bean。spring会扫描到这个类,并添加到beanDefinitionMap
和BeanDefinitionNames
中:
@Component
public class MyService {
@Autowired
private Dao dao;
public void query(){
System.out.println("executing query method");
dao.query();
}
}
看一下finishBeanFactoryInitialization
中的代码:
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
//如果bdMap中存在conversionService,则进行初始化
//该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
//可用于AspectJ静态织入过程
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
//销毁之前在prepareBeanFactory()中生成的临时ClassLoader
beanFactory.setTempClassLoader(null);
//在这冻结对BeanDefinition的修改
//防止spring在初始化的时候发生BeanDefinition的修改
beanFactory.freezeConfiguration();
beanFactory.preInstantiateSingletons();
}
这个方法中,前面都是在做一些准备工作,直到最后执beanFactory
的preInstantiateSingletons
方法,才开始准备执行非懒加载的bean的实例化过程。先看preInstantiateSingletons
方法的前半段:
public void preInstantiateSingletons() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Pre-instantiating singletons in " + this);
}
//得到所有bean的名字
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
for (String beanName : beanNames) {
//做了合并父类的BeanDefinition的操作
//在会用xml配置bean时 有一个parent 属性,可以继承类名,作用域等
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
//判断是FactoryBean
if (isFactoryBean(beanName)) {
//如果是FactoryBean则加上 &
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
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);
}
}
}
else { //不是factoryBean的情况
getBean(beanName);
}
}
}
...
首先从beanDefinitionNames
的List中拿到所有的beanName
,进行遍历。之前讲过DefaultListableBeanFactory
内部缓存了一个beanDefinitionMap的Map
,和这个beanDefinitionNames
的List,从这也可以看出,通过适当的冗余可以一定程度上减少编码中的工作量。
在对bean进行初始化前包含3个条件:不能为抽象类、单例bean、以及非懒加载。非常好理解不再多说,重点说明一下通过isFactoryBean
方法判断bean是否Factorybean
。Factorybean
是一个比较特殊的bean,并且受spring容器管理,看一下接口定义:
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
如果一个类实现了FactoryBean
接口,那个spring容器中会存在两个对象,一个是getObject
方法返回的对象,另一个是当前FactoryBean
对象本身,并且用&
添加在beanName
前进行区分。举个例子:
@Component
public class MyFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
return new TestDao();
}
@Override
public Class<?> getObjectType() {
return TestDao.class;
}
}
测试:
System.out.println(context.getBean("myFactoryBean"));
System.out.println(context.getBean("&myFactoryBean"));
结果:
com.hydra.dao.TestDao@fbd1f6
com.hydra.factorybean.MyFactoryBean@1ce24091
对于FactoryBean
的获取,要在beanName
前加上一个前缀&
,然后会先判断是否是SmartFactoryBean
并且isEagerInit
为true,如果是才调用getBean
方法进行初始化。此处内容略过,直接看重要的getBean
方法:
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
此处为空方法,继续调用doGetBean
方法,从这开始为实例化bean的核心流程。
实例化bean
为了方便分析,我们将类与方法按照调用顺讯进行编号,方便后面解析流程的分析。
1、AbstractBeanFactory 的 doGetBean方法
和以前一样,非重点的内容直接在代码中用注释解释。
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
//先尝试从spring容器中获取一次,如果为空则实例化
Object sharedInstance = getSingleton(beanName);
//在调用getBean时,args为空
//如果不为空,那么意味着调用方不是希望获取bean,而是创建bean
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
//如果当前线程已经创建过了prototype类型的这个bean,抛出异常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 如果对spring没有进行改造,这里默认 parentBeanFactory为空
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
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) {
//typeCheckOnly为false,添加到alreadyCreated Set集合当中,表示它已经创建过
//防止重复创建
markBeanAsCreated(beanName);
}
//重点部分,创建singleton的bean,或创建新的prototype的bean
try {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// 判断当前bean是否有依赖,这里指的是使用depends-on的情况,需要先实例化依赖bean
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
//注册依赖关系
registerDependentBean(dep, beanName);
try {
//初始化被依赖bean
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
//在这才真正创建bean的实例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
//真正创建功能的语句
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
// 创建 prototype 的实例
else if (mbd.isPrototype()) {
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
//如果不是singleto和prototype,委托给相应的实现类来处理
else {
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
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);
}
//抛出异常,代码省略...
}
//类型检查,正常则返回,异常则抛出
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
在创建bean前,首先调用了DefaultSingletonBeanRegistry
的getSingleton
方法,也就是说spring在初始化一个bean前先去尝试获取一次,判断这个对象是否已经被实例化好了,如果已经存在就直接拿过来用。进入getSingleton
方法,核心代码:
Object singletonObject = this.singletonObjects.get(beanName);
看一下singletonObjects
的定义:
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
这里提前剧透一下,这个Map就是用于存放实例化好的单例bean,并且从狭义上来说,可以说这个singletonObjects
就是spring容器,并且它使用了ConcurrentHashMap
,来保证并发操作的安全性。
因为我们的bean还处于创建阶段,那么这一次是肯定不能从Map获取到实例的,那么接着向下运行,看一下调用的createBean
方法。
2、AbstractAutowireCapableBeanFactory 的 createBean方法
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isDebugEnabled()) {
logger.debug("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
//确保 BeanDefinition 中的 Class 被加载
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// 处理 lookup-method 和 replace-method 配置
// spring中把lookup-method 和 replace-method 统称为method overrides
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
//应用后置处理器,如果后置处理器返回的bean不为空则直接返回
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);
}
try {//调用doCreateBean创建bean
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
//非重要代码省略...
}
前面做了很长的铺垫工作,但还是没有创建bean,创建bean的工作被交给了doCreateBean
方法完成。
3、AbstractAutowireCapableBeanFactory 的 doCreateBean方法
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
//BeanWrapper是一个包装接口,真正实例化的是 BeanWrapperImpl
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
//创建bean实例,并将实例包裹在 BeanWrapper 实现类对象中返回
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 使用BeanWrapper 产生一个原生对象
final Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// Allow post-processors to modify the merged bean definition.
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
//执行后置处理器MergedBeanDefinitionPostProcessor
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
//用于处理循环依赖,后面单独分析
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
//执行后置处理器
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
//到此为止,还是原生对象
Object exposedObject = bean;
try {
//赋值属性,依赖,非常重要
populateBean(beanName, mbd, instanceWrapper);
//执行后置处理器,变成代理对象,aop就是在这里完成的处理
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
//中间非重要代码省略...
return exposedObject;
}
这里面做了三个比较重要的工作:
- ①、调用
createBeanInstance
方法创建bean实例 - ②、调用
populateBean
进行属性的填充,依赖注入就是在这里完成 - ③、调用
initializeBean
,执行各种后置处理器,执行各种回调函数
我们按照顺序,先接着讲①中创建bean势力的过程,等这个过程完了再回头分析属性填充和回调方法。
4、AbstractAutowireCapableBeanFactory 的 createBeanInstance方法
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
//确保加载了该class
Class<?> beanClass = resolveBeanClass(mbd, beanName);
//检测一个类的访问权限 spring默认情况下对于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);
}
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
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) {
//通过构造方法自动装配的方式构造bean对象
return autowireConstructor(beanName, mbd, null, null);
}
else {
//通过默认的无参构造方法进行
return instantiateBean(beanName, mbd);
}
}
//spring目前不知道用什么方式实例化这个bean,所以先拿到所有的构造方法
//由后置处理器决定返回哪些构造方法
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
//使用默认的无参构造方法进行初始化
return instantiateBean(beanName, mbd);
}
如果bean拥有多个构造方法的话,会根据参数去判断具体使用哪一个,具体内容比较复杂,准备以后放在一篇单独的文章中进行分析。如果只有无参构造方法或不写构造方法的话,都会默认使用无参构造方法进行实例化,这里暂时只对这种情况进行分析。
5、AbstractAutowireCapableBeanFactory 的 instantiateBean方法
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 {
// getInstantiationStrategy得到类的实例化策略
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);
}
}
这里通过getInstantiationStrategy
得到类的实例化策略,默认情况下是得到一个反射的实例化策略。然后调用instantiate
方法进行实例化。
6、SimpleInstantiationStrategy 的 instantiate方法
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
// 检测bean配置中是否配置了lookup-method 或 replace-method
//如果配置了就需使用CGLIB构建bean对象
if (!bd.hasMethodOverrides()) {
Constructor<?> constructorToUse;
synchronized (bd.constructorArgumentLock) {
constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
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);
}
}
}
//使用构造方法进行实例化
return BeanUtils.instantiateClass(constructorToUse);
}
else {
//使用CGLIB进行实例化
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
instantiateClass
方法中,通过反射创建对象:
//设置构造方法为可访问
ReflectionUtils.makeAccessible(ctor);
//反射创建对象
return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ?
KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args));
运行到这,实例化的过程就完成了,但是目前属性还没有注入,回到开头我们举的那个例子,其中还有一个Dao没有被注入,下一篇文章,我们接下来分析属性的注入。
到此这篇关于Spring源码解析 Bean的实例化的文章就介绍到这了,更多相关Spring Bean实例化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!