亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot?server.port配置原理詳解

 更新時(shí)間:2021年12月30日 09:01:48   作者:fenglllle  
這篇文章主要介紹了Spring?Boot?server.port配置原理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

  • SpringBoot整合gateway全過程

    SpringBoot整合gateway全過程

    這篇文章主要介紹了SpringBoot整合gateway全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼

    mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼

    本文主要介紹了mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • SpringBoot整合Web開發(fā)之Json數(shù)據(jù)返回的實(shí)現(xiàn)

    SpringBoot整合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-08
  • 基于java Files類和Paths類的用法(詳解)

    基于java Files類和Paths類的用法(詳解)

    下面小編就為大家分享一篇基于java Files類和Paths類的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • Spring?Security實(shí)現(xiàn)HTTP認(rèn)證

    Spring?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-06
  • 使用@RequestParam 綁定List參數(shù)

    使用@RequestParam 綁定List參數(shù)

    這篇文章主要介紹了使用@RequestParam 綁定List參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中死鎖產(chǎn)生的原因有哪些?

    Java中死鎖產(chǎn)生的原因有哪些?

    這篇文章主要介紹了Java中死鎖產(chǎn)生的原因有哪些?死鎖即Dead?Lock指的是兩個(gè)或兩個(gè)以上的運(yùn)算單元,下文關(guān)于其產(chǎn)生的原因,需要的小伙伴可以參考一下
    2022-05-05
  • Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式總結(jié)

    Java實(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
  • Java中的類初始化解析

    Java中的類初始化解析

    這篇文章主要介紹了Java中的類初始化解析,類的初始化是一個(gè)Java類生命周期中的其中一個(gè)階段,初始化是類加載的最后一個(gè)階段,也正是在初始化階段,才會(huì)真正開始執(zhí)行類中所寫的Java代碼,需要的朋友可以參考下
    2023-08-08
  • SpringBoot中@Autowired生效方式詳解

    SpringBoot中@Autowired生效方式詳解

    @Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準(zhǔn)確地控制bean在何處如何自動(dòng)裝配的過程。在默認(rèn)情況下,該注解是類型驅(qū)動(dòng)的注入
    2022-06-06

最新評(píng)論