0%

springBoot配置文件

文章字数:324,阅读全文大约需要1分钟

application.properties是保存spring配置的地方。springBoot提供了很多模块,只需要在pom中引入模块化的starter POMs(spring-boot-starter-*,ru dara-jpa这些spring整合的模块)。除了这些模块的应用外,还能有其他特殊的使用场景。

自定义属性及加载

1
2
//key=value 类型
xxx.demo.test=test

在变量上加上注解,自动加载值

1
2
3
//属性名
@Value("${xxx.demo.test}")
private String testValue

参数间引用

1
2
xxx.demo.test=test
xxx.demo.info=this is a ${xxx.demo.test}

随机数

1
2
3
4
5
6
7
8
9
10
# 随机字符串
xxx.demo.test=${random.value}
# 随机int
xxx.demo.test=${random.int}
# 随机long
xxx.demo.test=${random.long}
# 10以内随机数
xxx.demo.test=${random.int(10)}
# 10-20间随机数
xxx.demo.test=${random.init(10,20)}

屏蔽命令行修改属性值

java -jar xxx.jar --server.port=888等价于在配置文件中设置xxx.jar,--是对于配置属性赋值的标识。

  • 屏蔽修改属性
    1
    SpringApplication.setAddCommandLineProperties(false)

    多环境配置

    就是生产、测试、开发环境都用各自的配置文件

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境
    在主配置文件application.propertiesspring.profiles.active配置机体需要那一份配置。(rest,dev,prod)
    也可以命令行选则
    1
    java -jar xxx.jar --spring.profiles.active=test