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

关于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)

如图编写实现类在customize方法中可设置容器端口号为8088

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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值