springboot2.0如何編碼配置servlet容器屬性

使用springboot2.0之後,如何通過編碼自定義servlet容器參數,使用如下代碼

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> embeddedServletContainerCustomizer() {
	return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
		@Override
		public void customize(ConfigurableWebServerFactory factory) {
			  factory.setPort(8081);
		}
	};
}

那麼這個配置是何時且如何被調用的呢,請看WebServerFactoryCustomizerBeanPostProcessor,該類實現BeanPostProcessor, BeanFactoryAware,在實現方法中調用WebServerFactoryCustomizer類的customize方法

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

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章