You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用启动时如何获取context-path、info配置及端口等信息

Spring Boot启动时获取配置信息的实现方案

方案一:通过@Value注解直接注入(适合少量配置项场景)

直接将配置项路径写到@Value注解中,注入到需要使用的Bean属性中即可。你可以通过实现ApplicationRunner接口,在Spring容器初始化完成后自动执行代码输出配置信息。

代码示例

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class AppStartupInfoLogger implements ApplicationRunner {
    // 注入服务端口
    @Value("${server.port}")
    private Integer serverPort;
    // 注入context-path,冒号后为默认值,避免无配置时启动报错
    @Value("${server.servlet.context-path:}")
    private String contextPath;
    // 注入info下的应用配置
    @Value("${info.app.name}")
    private String appName;
    @Value("${info.app.version}")
    private String appVersion;
    @Value("${info.app.description}")
    private String appDescription;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("=================== 服务启动成功 配置信息如下 ===================");
        System.out.println("服务端口:" + serverPort);
        System.out.println("访问前缀:" + contextPath);
        System.out.println("应用名称:" + appName);
        System.out.println("应用版本:" + appVersion);
        System.out.println("应用描述:" + appDescription);
    }
}

方案二:通过@ConfigurationProperties批量绑定(适合多配置项场景)

如果需要管理的配置项较多,推荐用这种方式统一绑定管理,避免散落的@Value注解难以维护。

步骤1:编写配置绑定类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "info.app")
public class AppInfoProperties {
    // 属性名要和配置文件中的key保持一致,必须提供getter、setter方法
    private String name;
    private String description;
    private String version;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
}

步骤2:启动时读取配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class AppStartupInfoLogger implements ApplicationRunner {
    @Value("${server.port}")
    private Integer serverPort;
    @Value("${server.servlet.context-path:}")
    private String contextPath;
    // 注入绑定好的配置对象
    @Autowired
    private AppInfoProperties appInfoProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("=================== 服务启动成功 配置信息如下 ===================");
        System.out.println("服务端口:" + serverPort);
        System.out.println("访问前缀:" + contextPath);
        System.out.println("应用名称:" + appInfoProperties.getName());
        System.out.println("应用版本:" + appInfoProperties.getVersion());
        System.out.println("应用描述:" + appInfoProperties.getDescription());
    }
}

注意事项

  • 如果使用Spring Boot 2.2及以上版本,可在启动类添加@ConfigurationPropertiesScan注解,配置绑定类不需要再加@Component注解,框架会自动扫描识别。
  • 如果配置了server.port=0使用随机端口,通过@Value注入的端口值为0,无法获取实际启动端口,需要通过注入ServletWebServerApplicationContext获取真实端口:
    @Autowired
    private ServletWebServerApplicationContext webServerContext;
    
    // 调用方法获取实际端口
    int realPort = webServerContext.getWebServer().getPort();
    

内容的提问来源于stack exchange,提问作者Tiee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:24:03