温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot中的properties参数配置详解

发布时间:2020-10-01 12:52:29 来源:脚本之家 阅读:180 作者:给人生一个梦 栏目:编程语言

application.properties

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

src\main\resources
src\main\resources\config

配置系统参数

在application.properties中可配置一些系统参数,spring boot会自动加载这个参数到相应的功能,如下

#端口,默认为8080 
server.port=80 
#访问路径,默认为/ 
server.context-path=/test 
#输出日志文件,默认不输出  
logging.file=/log.txt 
#修改日志级别,默认为INFO 
logging.level.root=DEBUG 

自定义properties文件

在spring boot启动类或配置类中添加以下注解,可在启动时载入自定义的配置文件

@PropertySource("classpath:config/xxx.properties") 

如果要同时载入多个文件

@PropertySource(value={"classpath:config/a.properties","classpath:config/b.properties"}) 

自定义参数

以自命名配置一些参数,如

key1=values1 
key2=values2 

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

@RestController 
public class SampleController { 
  @Value(value="${key1}") 
  private String key; 

批量注入到类变量

在properties中配置两个以a为前缀的参数

a.key1=values1 
a.key2=values2 

在JAVA中用@ConfigurationProperties 将以a为前缀的参数注入到当前变量中,需要有setXxx()方法

@RestController 
@ConfigurationProperties(prefix = "a") 
public class SampleController { 
  private String key1; 
  private String key2; 
  public void setKey1(String key1) { 
    this.key1 = key1; 
  } 
  public void setKey2(String key2) { 
    this.key2 = key2; 
  } 

总结

以上所述是小编给大家介绍的spring boot中的properties参数配置详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI