(五)SpringBoot启动过程的分析-刷新ApplicationContext (3)

① - 调用父类的实现

// ServletWebServerApplicationContext.java protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { // 添加用于处理WebApplicationContextServletContextAware接口的processor beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this)); // 忽略ServletContextAware接口的注入 beanFactory.ignoreDependencyInterface(ServletContextAware.class); registerWebApplicationScopes(); } // 注册web应用的作用域 public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, @Nullable ServletContext sc) { beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope()); if (sc != null) { ServletContextScope appScope = new ServletContextScope(sc); beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope); // Register as ServletContext attribute, for ContextCleanupListener to detect it. sc.setAttribute(ServletContextScope.class.getName(), appScope); } beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory()); beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory()); beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory()); beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory()); if (jsfPresent) { FacesDependencyRegistrar.registerFacesDependencies(beanFactory); } }

② - 根据basePackage指定的位置进行扫描bean
③ - 根据注解来扫描指定的bean

invokeBeanFactoryPostProcessors()

主要用于调用BeanFactoryPostProcessors的实现

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { // 调用BeanFactoryPostProcessor PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }

在调用BeanFactoryPostProcessor时,会首先调用BeanDefinitionRegistryPostProcessor, 因为后者是对前者的扩展,并且有可能在后者中又重新注册了前者的其他实例。由于PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()方法过长,这里直接写行内注释能够比较
直观的分析前后关系。

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. // 已处理的Bean Set<String> processedBeans = new HashSet<>(); // 判断当前的BeanFactory是否为一个Bean注册器,实际上就是代表同时实现了BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor接口的实现 // 对于同时实现两个接口的类,将先调用BeanDefinitionRegistryPostProcessor里面的方法,再调用BeanFactoryPostProcessor里面的方法 // 在调用的时候又要区分是实现了PriorityOrdered还是Ordered接口。 if (beanFactory instanceof BeanDefinitionRegistry) { // 转换为注册器 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; // 用于存放常规的BeanFactoryPostProcessor List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); // 用于存放BeanFactoryPostProcessor的扩展BeanDefinitionRegistryPostProcessor,这里是一个汇总的列表 List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); // 遍历传入的BeanFactoryPostProcessor for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { // 优先执行BeanFactoryPostProcessor的扩展类BeanDefinitionRegistryPostProcessor,它的优先级最高 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { // 类型转换 BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; // 调用postProcessBeanDefinitionRegistry()方法,内部可能注册了Bean,也可能重新定义了一些普通的BeanFactoryPostProcessor registryProcessor.postProcessBeanDefinitionRegistry(registry); // 添加到已处理列表 registryProcessors.add(registryProcessor); } else { // 对比上面if代码块会发现,这里没有作调用,直接先保存在常规列表内部,因为常规的Processor在调用的时候还有其他考虑,接着往下看便是 regularPostProcessors.add(postProcessor); } } // 不要在这里初始化FactoryBean(请看清是FactoryBean,工厂类,不是类工厂(BeanFactory),他们有巨大的差异),需要保留所有的常规类未初始化,以便使用BeanFactoryPostProcessor对其处理 // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // 根据BeanDefinitionRegistryPostProcessors 实现的接口划分为三类:实现了PriorityOrdered的、实现了Ordered的以及前面两者都没实现的 // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. // 保存当前将要处理的BeanDefinitionRegistryPostProcessor列表,每处理完一种分类的就清空 List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // 首先调用实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessors // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. // 获取当前Bean工厂内部所有的,类型为BeanDefinitionRegistryPostProcessor.class的后处理器名称 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { // 筛选出实现了PriorityOrdered接口的后处理器,放入当前处理列表 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); // 同时放入已处理列表 processedBeans.add(ppName); } } // 按照优先级排序 sortPostProcessors(currentRegistryProcessors, beanFactory); // 添加实现了PriorityOrder接口的BeanDefinitionRegistryPostProcessor到它的汇总列表里面 registryProcessors.addAll(currentRegistryProcessors); // 调用所有的BeanDefinitionRegistryPostProcessor实例的postProcessBeanDefinitionRegistry()方法 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); // 清除内部的实现了PriorityOrder的BeanDefinitionRegistryPostProcessor currentRegistryProcessors.clear(); // 上面是处理实现了PriorityOrdered接口的,这里处理实现了Ordered接口的, 为何这里又获取了一次postProcessorNames,前面不是才获取么? // 这里获取一次是因为前面处理的时候有可能又加入了新的BeanDefinitionRegistryPostProcessor // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); // 清空上一步已经执行过的 currentRegistryProcessors.clear(); // 继续调用普通的BeanDefinitionRegistryPostProcessors // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); reiterate = true; } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); } // 最后调用普通的 BeanFactoryPostProcessor, // Now, invoke the postProcessBeanFactory callback of all processors handled so far. invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); //因为BeanDefinitionRegistryPostProcessor也是继承了BeanFactoryPostProcessor,,也具有postProcessBeanFactory()方法的,所以也需要执行 invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // 若不是BeanDefinitionRegistry,那就是直接实现了BeanFactoryPostProcessor // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } // 下面这部分逻辑就和上面套路一样,无非处理的是BeanFactoryPostProcessor罢了 // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); List<String> orderedPostProcessorNames = new ArrayList<>(); List<String> nonOrderedPostProcessorNames = new ArrayList<>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. sortPostProcessors(priorityOrderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(orderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // Clear cached merged bean definitions since the post-processors might have // modified the original metadata, e.g. replacing placeholders in values... beanFactory.clearMetadataCache(); } 小结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzszw.html