初识spring boot maven管理--属性文件配置

我不是女神ヾ 2022-08-26 14:19 264阅读 0赞

在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下:

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order:

  1. Command line arguments.
  2. Java System properties (System.getProperties()).
  3. OS environment variables.
  4. @PropertySource annotations on your @Configuration classes.
  5. Application properties outside of your packaged jar (application.properties including YAML and profile variants).
  6. Application properties packaged inside your jar (application.properties including YAML and profile variants).
  7. Default properties (specified using SpringApplication.setDefaultProperties).

英语不太好,大概的意思是:springboot将会使用指定的 PropertySource加载顺序加载属性文件,此目的是为了可以动态赋值!下面是顺序关系:

1、命令行参数

2、java的系统属性文件

3、OS环境变量

4、使用@PropertySource

5、在jar包外的application.properties

6、在jar包内的application.properties(位置在src目录下)

7、编码指定的位置 使用SpringApplication.setDefaultProperties方法

如果觉得不准确,请更正,谢谢!

属性文件配置好之后就可以在属性中使用

@Value(“${name}“)
private String name;

name的值将根据properties文件进行动态赋值!

发表评论

表情:
评论列表 (有 0 条评论,264人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot框架

    前面的铺垫文章已经连着写了六篇了,主要是介绍了Spring和SpringMVC框架,小伙伴们在学习的过程中大概也发现了这两个框架需要我们手动配置的地方非常多,不过做JavaEE