栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Bean加载过程(注解)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 

 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

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

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

创建处理@EventListener实例工厂类

二、注册Config类

创建BeanDefinition

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

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

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

 (org.springframework.context.annotation.AnnotationConfigUtils#processCommonDefinitionAnnotations)

BeanDefinition放进BeanDefinitionRegistry(即DefaultListableBeanFactory)

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

三、bean实例化过程

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)

2、调用beanFactoryPostProcessor注册beanDefinition

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

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

4、根据beanDefinition实例化bean

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

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

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

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

 
6、单例bean创建

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

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

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

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

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

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

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

10、执行MergedBeanDefinitionPostProcessor,进行beanDefinition合并

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

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

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

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

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

13、属性填充 

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

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

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

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

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

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

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

17、bean初始化

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

 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)

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

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

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

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

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号