关于spring2.0的自定义配置端口问题

    xiaoxiao2022-07-07  202

    关于spring2.0的自定义配置Tomcat端口问题

    1.关于spring2.0的自定义配置端口问题

    最近本人新入坑了springboot,由于使用版本是2.0.9.RELEASE,故在学习入门篇springboot-helloworld的时候,便遇到了需要自定义Tomcat端口,由于本人电脑8080端口被占用,所以,很久之前就将Tomcat的默认端口改成了8888,但是大多数的springboot教程都没有教如何自定义Tomcat端口,从而成功运行helloworld。于是本人就在网上搜索,首先找到了

    通过实现EmbeddedServletContainerCustomizer接口自定义Tomcat端口(只适用于spring-boot1.0)

    网上有很多类似代码我再次不赘述

    2.Spring2.0废弃EmbeddedServletContainerCustomizer接口使用WebServerFactoryCustomizer代替(代码如下)、

    package com.shu.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; @SpringBootApplication public class HelloMainApplication implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { public static void main(String[] args) { SpringApplication.run(HelloMainApplication.class,args); } @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8888); } }

    3.实现效果如下

    当然有更简单的方法··· 上面是我初次接触springboot走的弯路 直接在application.properties中加入server.port=8080 即可自定义端口,相较于上文,这个确实很简单

    最新回复(0)