基本流程图
image.pngTomcatServletWebServerFactory实例化过程
实例化
bean
生命周期,首先是实例化:
初始化之前处理
实例化完成后,进行初始化之前,有处理器要处理,就是我们前面注册的WebServerFactoryCustomizerBeanPostProcessor
:
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
网友评论