Spring Boot 启动 参数设置

(1)读取优先顺序 

a - 命令行参数  --key=value 

引用

$ mvn spring-boot:run -Drun.arguments="--server.port=9090,--server.context-path=/test" 
$ java -jar target/xxx.jar --server.port=9090 --server.context-path=/test


b - JVM参数 -Dkey=value 

引用

$ mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Dserver.port=9090 -Dserver.context-path=/test" 
$ java -jar target/xxx.jar -Dserver.port=9090 -Dserver.context-path=/test


c - 环境变量 
d - application-{profile}.properties 
e - application.properties 

*** 还有很多地方可以设置,以上只是常用的! 

(2)文件格式 
同时支持properties或YAML。 

application.yml 

引用

prefix: 
    stringProp1: propValue1 
    stringProp2: propValue2 
    intProp1: 10 
    listProp: 
        - listValue1 
        - listValue2 
    mapProp: 
        key1: mapValue1 
        key2: mapValue2



application.properties 

引用

prefix.stringProp1=propValue1 
prefix.stringProp2=propValue2 
prefix.intProp1=10 
prefix.listProp[0]=listValue1 
prefix.listProp[1]=listValue2 
prefix.mapProp.key1=mapValue1 
prefix.mapProp.key2=mapValue2



变量可以嵌套使用 

引用

project.base-dir=file:///D:/springbootsample/spring-boot-demo1 
spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/



(3)文件位置 
src/main/resources/config/application.properties 
src/main/resources/application.properties 

*** 也可以通过@PropertySource("classpath:config.properties") 来读入任意其他设置文件。 

(4)多环境配置 
通过设置参数“spring.profiles.active”即可切换设置文件。 

引用

application.properties 
application-develop.properties 
application-test.properties 
application-product.properties



(5)设置值的Key支持Relaxed binding 
以下四种写法都是可以的。 

  • person.firstName 标准骆驼式语法
  • person.first-name 横线 多用于.properties 或 .yml
  • person.first_name 下划线 多用于.properties 或 .yml
  • PERSON_FIRST_NAME 大写 多用于环境变量



(6)读取配置值 

a - @Value() 

Java代码 

 收藏代码

  1. public class SamplePropertyLoading {  
  2.     @Value("${prefix.stringProp1}")  
  3.     private String stringProp1;  
  4.     @Value("${prefix.stringProp2}")  
  5.     private String stringProp2;  
  6.     @Value("${prefix.intProp1}")  
  7.     private Integer intProp1;  
  8.     @Value("${prefix.listProp}")  
  9.     private List<String> listProp;  
  10.     @Value("${prefix.mapProp}")  
  11.     private Map<String, String> mapProp;  
  12.     // ...  
  13. }  



@Value支持二元操作符并支持嵌套: 

引用

#{expression?:default value} 
${property:default value}



b - @ConfigurationProperties 

Java代码 

 收藏代码

  1. @Component  
  2. @ConfigurationProperties(prefix = "prefix")  
  3. public class SampleProperty {  
  4.     private String stringProp1;  
  5.     private String stringProp2;  
  6.     private Integer intProp1;  
  7.     private List<String> listProp;  
  8.     private Map<String, String> mapProp;  
  9.     // ...  
  10. }  



c - Environment 

Java代码 

 收藏代码

  1. @Autowired  
  2. private Environment env;  
  3.   
  4. String errorPath = env.getProperty("server.error.path");  



(7)常用设置 

官方文档里有详细的设置说明,用什么设置什么即可。 
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 
 

引用

server.port= 
server.context-path= 
server.session.timeout= 

logging.level= 

spring.messages.cache-seconds= 
spring.thymeleaf.cache= 
spring.velocity.cache= 

spring.datasource.url= 
spring.datasource.username= 
spring.datasource.password= 
spring.datasource.driver-class-name= 

spring.mvc.throw-exception-if-no-handler-found=

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值