spring学习笔记(八)springboot 配置

2.7 配置文件占位符

1.随机数

${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}

2.占位符获取之前的值,如果没有可以用“:”指定默认值

person.yml

person:
  age: ${random.int}
  last-name: 张三${random.uuid}
  boss: false
  birth: 2019/10/02
  maps:
    ki: v1
    k2: v2
  list:
    - lisi
    - wangwu
  dog:
    name: ${person.hello:hello}dog
    age: 2

3.测试

2.8 Profile多环境配置

1.多Profile文件形式

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml

默认使用application.properties的配置

2.多profile文档块模式--yml

server:
  port: 8083
spring:
  profiles:
    active: prod

---
server:
  port: 8084
spring:
  profiles: dev

---
server:
  port: 8085
spring:
  profiles: prod

3.激活方式

1.在配置文件中指定

    spring.profiles.active=dev

2.命令行:

    打包 java -jar springboot-01-helloworld-quick-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

    可以在测试过程不用打包,直接配置命令行参数(Program arguments) --spring.profiles.active=dev

3.虚拟机参数(VM options)

    -Dspring.profiles.active=dev

2.9 配置文件加载位置

spring boot 启动会扫描以下位置的application.properties或application.yml文件作为Spring boot的默认配置文件

- file:./config/

- file ./

-classpath:/config/

-calsspath:/

以上是按照优先级从高到低的顺寻,高优先级的配置内容会覆盖低优先级的配置内容

所有位置的文件都会被加载,互补配置

我们也可以通过配置spring.config.location来改变默认的配置文件位置(通常情况下,打包后要改某个配置,则只需要把该配置修改并存到本地路径中),利用命令行参数的形式java -jar xxx.jar --spring.config.location=d:/application.properties,这个指定的配置文件和默认加载的配置文件会共同起作用你,形成互补配置 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值