当前位置: 首页 > news >正文

SpringBoot 端口配置

在Spring Boot中,配置应用程序的监听端口有多种方式。以下是常见的几种方法:

1. 通过 application.properties 或 application.yml 文件配置

application.properties

server.port=8081

application.yml

server:
  port: 8081

如果没有显式配置 server.port,Spring Boot 默认使用 8080 端口。

2. 通过命令行参数配置

在运行Spring Boot应用时,可以通过命令行参数动态指定端口。

java -jar myapp.jar --server.port=8082

3. 通过编程方式配置

可以在主应用程序类中通过编程方式设置端口。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.setPort(8084);
    }
}

通过以上方式将端口号修改为8081,并使用浏览器访问。

在这里插入图片描述

http://www.dtcms.com/a/47159.html

相关文章:

  • nlp第七节——文本匹配任务
  • 下载 MindSpore 配置 PyTorch环境
  • 使用消息队列怎样防止消息重复?
  • 苹果廉价机型 iPhone 16e 影像系统深度解析
  • Rust ~ Dyn Error
  • C语言:质因数分解
  • SpringCloud基础学习
  • C++11特性(笔记二lambda,function)
  • 《解锁万相2.1大模型:开启视频创作新世界》:此文为AI自动生成
  • 聊一聊 IM 如何优化监控
  • spring boot打包插件的问题
  • 计算机毕业设计SpringBoot+Vue.js医院资源管理系统(源码+文档+PPT+讲解)
  • 计算机毕业设计SpringBoot+Vue.js文档管理系统(源码+文档+PPT+讲解)
  • Linux常见基本指令(二)
  • STM32中的ADC
  • 第2章 windows故障排除(网络安全防御实战--蓝军武器库)
  • C++性能优化常用技巧
  • DeepSeek到TinyLSTM的知识蒸馏
  • 【Maven】入门介绍 与 安装、配置
  • [前端]Typescript中装饰器和泛型详解
  • 【软件测试】_使用selenium进行自动化测试示例
  • 神经网络 - 激活函数(ReLU 函数)
  • torch.einsum 的 10 个常见用法详解以及多头注意力实现
  • LeetCode 2353. 设计食物评分系统题解
  • 3.jvm的执行流程
  • 16. LangChain实战项目2——易速鲜花内部问答系统
  • C++小课堂——变量的声明,赋值和初始化
  • h5 IOS端渐变的兼容问题 渐变实现弧形效果
  • 深入解析数据倾斜:原因、影响与优化方案
  • 回忆Redis的持久化机制