spring IoC 二 实例化(instant)、初始化(init)
2021-06-16 13:04
标签:rop throwable override ssi res creat ceo -- finish 接着上一篇 AbstractAutowireCapableBeanFactory中createBean方法: ①resolveBeforeInstantiation方法 作用Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. 该方法中用到 resolveBeforeInstantiation--->applyBeanPostProcessorsBeforeInstantiation--->postProcessBeforeInstantiation(实例前处理) ②doCreateBean如下 AbstractAutowireCapableBeanFactory中doCreateBean方法 其中 ①createBeanInstance Bean实例化,工厂、jvm、cglib生成 ②populateBean 依赖注入 包括 autowire_by_name、autowire_by_type,其中调用了postProcessAfterInstantiation(实例后处理) ③initializeBean 初始化 invokeAwareMethods、applyBeanPostProcessorsBeforeInitialization-->postProcessBeforeInitialization(初始化前处理)、 invokeInitMethods(afterPropertiesSet invokeCustomInitMethod自定义init)、applyBeanPostProcessorsAfterInitialization-->postProcessAfterInitialization(初始化后处理) spring IoC 二 实例化(instant)、初始化(init) 标签:rop throwable override ssi res creat ceo -- finish 原文地址:https://www.cnblogs.com/toUpdating/p/9726300.html 2 protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
3 if (logger.isDebugEnabled()) {
4 logger.debug("Creating instance of bean ‘" + beanName + "‘");
5 }
6 // Make sure bean class is actually resolved at this point.
7 resolveBeanClass(mbd, beanName);
8
9 // Prepare method overrides.
10 try {
11 mbd.prepareMethodOverrides();
12 }
13 catch (BeanDefinitionValidationException ex) {
14 throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
15 beanName, "Validation of method overrides failed", ex);
16 }
17
18 try {
19 // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
20 Object bean = resolveBeforeInstantiation(beanName, mbd);
21 if (bean != null) {
22 return bean;
23 }
24 }
25 catch (Throwable ex) {
26 throw new BeanCreationException(mbd.getResourceDescription(), beanName,
27 "BeanPostProcessor before instantiation of bean failed", ex);
28 }
29
30 Object beanInstance = doCreateBean(beanName, mbd, args);
31 if (logger.isDebugEnabled()) {
32 logger.debug("Finished creating instance of bean ‘" + beanName + "‘");
33 }
34 return beanInstance;
35 } 1 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
2 // Instantiate the bean.
3 BeanWrapper instanceWrapper = null;
4 if (mbd.isSingleton()) {
5 instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
6 }
7 if (instanceWrapper == null) {
8 instanceWrapper = createBeanInstance(beanName, mbd, args);
9 }
10 final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
11 Class> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
12
13 // Allow post-processors to modify the merged bean definition.
14 synchronized (mbd.postProcessingLock) {
15 if (!mbd.postProcessed) {
16 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
17 mbd.postProcessed = true;
18 }
19 }
20
21 // Eagerly cache singletons to be able to resolve circular references
22 // even when triggered by lifecycle interfaces like BeanFactoryAware.
23 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
24 isSingletonCurrentlyInCreation(beanName));
25 if (earlySingletonExposure) {
26 if (logger.isDebugEnabled()) {
27 logger.debug("Eagerly caching bean ‘" + beanName +
28 "‘ to allow for resolving potential circular references");
29 }
30 addSingletonFactory(beanName, new ObjectFactory
文章标题:spring IoC 二 实例化(instant)、初始化(init)
文章链接:http://soscw.com/index.php/essay/94580.html