美文网首页
SpringMVC源码之web环境初始化二

SpringMVC源码之web环境初始化二

作者: 程序员札记 | 来源:发表于2023-05-26 06:43 被阅读0次

基本流程图

image.png

TomcatServletWebServerFactory实例化过程

实例化

bean生命周期,首先是实例化:

image.png

初始化之前处理

实例化完成后,进行初始化之前,有处理器要处理,就是我们前面注册的WebServerFactoryCustomizerBeanPostProcessor

image.png

WebServerFactoryCustomizerBeanPostProcessor的postProcessBeforeInitialization

这个里面有个lambda表达式的操作,其实就是先getCustomizers()获取所有的WebServerFactoryCustomizer集合,然后invoke调用每一个的customize方法做定制化,我们来看看具体的怎么做的。

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof WebServerFactory) {
            postProcessBeforeInitialization((WebServerFactory) bean);
        }
        return bean;
    }

    private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
        LambdaSafe.callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory)
                .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
                .invoke((customizer) -> customizer.customize(webServerFactory));
    }

getCustomizers

如果不存在这些定制化器,就进行获取,然后排序,变成不可变集合返回。

    private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
        if (this.customizers == null) {
            // Look up does not include the parent context
            this.customizers = new ArrayList<>(getWebServerFactoryCustomizerBeans());
            this.customizers.sort(AnnotationAwareOrderComparator.INSTANCE);
            this.customizers = Collections.unmodifiableList(this.customizers);
        }
        return this.customizers;
    }

getWebServerFactoryCustomizerBeans

从容器里获取WebServerFactoryCustomizer类型的集合返回。

    private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() {
        return (Collection) this.beanFactory.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values();
    }

我们来看看这些都在哪里:


image.png image.png image.png image.png image.png image.png

相关文章

网友评论

      本文标题:SpringMVC源码之web环境初始化二

      本文链接:https://www.haomeiwen.com/subject/bqrkedtx.html