Spring Bean 元信息配置阶段
BeanDefinition 配置
- 面向资源
- XML配置
- Properties 配置
- 面向注解
- 面向 API
Spring Bean 元信息解析阶段
面向资源 BeanDefinition 解析
- BeanDefinitionReader
- XML 解析器 - BeanDefintionParser
面向注解 BeanDefinition 解析
- AnnotatedBeanDefinitionReader
/**
* 注解 BeanDefinition 解析示例
*
* @author wangzefeng
*/
public class AnnotatedBeanDefinitionParsingDemo {
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 基于注解的 AnnotatedBeanDefinitionReader
AnnotatedBeanDefinitionReader beanDefinitionReader = new AnnotatedBeanDefinitionReader(beanFactory);
int beforeRegister = beanFactory.getBeanDefinitionCount();
System.out.println("Before register, bean count is : " + beforeRegister);
// 注册当前类(非 @Component Class)
beanDefinitionReader.register(AnnotatedBeanDefinitionParsingDemo.class);
int afterRegister = beanFactory.getBeanDefinitionCount();
System.out.println("After register, bean count is : " + afterRegister);
System.out.println("Register bean count is : " + (afterRegister - beforeRegister));
// 普通 class 作为 Component 注册到 Spring IoC 容器后,通常 Bean 名称为首字母小写类名
// Bean 名称生成来自于 BeanNameGenerator,注解实现:AnnotationBeanNameGenerator
AnnotatedBeanDefinitionParsingDemo lookup = beanFactory.getBean("annotatedBeanDefinitionParsingDemo", AnnotatedBeanDefinitionParsingDemo.class);
System.out.println(lookup);
}
}
Spring Bean 注册阶段:BeanDefinition 与单体 Bean 注册
BeanDefinition 注册接口 - BeanDefinitionRegistry
实现:DefaultListableBeanFactory#registerBeanDefinition
-
allowBeanDefinitionOverriding:是否允许 BeanDefinition 覆盖,Spring 默认允许,SpringBoot 默认不允许覆盖(org.springframework.boot.SpringApplication#allowBeanDefinitionOverriding)
-
beanDefinitionMap - key 为 bean name,value 为 BeanDefinition 对象
-
beanDefinitionNames - 按注册顺序的 BeanDefinition name 列表
Spring BeanDefinition 合并阶段
GenericBeanDefintion -> RootBeanDefinition
Spring Bean Class 加载阶段
Spring Bean 实例化阶段(Instantiation)
AbstractAutowireCapableBeanFactory#createBean(RootBeanDefinition, java.lang.Object[])
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
...
try {
// 非主流生命周期 - Bean 实例化前阶段
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
try {
// Bean 实例化阶段
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
...
}
非主流生命周期 - Bean 实例化前阶段 - resolveBeforeInstantiation
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation 返回非 null 实例时,会短路掉后续该 Bean 的实例化初始化过程,使用该实例作为 Spring Bean 返回。
AbstractAutowireCapableBeanFactory#createBean()
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInstantiation
@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
Object result = bp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
return null;
}
Bean 实例化阶段 - doCreateBean(beanName, mbdToUse, args)
AbstractAutowireCapableBeanFactory#doCreateBean
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
// 创建 BeanWrapper 实例
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
Object bean = instanceWrapper.getWrappedInstance();
...
// Bean 实例化后阶段 - Bean 属性赋值
populateBean(beanName, mbd, instanceWrapper);
// Bean 初始化阶段
exposedObject = initializeBean(beanName, exposedObject, mbd);
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
...
// 构造器依赖注入
// Candidate constructors for autowiring?
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
// 实例化策略 - InstaniationStrategy
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}
-
实例化方式
-
实例化策略 - InstaniationStrategy
-
构造器依赖注入
-
实例化策略 - InstaniationStrategy
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
Object beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
SimpleInstantiationStrategy#instantiate(RootBeanDefinition, BeanFactory)
// Don't override the class with CGLIB if no overrides.
if (!bd.hasMethodOverrides()) {
// 通过无参构成器反射实例化 bean
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// Must generate CGLIB subclass.
return instantiateWithMethodInjection(bd, beanName, owner);
}
构造器注入
-
优先注入 @Primary 候选 bean
-
无 @Primary 候选bean,优先按构造器参数名称匹配候选 bean
Bean 实例化后阶段 - populateBean(beanName, mbd, instanceWrapper)
Bean 属性赋值(Populate)判断
InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation
- true (默认值)
- false: 代表该 Bean 对象属性不允许赋值(配置元信息 -> 属性值)
Bean 属性赋值(Populate)前阶段
-
Bean 属性值元信息
PropertyValues
-
Bean 属性赋值前回调
- Spring1.2 - Spring5.0 - InstantiationAwareBeanPostProcessor#postProcessPropertyValues
- Spring 5.1+ - InstantiationAwareBeanPostProcessor#postProcessProperties
Bean 属性赋值 - applyPropertyValues
Spring Bean 初始化阶段(Initialization)
AbstractAutowireCapableBeanFactory#doCreateBean
->AbstractAutowireCapableBeanFactory#initializeBean
populateBean(beanName, mbd, instanceWrapper);
exposedObject = initializeBean(beanName, exposedObject, mbd);
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
invokeAwareMethods(beanName, bean);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
Spring Bean Aware 接口回调阶段 - invokeAwareMethods(beanName, bean);
Spring Aware 接口
-
BeanNameAware
-
BeanClassLoaderAware
-
BeanFactoryAware
-
其他由 ApplicationContext 引入的 Aware - ApplicationContextAwareProcessor
-
EnvironmentAware
-
EmbeddedValueResolverAware
-
ResourceLoaderAware
-
ApplicationEventPubliserAware
-
MessageSourceAware
-
ApplicationStartupAware
-
ApplicationContextAware
-
private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
bean instanceof ApplicationStartupAware)) {
return bean;
}
AccessControlContext acc = null;
if (System.getSecurityManager() != null) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
}
else {
invokeAwareInterfaces(bean);
}
return bean;
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationStartupAware) {
((ApplicationStartupAware) bean).setApplicationStartup(this.applicationContext.getApplicationStartup());
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
Spring Bean 初始化前阶段 - applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)
- AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
- BeanPostProcessor#postProcessBeforeInitialization
/**
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
- 在 ApplicationContext 环境中,第一个添加的 BeanPostProccessor 是 ApplicationContextAwareProcessor, 所以相应的 Aware 接口回调也最先被执行
AbstractApplicationContext#prepareBeanFactory
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
...
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartup.class);
...
}
Spring Bean 初始化阶段 - invokeInitMethods(beanName, wrappedBean, mbd)
回调顺序如下:
-
@PostConstruct 标注方法
CommonAnnotationBeanPostProcessor <- InitDestroyAnnotationBeanPostProcessor
即在 AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization 阶段完成回调
-
实现 InitializingBean 接口的 afterPropertiesSet() 方法
-
自定义初始化方法 initMethod
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
// 实现 InitializingBean 接口的 afterPropertiesSet() 方法
((InitializingBean) bean).afterPropertiesSet();
}
if (mbd != null && bean.getClass() != NullBean.class) {
String initMethodName = mbd.getInitMethodName();
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
// 自定义初始化方法 initMethod
invokeCustomInitMethod(beanName, bean, mbd);
}
}
}
Spring Bean 初始化后阶段 - applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
/**
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other {@code BeanPostProcessor} callbacks.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
Spring Bean 初始化完成阶段 - SmartInitializingSingleton - Spring 4.1+
DefaultListableBeanFactory#preInstantiateSingletons
-> DefaultListableBeanFactory#preInstantiateSingletons
-> SmartInitializingSingleton#afterSingletonsInstantiated
Spring Bean 销毁阶段
AbstractAutowireCapableBeanFactory#destroyBean
->DisposableBeanAdapter#destroy
public void destroy() {
if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
processor.postProcessBeforeDestruction(this.bean, this.beanName);
}
}
// 实现 DiposableBean 接口 desotry() 方法回调
if (this.invokeDisposableBean) {
((DisposableBean) this.bean).destroy();
}
// 自定义 destoryMethod() 方法回调
if (this.destroyMethod != null) {
invokeCustomDestroyMethod(this.destroyMethod);
}
else if (this.destroyMethodName != null) {
Method methodToInvoke = determineDestroyMethod(this.destroyMethodName);
if (methodToInvoke != null) {
invokeCustomDestroyMethod(ClassUtils.getInterfaceMethodIfPossible(methodToInvoke));
}
}
}
Spring Bean 销毁前阶段 - DestructionAwareBeanPostProcessor
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction
其中一个实现 CommonAnnotationBeanPostProcessor: @PreDestroy 注解方法的回调
Spring Bean 销毁阶段
- @PreDestory 标注方法
- 实现 DisposableBean 接口的 destroy() 方法
- 自定义销毁方法(destoryMethod)