解決springboot application.properties server.port配置問題
springboot application.properties server.port配置的問題
近年來,springboot以其快速構(gòu)建方便便捷,開箱即用,約定優(yōu)于配置(Convention Over Configuration)的特性深受廣大開發(fā)者喜愛。
springboot已經(jīng)集成配置好了一套web開發(fā)的默認(rèn)配置,開發(fā)者可以無需修改任何配置即可開始一個web工程,但是實(shí)際情況中有時候開發(fā)者還是需要修改部分默認(rèn)配置項(xiàng)來使其更加契合自己的項(xiàng)目需求。
下面就其中一個小問題做個記錄
在配置服務(wù)啟動的端口時,springboot默認(rèn)在application.properties配置文件中提供了server.port配置項(xiàng)來
讓開發(fā)者自行配置服務(wù)啟動端口號,**但是注意:**
#服務(wù)啟動端口號 server.port=8889
該配置項(xiàng)要想生效其實(shí)是依賴于項(xiàng)目中內(nèi)嵌的tomcat容器,如下圖:
內(nèi)嵌tomcat的jar包依賴包含在pom中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果pom中不引人上述依賴,那么項(xiàng)目中不會導(dǎo)入內(nèi)嵌tomcat的jar包,相應(yīng)的application.properties配置文件中server.port配置項(xiàng)也將無法生效,因?yàn)樵撆渲庙?xiàng)實(shí)際上修改的就是內(nèi)嵌tomcat的web端口號。
Spring Boot server.port配置原理
我們經(jīng)常配置server.port=xxx,但其實(shí)這是一個比較復(fù)雜的過程才生效的,這次講講生效的過程。
1. autoConfigure
本質(zhì)來源于自動配置
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è)置好了。
這里使用函數(shù)式編程,lambda表達(dá)式,將port的值設(shè)置進(jìn)了
ConfigurableServletWebServerFactory ,即TomcatServletWebServerFactory對象
2. embed tomcat如何使用
tomcat創(chuàng)建時,會通過getBean方式獲取工廠
就是 TomcatServletWebServerFactory
然后設(shè)置connector,從TomcatServletWebServerFactory讀取port,設(shè)置connector,設(shè)置結(jié)束
總結(jié)
Spring Boot在解耦的時候繞了很多彎,先@Bean factory對象,然后BeanPostProcess,然后啟動embed tomcat 在factory 中new Tomcat 然后設(shè)置Connector,設(shè)置port。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中application.properties與application.yml區(qū)別小結(jié)
- SpringBoot中讀取application.properties配置文件的方法
- 解決SpringBoot加載application.properties配置文件的坑
- SpringBoot沒有讀取到application.yml問題及解決
- SpringBoot application.yml和bootstrap.yml的區(qū)別
- springboot讀取application.yaml文件數(shù)據(jù)的方法
- SpringBoot中application.properties、application.yaml、application.yml區(qū)別
相關(guān)文章
java對list<Object>進(jìn)行手動分頁實(shí)現(xiàn)
本文主要介紹了java對list<Object>進(jìn)行手動分頁實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Spring 動態(tài)代理實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Spring 動態(tài)代理實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Java實(shí)現(xiàn)的爬蟲抓取圖片并保存操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)的爬蟲抓取圖片并保存操作,涉及Java針對頁面URL訪問、獲取、字符串匹配、文件下載等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08SpringBoot項(xiàng)目打jar包與war包的詳細(xì)步驟
SpringBoot和我們之前學(xué)習(xí)的web應(yīng)用程序不一樣,其本質(zhì)上是一個 Java應(yīng)用程序,那么又如何部署呢?這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目打jar包與war包的詳細(xì)步驟,需要的朋友可以參考下2023-02-02