这一部分分析默认构造方法实例化获取实例对象的BeanWrapper,代码相对于前几种方式简单的多,因为没有参数,所以简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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 { 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); } }
|
到这里总算分析了差不多AbstractAutowireCapableBeanFactory中的createBeanInstance()方法。
也就是doCreateBean方法中获取BeanWrapper部分,接下来把InstantiationStrategy单独拿出来试着分析一下。