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