spring boot配置文件

本文详细介绍了Spring Boot中不同配置文件的应用方式,包括properties和yml格式的特点与区别,以及如何在不同环境下切换配置文件,并展示了如何在Java类中读取这些配置值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、配置文件

      1)application.properties(核心配置文件)

test.a= a_properties
test.b = b_${test.a}

      2)application.yml(核心配置文件)

test:
  a: a.yml
  b: b_${test.a}
  c: c.yml
  d: d_${test.c}

      3)other.properties(普通配置文件)

other.config= this is common config!

      4)类中读取属性值

@Value("${test.a}")
private String a;

@Value("${test.b}")
private String b;

@Value("${test.c}")
private String c;

@Value("${test.d}")
private String d;

private static String other;
static {
    ResourceBundle oBundle = ResourceBundle.getBundle("other");
    other = oBundle.getString("other.config");
}

@GetMapping("/test")
public String testConfig() {
    return a + " ; " + b + " ; " + c + " ; " + d + " ; " + other;
}

注:application.properties与application.yml都会自动加载,相同配置优先使用application.properties中设置的值

2、切换配置文件

      1)默认配置:application.yml

config:
  description: This is default config!

# 配置文件切换
#spring:
#  profiles:
#    active: test

      2)测试环境配置:application-test.yml

config:
  description: This is test config!

      3)生产环境配置:application-prod.yml

config:
  description: This is produce config!

3、设置context-path和端口号

#设置context-path和端口号,默认为"/"和8080
server:
  context-path: /v
  port: 18080
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值