写点什么

Spring 源码 -BeanFactory 创建 Bean

用户头像
云淡风轻
关注
发布于: 2020 年 05 月 29 日

父级接口:

BeanFactory

The root interface for accessing a Spring bean container.


子级接口:

HierarchicalBeanFactory

Sub-interface implemented by bean factories that can be part of a hierarchy.

ConfigurationBeanFactory

Configuration interface to be implemented by most bean factories. Provides facilities to configure a bean factory, in addition to the bean factory client methods in the BeanFactory interface.

AutowireCapableBeanFactory

Interface to be implemented by bean factories that are capable of autowiring.


抽象实现类:

AbstractBeanFactory

Abstract base class for BeanFactory implementations, providing the full capabilities of the ConfigurableBeanFactory SPI

AbstractAutowireCapableBeanFactory

Abstract bean factory superclass that implements default bean creation, with the full capabilities specified by the RootBeanDefinition class. Implements the AutowireCapableBeanFactoryinterface in addition to AbstractBeanFactory's createBean method.


层级关系:


核心方法:

/**	 * Central method of this class: creates a bean instance,	 * populates the bean instance, applies post-processors, etc.	 * @see #doCreateBean	 */	@Override	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)			throws BeanCreationException {
if (logger.isTraceEnabled()) { logger.trace("Creating instance of bean '" + beanName + "'"); } RootBeanDefinition mbdToUse = mbd;
// Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); }
// Prepare method overrides. try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); }
try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. 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 { Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { // A previously detected exception with proper bean creation context already, // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry. throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
复制代码


Standard Bean Lifecycle:

Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:

* BeanNameAware's {@code setBeanName}

* BeanClassLoaderAware's {@code setBeanClassLoader}

* BeanFactoryAware's {@code setBeanFactory}

* EnvironmentAware's {@code setEnvironment}

* EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}

* ResourceLoaderAware's {@code setResourceLoader}

* (only applicable when running in an application context)

* ApplicationEventPublisherAware's {@code setApplicationEventPublisher}

* (only applicable when running in an application context)

* MessageSourceAware's {@code setMessageSource}

* (only applicable when running in an application context)

* ApplicationContextAware's {@code setApplicationContext}

* (only applicable when running in an application context)

* ServletContextAware's {@code setServletContext}

* (only applicable when running in a web application context)

* {@code postProcessBeforeInitialization} methods of BeanPostProcessors

* InitializingBean's {@code afterPropertiesSet}

* a custom init-method definition

* {@code postProcessAfterInitialization} methods of BeanPostProcessors


On shutdown of a bean factory, the following lifecycle methods apply:

* {@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors

* DisposableBean's {@code destroy}

* a custom destroy-method definition


用户头像

云淡风轻

关注

云淡风轻 2018.08.18 加入

JAVA软件工程师

评论

发布
暂无评论
Spring源码-BeanFactory创建Bean