Spring Bean加载过程(注解)

 新手上路中...

目录

项目环境

一、注册beanProcessor

二、注册Config类

三、bean实例化过程


项目环境

package com.lb.bean.load;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class BeanService {


	public void method(){
		System.out.println("bean service");

	}

}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class BeanLoadConfiguration {
}
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanLoadApplication {

	public static void main(String[] args) throws Exception {

		System.out.println("==========start=============");
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanLoadConfiguration.class);
		BeanService bean = context.getBean(BeanService.class);
		bean.method();
		context.close();
		System.out.println("==============end===============");
	}
}

一、注册beanProcessor

Spring Bean加载过程(注解)_第1张图片

Spring Bean加载过程(注解)_第2张图片 Spring Bean加载过程(注解)_第3张图片

 org.springframework.context.annotation.ConfigurationClassPostProcessor--处理@Configuration、@PropertySources、@ComponentScan、@Component

 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor-处理@Autowired

 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor-处理@PostConstruct、@PreDestroy、@Resource、@EJB

Spring Bean加载过程(注解)_第4张图片

 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor-处理@PersistenceUnit、@PersistenceContext、@EntityManagerFactory、@EntityManager

Spring Bean加载过程(注解)_第5张图片

 org.springframework.context.event.EventListenerMethodProcessor-处理@EventListener

Spring Bean加载过程(注解)_第6张图片

创建处理@EventListener实例工厂类

Spring Bean加载过程(注解)_第7张图片

二、注册Config类

创建BeanDefinition

(org.springframework.context.annotation.AnnotationConfigApplicationContext#register)

Spring Bean加载过程(注解)_第8张图片

 获取公共注解数据-@Lazy、@Primary、@DependsOn、@Role、@Description

(org.springframework.context.annotation.AnnotatedBeanDefinitionReader#doRegisterBean)

Spring Bean加载过程(注解)_第9张图片

 (org.springframework.context.annotation.AnnotationConfigUtils#processCommonDefinitionAnnotations)Spring Bean加载过程(注解)_第10张图片

BeanDefinition放进BeanDefinitionRegistry(即DefaultListableBeanFactory)

(org.springframework.context.annotation.AnnotatedBeanDefinitionReader#doRegisterBean)

三、bean实例化过程

Spring Bean加载过程(注解)_第11张图片

1、注册ApplicationContextAwareProcessor-为实现以下接口注入上下文

org.springframework.context.EnvironmentAware

org.springframework.context.EmbeddedValueResolverAware

org.springframework.context.ResourceLoaderAware               

org.springframework.context.ApplicationEventPublisherAware

org.springframework.context.MessageSourceAware

org.springframework.context.ApplicationStartupAware

org.springframework.context.ApplicationContextAware

(org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory)

Spring Bean加载过程(注解)_第12张图片

2、调用beanFactoryPostProcessor注册beanDefinition

(org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors)

Spring Bean加载过程(注解)_第13张图片

Spring Bean加载过程(注解)_第14张图片

3、通过 org.springframework.context.annotation.ConfigurationClassPostProcessor扫描bean注册到BeanDefinitionRegistry

Spring Bean加载过程(注解)_第15张图片

4、根据beanDefinition实例化bean

(org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization)

Spring Bean加载过程(注解)_第16张图片

(org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons)

Spring Bean加载过程(注解)_第17张图片

5、先从三级缓存singletonFactories获取提前暴露bean

(org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean)

Spring Bean加载过程(注解)_第18张图片

 
6、单例bean创建

(org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean)

Spring Bean加载过程(注解)_第19张图片

7、调用 InstantiationAwareBeanPostProcessor可以提前实例化

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean)

Spring Bean加载过程(注解)_第20张图片

8、执行InstantiationAwareBeanPostProcessor,进行实例化前和实例化后处理

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation)

Spring Bean加载过程(注解)_第21张图片

 9、非提前实例化,则调用doCreateBean 

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean)

Spring Bean加载过程(注解)_第22张图片

10、执行MergedBeanDefinitionPostProcessor,进行beanDefinition合并

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors)

Spring Bean加载过程(注解)_第23张图片

11、提前暴露,加入三级缓存singletonFactories

(org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingletonFactory)

Spring Bean加载过程(注解)_第24张图片

12、从三级缓存换取对象,触发执行SmartInstantiationAwareBeanPostProcessor 

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#getEarlyBeanReference)

Spring Bean加载过程(注解)_第25张图片

13、属性填充 

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean)

Spring Bean加载过程(注解)_第26张图片

14、执行InstantiationAwareBeanPostProcessor,实例化后处理 

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean)

Spring Bean加载过程(注解)_第27张图片

15、获取属性值,执行InstantiationAwareBeanPostProcessor,如CommonAnnotationBeanPostProcessor处理@Resource;AutowiredAnnotationBeanPostProcessor处理@Autowired、@Value

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean)

Spring Bean加载过程(注解)_第28张图片

16、将获取的属性填充到属性 

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean)

Spring Bean加载过程(注解)_第29张图片

17、bean初始化

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean)

Spring Bean加载过程(注解)_第30张图片

 18、执行Aware属性注入,BeanNameAware注入beanName;BeanClassLoaderAware注入ClassLoader;BeanFactoryAware注入beanFactory

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods)

 

19、执行init方法,如实现接口InitializingBean,执行afterPropertiesSet或自定义初始化方法

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeInitMethods)

Spring Bean加载过程(注解)_第31张图片

20、执行BeanPostProcessor,执行初始化后处理

(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization)

Spring Bean加载过程(注解)_第32张图片

21、bean销毁回调方法注册-实现接口DisposableBean或AutoCloseable或DestructionAwareBeanPostProcessor判断需要销毁方法

(org.springframework.beans.factory.support.AbstractBeanFactory#registerDisposableBeanIfNecessary)

Spring Bean加载过程(注解)_第33张图片

 (org.springframework.beans.factory.support.AbstractBeanFactory#requiresDestruction)Spring Bean加载过程(注解)_第34张图片

 

你可能感兴趣的:(spring,java,后端)