Spring启动流程
2022-02-25 11:01:12 0 举报
Spring启动流程
作者其他创作
大纲/内容
// 第一次进入拿到的是ConfigurationClassPostProcessor 的 BeanDefinition 与扫描相关//当前循环 List<BeanDefinitionRegistryPostProcessor> 放入 实现PriorityOrdered排好序的BeanDefinitionRegistryPostProcessor(beanFactory 后置处理器) registryProcessors.addAll(currentRegistryProcessors);
// 增强配置类,代理加了Configuration注解的配置类(cglb 动态代理生成代理类,修改BeanDifinition的beanClass属性指向生成的代理类)// Configuration 的作用是在-->某个用@Bean 注解注释的方法内部有调用其他@Bean 注解注释的方法时,是用该配置类的代理类去执行调用方法,那么在执行之前会执行代理逻辑,判断被调用的bean是不是已经创建出来了,如果是那就不需要再创建,直接从单例池获取 enhanceConfigurationClasses(beanFactory);
注册CommonAnnotationBeanPostProcessor类型的BeanDefinition会处理bean中的@Resource注解
// ConfigurationClassPostProcessor 会解析配置类 processConfigBeanDefinitions(registry);
prepareBeanFactory(beanFactory); 在这个步骤会加入ApplicationListenerDetector,这里也加了一次,不是重复了吗?
// 执行完BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry()方法后, // 再执行BeanDefinitionRegistryPostProcessor的postProcessBeanFactory()方法 font color=\"#b71c1c\
// 添加了事件监听器后,判断是否有earlyApplicationEvents,如果有就使用事件广播器发布earlyApplicationEvents\t\t// earlyApplicationEvents表示在事件广播器还没生成好之前ApplicationContext所发布的事件\t\tSet<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;\t\tthis.earlyApplicationEvents = null;\t\tif (!CollectionUtils.isEmpty(earlyEventsToProcess)) {\t\t\tfor (ApplicationEvent earlyEvent : earlyEventsToProcess) {\t\t\t\tgetApplicationEventMulticaster().multicastEvent(earlyEvent);\t\t\t}\t\t}
子流程
if (this.applicationEventMulticaster != null) { this.applicationEventMulticaster.addApplicationListener(listener); } this.applicationListeners.add(listener);
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(AppConfig.class); applicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } }); applicationContext.refresh();
继承 SmartInitializingSingleton
prepareRefresh()
关联
// ApplicationListenerDetector负责把ApplicantsListener类型的Bean注册到ApplicationContext中 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
ConfigurationClassPostProcessor.postProcessBeanFactory()
解析配置类会扫描,扫描出程序员自己定义出来的BeanFactoryPostProcessor
注册AutowiredAnnotationBeanPostProcessor类型的BeanDefinition会处理bean中的@Value @Autowired注解
this.beanFactory = new DefaultListableBeanFactory();
// 准备BeanFactory // 1. 设置BeanFactory的类加载器、SpringEL表达式解析器、类型转化注册器 // 2. 添加三个BeanPostProcessor,注意是具体的BeanPostProcessor实例对象 // 3. 记录ignoreDependencyInterface // 4. 记录ResolvableDependency // 5. 添加三个单例Bean prepareBeanFactory(beanFactory);
加入beanFactory的beanPostProcessors属性前会移除(根据ApplicationListenerDetector的equals()方法)之前加的 this.beanPostProcessors.remove(beanPostProcessor); this.beanPostProcessors.add(beanPostProcessor);
// 模板方法比如子类可以把ServletContext中的参数对设置到Environment(spring环境容器中) initPropertySources();
refresh();
//font color=\"#b71c1c\
// 设置lifecycleProcessor,默认为DefaultLifecycleProcessor\t\tinitLifecycleProcessor();
this.scanner = new ClassPathBeanDefinitionScanner(this);
afterSingletonsInstantiated()// 所有单例bean 实例化后调用
注册EventListenerMethodProcessor类型的BeanDefinition,用来处理@EventListener注解的
当前未执行
// 将扫描到的BeanPostProcessors实例化并排序,并添加到BeanFactory的beanPostProcessors属性中去 registerBeanPostProcessors(beanFactory);
// 子类来设置一下BeanFactory与web相关 postProcessBeanFactory(beanFactory);
publishEvent(new ContextRefreshedEvent(this));// 发布容器启动完成事件
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);初始化容器开始
//实现ImportAware实现类上没有@Component,通过其他类@Import导入,可以获取到导入类的注解信息\t\tbeanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
beanFactory.setBeanClassLoader(getClassLoader());
启动结束
//最后加入ApplicationListenerDetector 这个BeanPostProcessorbeanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
currentRegistryProcessors.clear();
registerDefaultFilters();
//在初始化后 将ApplicationListener 加入applicationContext 的事件广播器中Boolean flag = this.singletonNames.get(beanName); if (Boolean.TRUE.equals(flag)) { this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); }
// 把定义的ApplicationListener的Bean对象,设置到ApplicationContext中去,并执行在此之前所发布的事件 registerListeners();
// 将单例非懒加载bean 实例化放入单例池// 流程图请看'spring实例化单例bean' finishBeanFactoryInitialization(beanFactory);
// Spring5.3中新增的功能,可以选择是否开启Spel功能,shouldIgnoreSpel默认为false,表示开启\t\tif (!shouldIgnoreSpel) {\t\t\tbeanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));\t\t}
@Override\tpublic boolean equals(@Nullable Object other) {\t\treturn (this == other || (other instanceof ApplicationListenerDetector &&\t\t\t\tthis.applicationContext == ((ApplicationListenerDetector) other).applicationContext));\t}
getLifecycleProcessor().onRefresh();// 调用LifecycleBean的start()
this();
// beanFactoryPostProcessors集合一般情况下都是空的,除非我们手动调用容器的addBeanFactoryPostProcessor方法添加了 // beanFactoryPostProcessors中可能包含了:普通BeanFactoryPostProcessor对象和BeanDefinitionRegistryPostProcessor对象 // 对于BeanDefinitionRegistryPostProcessor对象,会执行自己的postProcessBeanDefinitionRegistry()方法 for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryProcessor.postProcessBeanDefinitionRegistry(registry); registryProcessors.add(registryProcessor); } else { regularPostProcessors.add(postProcessor); } }
// 组成一个BeanPostProcessor,用来处理EnvironmentAware、EmbeddedValueResolverAware等回调 beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
this.reader = new AnnotatedBeanDefinitionReader(this);
getEnvironment().validateRequiredProperties();//验证环境变量中是否有对应键值存在
// 注册@Component对应的AnnotationTypeFilter\t\tthis.includeFilters.add(new AnnotationTypeFilter(Component.class));
finishRefresh();
// Invoke factory processors registered as beans in the context. // BeanFactory准备好了之后,执行BeanFactoryPostProcessor,开始对BeanFactory进行处理 // 默认情况下: // 此时beanFactory的beanDefinitionMap中有6个BeanDefinition,5个基础BeanDefinition+AppConfig的BeanDefinition // 而这6个中只有一个BeanFactoryPostProcessor:ConfigurationClassPostProcessor // 这里会执行ConfigurationClassPostProcessor进行@Component的扫描,扫描得到BeanDefinition,并注册到beanFactory中 // 注意:扫描的过程中可能又会扫描出其他的BeanFactoryPostProcessor,那么这些BeanFactoryPostProcessor也得在这一步执行 invokeBeanFactoryPostProcessors(beanFactory); // scanner.scan()
if (processedBeans.contains(ppName)) { // skip - already processed in first phase above }按照之前特殊BeanFactory后置处理器一样 排序(排序之前要排除掉已经执行过后置方法的BeanFactory后置处理器)执行后置方法postProcessor.postProcessBeanFactory(beanFactory)
// 给子类的模板方法没用 onRefresh()
// 执行手动添加的普通BeanFactoryPostProcessor的postProcessBeanFactory()方法//也就是一开始我们手动调用容器的addBeanFactoryPostProcessor方法添加了 invokeBeanFactoryPostProcessors(font color=\"#b71c1c\
当前未执行,只代表有关联
// 放入applicationContext 的事件广播器applicationEventMulticastercontext.addApplicationListener(applicationListener);
因为上面的步骤完成了扫描,这个过程中程序员可能自己定义了一些BeanPostProcessor,在这一步骤就会把BeanFactory中所有的BeanPostProcessor 找出来并实例化加到BeanFactory的beanPostProcessors属性中去,并且按照是否实现PriorityOrdered ,Ordered 接口排序
添加beanFactory.ignoreDependencyInterface的属性如果一个bean属性对应的set方法在ignoredDependencyInterfaces接口中被定义了,则该属性不会进行自动注入(是Spring中的自动注入@Bean(autowire = Autowire.BY_NAME/BY_TYPE),不是@Autowired)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(ApplicationStartupAware.class);
0 条评论
下一页