springboot application.properties server.port配置的問題
近年來,springboot以其快速構建方便便捷,開箱即用,約定優(yōu)于配置(Convention Over Configuration)的特性深受廣大開發(fā)者喜愛。
springboot已經(jīng)集成配置好了一套web開發(fā)的默認配置,開發(fā)者可以無需修改任何配置即可開始一個web工程,但是實際情況中有時候開發(fā)者還是需要修改部分默認配置項來使其更加契合自己的項目需求。
下面就其中一個小問題做個記錄
在配置服務啟動的端口時,springboot默認在application.properties配置文件中提供了server.port配置項來
讓開發(fā)者自行配置服務啟動端口號,**但是注意:**
#服務啟動端口號 server.port=8889
該配置項要想生效其實是依賴于項目中內嵌的tomcat容器,如下圖:
內嵌tomcat的jar包依賴包含在pom中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果pom中不引人上述依賴,那么項目中不會導入內嵌tomcat的jar包,相應的application.properties配置文件中server.port配置項也將無法生效,因為該配置項實際上修改的就是內嵌tomcat的web端口號。
Spring Boot server.port配置原理
我們經(jīng)常配置server.port=xxx,但其實這是一個比較復雜的過程才生效的,這次講講生效的過程。
1. autoConfigure
本質來源于自動配置
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration
TomcatServletWebServerFactory
為什么是這個類,核心是beanPostProcess原理
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties { /** * Server HTTP port. */ private Integer port;
beanPostProcess
public class WebServerFactoryCustomizerBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { private ListableBeanFactory beanFactory; private List<WebServerFactoryCustomizer<?>> customizers; @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)); } 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; } @SuppressWarnings({ "unchecked", "rawtypes" }) private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() { return (Collection) this.beanFactory.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values(); }
最終
beanFactory.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values()
WebServerFactoryCustomizer對象.customize(webServerFactory)
@Configuration @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @ConditionalOnClass(ServletRequest.class) @ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties(ServerProperties.class) @Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class }) public class ServletWebServerFactoryAutoConfiguration { @Bean public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) { return new ServletWebServerFactoryCustomizer(serverProperties); }
這里就將port設置好了。
這里使用函數(shù)式編程,lambda表達式,將port的值設置進了
ConfigurableServletWebServerFactory ,即TomcatServletWebServerFactory對象
2. embed tomcat如何使用
tomcat創(chuàng)建時,會通過getBean方式獲取工廠
就是 TomcatServletWebServerFactory
然后設置connector,從TomcatServletWebServerFactory讀取port,設置connector,設置結束
總結
Spring Boot在解耦的時候繞了很多彎,先@Bean factory對象,然后BeanPostProcess,然后啟動embed tomcat 在factory 中new Tomcat 然后設置Connector,設置port。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/quentinschuman/article/details/103435886