- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 如何解決springboot application.properties server.port配置
這篇文章主要介紹了如何解決springboot application.properties server.port配置問(wèn)題,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
近年來(lái),springboot以其快速構建方便便捷,開(kāi)箱即用,約定優(yōu)于配置(Convention Over Configuration)的特性深受廣大開(kāi)發(fā)者喜愛(ài)。
springboot已經(jīng)集成配置好了一套web開(kāi)發(fā)的默認配置,開(kāi)發(fā)者可以無(wú)需修改任何配置即可開(kāi)始一個(gè)web工程,但是實(shí)際情況中有時(shí)候開(kāi)發(fā)者還是需要修改部分默認配置項來(lái)使其更加契合自己的項目需求。
在配置服務(wù)啟動(dòng)的端口時(shí),springboot默認在application.properties配置文件中提供了server.port配置項來(lái)
讓開(kāi)發(fā)者自行配置服務(wù)啟動(dòng)端口號,**但是注意:**
#服務(wù)啟動(dòng)端口號 server.port=8889
該配置項要想生效其實(shí)是依賴(lài)于項目中內嵌的tomcat容器,如下圖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果pom中不引人上述依賴(lài),那么項目中不會(huì )導入內嵌tomcat的jar包,相應的application.properties配置文件中server.port配置項也將無(wú)法生效,因為該配置項實(shí)際上修改的就是內嵌tomcat的web端口號。
我們經(jīng)常配置server.port=xxx,但其實(shí)這是一個(gè)比較復雜的過(guò)程才生效的,這次講講生效的過(guò)程。
本質(zhì)來(lái)源于自動(dòng)配置
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration
TomcatServletWebServerFactory
為什么是這個(gè)類(lèi),核心是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設置好了。
這里使用函數式編程,lambda表達式,將port的值設置進(jìn)了
ConfigurableServletWebServerFactory ,即TomcatServletWebServerFactory對象
tomcat創(chuàng )建時(shí),會(huì )通過(guò)getBean方式獲取工廠(chǎng)
就是 TomcatServletWebServerFactory
然后設置connector,從TomcatServletWebServerFactory讀取port,設置connector,設置結束
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站