Spring启动—Bean的初始化顺序

通过这段代码作为分析的入口。以下分析都基于该示例,完整代码见:https://github.com/abelzha/spring-framework

public class InitMain {
   
	public static void main(String[] args) {
   
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfigInit.class);
		System.out.println(Arrays.asList(context.getBeanFactory().getBeanDefinitionNames()).toString().replaceAll(",", "\n"));

		Person person = context.getBean(Person.class);
		System.out.println(person.getName());
		context.close();
	}
}

1. IOC容器启动—Bean的初始化顺序

在Spring启动的时候会先创建容器、进行Bean的初始化。

1、根据配置类,创建容器,完成添加内置类的beanDefinitionMap

1、创建容器(AnnotationConfigApplicationContext)
容器的中的bean工厂:DefaultListableBeanFactory

2、初始配置信息设置,填充各种map、set.如下图所示
在这里插入图片描述
现在我们只关注一个map,它就是beanDefinitionMap。
将内置类的beanDefinition放入beanDefinitionMap,以下是该示例的默认添加的5个内置类

1	name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor 
	class[org.springframework.context.annotation.ConfigurationClassPostProcessor]

2	name:org.springframework.context.event.internalEventListenerFactory
	class:[org.springframework.context.event.DefaultEventListenerFactory]

3	name:org.springframework.context.event.internalEventListenerProcessor
	class:[org.springframework.context.event.EventListenerMethodProcessor]

4	name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor 
	class:[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor]

5	name:org.springframework.context.annotation.internalCommonAnnotationProcessor
	class:[org.springframework.context.annotation.CommonAnnotationBeanPostProcessor]

代码说明:

/**
	 * Register all relevant annotation post processors in the given registry.
	 * @param registry the registry to operate on
	 * @param source the configuration source element (already extracted)
	 * that this registration was triggered from. May be {@code null}.
	 * @return a Set of BeanDefinitionHolders, containing all bean definitions
	 * that have actually been registered by this call
	 */
	public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值