解決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ā)者可以無需修改任何配置即可開始一個(gè)web工程,但是實(shí)際情況中有時(shí)候開發(fā)者還是需要修改部分默認(rèn)配置項(xiàng)來使其更加契合自己的項(xiàng)目需求。
下面就其中一個(gè)小問題做個(gè)記錄
在配置服務(wù)啟動(dòng)的端口時(shí),springboot默認(rèn)在application.properties配置文件中提供了server.port配置項(xiàng)來
讓開發(fā)者自行配置服務(wù)啟動(dòng)端口號,**但是注意:**
#服務(wù)啟動(dòng)端口號 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í)這是一個(gè)比較復(fù)雜的過程才生效的,這次講講生效的過程。
1. autoConfigure
本質(zhì)來源于自動(dòng)配置
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration

TomcatServletWebServerFactory
為什么是這個(gè)類,核心是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)建時(shí),會通過getBean方式獲取工廠

就是 TomcatServletWebServerFactory

然后設(shè)置connector,從TomcatServletWebServerFactory讀取port,設(shè)置connector,設(shè)置結(jié)束

總結(jié)
Spring Boot在解耦的時(shí)候繞了很多彎,先@Bean factory對象,然后BeanPostProcess,然后啟動(dòng)embed tomcat 在factory 中new Tomcat 然后設(shè)置Connector,設(shè)置port。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 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)文章
SpringCloud+MyBatis分頁處理(前后端分離)
這篇文章主要為大家詳細(xì)介紹了SpringCloud+MyBatis分頁處理,前后端分離,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
關(guān)于Spring?Boot內(nèi)存泄露排查的記錄
這篇文章主要介紹了關(guān)于Spring?Boot內(nèi)存泄露排查的記錄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表
這篇文章主要介紹了關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09
解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問題
這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java?IO及BufferedReader.readline()出現(xiàn)的Bug
這篇文章主要介紹了Java?IO及BufferedReader.readline()出現(xiàn)的Bug,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Android bdflow數(shù)據(jù)庫神器的使用
這篇文章主要介紹了Android bdflow數(shù)據(jù)庫神器的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Java編寫時(shí)間工具類ZTDateTimeUtil的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫時(shí)間工具類ZTDateTimeUtil,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

