spring boot(5)-properties参数配置


application.properties
application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件
src\main\resources
src\main\resources\config

配置系统参数
在application.properties中可配置一些系统参数,spring boot会自动加载这个参数到相应的功能,如下
  1. #端口,默认为8080  
  2. server.port=80  
  3. #访问路径,默认为/  
  4. server.context-path=/test  
  5. #输出日志文件,默认不输出   
  6. logging.file=/log.txt  
  7. #修改日志级别,默认为INFO  
  8. logging.level.root=DEBUG  

自定义properties文件
在spring boot启动类或配置类中添加以下注解,可在启动时载入自定义的配置文件
  1. @PropertySource("classpath:config/xxx.properties")  
如果要同时载入多个文件
  1. @PropertySource(value={"classpath:config/a.properties","classpath:config/b.properties"})  

自定义参数
以自命名配置一些参数,如
  1. key1=values1  
  2. key2=values2  

在JAVA代码中,使用@Value注解,在项目启动时会将自定义参数加载到全局变量,如下

  1. @RestController  
  2. public class SampleController {  
  3.     @Value(value="${key1}")  
  4.     private String key;  

批量注入到类变量
在properties中配置两个以a为前缀的参数
  1. a.key1=values1  
  2. a.key2=values2  
在JAVA中用 @ConfigurationProperties 将以a为前缀的参数注入到当前变量中,需要有setXxx()方法
  1. @RestController  
  2. @ConfigurationProperties(prefix = "a")  
  3. public class SampleController {  
  4.     private String key1;  
  5.     private String key2;  
  6.     public void setKey1(String key1) {  
  7.         this.key1 = key1;  
  8.     }  
  9.     public void setKey2(String key2) {  
  10.         this.key2 = key2;  
  11.     }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值