2023-09-14  阅读(222)
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105517981

基本流程图,方便查看

202309142259437521.png

TomcatServletWebServerFactory实例化过程

实例化

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

202309142259453782.png

初始化之前处理

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

202309142259464613.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();
    	}

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

202309142259484874.png

202309142259495085.png

202309142259504876.png

202309142259519957.png

202309142259530448.png

202309142259538189.png

接下去我们看他们定制化了什么。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文
  • 点赞