SpringBoot?server.port配置原理詳解
SpringBoot 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對(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ì)通過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。
server.port不起作用
啟動(dòng)項(xiàng)目報(bào)錯(cuò):
org.apache.catalina.LifecycleException: Protocol handler start failed
懷疑可能端口號(hào)沖突,在 application.properties 添加 server.port=8080 未生效
立刻百度一圈沒找到答案(感覺可能自己犯的錯(cuò)誤太低級(jí)),突然想起可能是環(huán)境配置里面的配置給覆蓋了
#讀取環(huán)境配置dev(開發(fā))/pro(生產(chǎn))/test(測試) spring.profiles.active=dev
找到 application-dev.properties、application-prod.properties 發(fā)現(xiàn)果然存在
按照配置的 dev 找到 application-dev.properties 修改 server.port=8080啟動(dòng)生效
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼
本文主要介紹了mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05SpringBoot整合Web開發(fā)之Json數(shù)據(jù)返回的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot整合Web開發(fā)其中Json數(shù)據(jù)返回的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Spring?Security實(shí)現(xiàn)HTTP認(rèn)證
本文主要介紹了Spring?Security實(shí)現(xiàn)HTTP認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式,在java開發(fā)中,經(jīng)常遇到需要調(diào)用第三方提供的接口服務(wù)的需求,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-08-08