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