SpringBoot版本:1.5.13.RELEASE
对应官方文档链接:https://docs.spring.io/spring-boot/docs/1.5.13.RELEASE/reference/htmlsingle/

上一篇:SpringBoot 入门篇(三) SpringApplication


  SpringBoot外部配置属性值的方式有很多种,SpringBoot为这多种配置方式指定了优先级,在属性相同的情况下,优先级高的配置方式会覆盖优先级低的配置方式。当然,如果属性不相同,则这些配置方式中的属性值都会被加载。
  SpringBoot官方文档指明了这多种配置方式的优先级,按照从高到低排序如下:
  (1)如果使用了Devtools,则优先级最高的是在home目录下指定的Devtools全局配置文件~/.spring-boot-devtools.properties(优先级最高)。
  (2)测试用例中,标注了 @TestPropertySource 配置项;
  (3)测试用例中,@SpringBootTest 注解中的 properties 属性值;
  (4)命令行参数;
  (5)内嵌在环境变量或者系统变量中的SPRING_APPLICATION_JSON中的属性值;
  (6)ServletConfig 初始化的参数;
  (7)ServletContext 初始化的参数;
  (8)java:comp/env 中的JNDI属性值;
  (9)Java的系统变量,通过System.getProperties()方法获取;
  (10)操作系统的环境变量;
  (11)RandomValuePropertySource配置的${random.*}属性值;
  (12)不在项目打成可执行jar包中的application-{profile}.properties或者application-{profile}.yml文件;
  (13)项目打成可执行jar包中的application-{profile}.properties或者application-{profile}.yml文件;;
  (14)不在项目打成可执行jar包中的application.properties或者application.yml文件;
  (15)项目打成可执行jar包中的application.properties或者application.yml文件;
  (16)同时标注@Configuration和@PropertySource的类中,标注了@PropertySource指定的属性值;
  (17)在main方法中设置的SpringApplication.setDefaultProperties值(优先级最低)。
  在下面的例子中,包含第(4)、(14)、(15)、(16)、(17)条中的属性设置方式,这5种方式也是开发过程中用得最多的方式。需要说明的是,MyConfiguration 类中,使用@Value(“${name}”)来获取外部配置的值。

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);

        Map<String,Object> proMap = new HashMap<String, Object>();
        proMap.put("name","(17)在main方法中设置的SpringApplication.setDefaultProperties值。");
        application.setDefaultProperties(proMap);

        application.run(args);
    }


    @Autowired
    private MyConfiguration myConfiguration;

    @RequestMapping("/getName")
    public String getName(){
        return myConfiguration.getName();
    }
}
/**
 * Create by qiezhichao on 2018/6/14 0014 21:59
 */
@Configuration
@PropertySource(value= {"classpath:propertySource.properties"})
public class MyConfiguration {

    // @Value("${name}")来获取外部配置的值
    @Value("${name}")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  代码结构如图

spring 配置优先级 springboot 优先级_属性值

  配置命令行参数,其中–spring.config.location=X:/application.properties表示加载本地磁盘X下的 application.properties 文件。

spring 配置优先级 springboot 优先级_属性值_02

  执行main方法,在浏览器输入http://localhost:8080/getName 得到如下结果

spring 配置优先级 springboot 优先级_jar包_03


  对于随机值的配置,官方文档指明可以使用${random.*}(通常在application.properties或者application.yml文件中)来注入随机值。

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

  对于命令行参数,通过 java -jar app.jar –name=”SpringBoot” –server.port=9090 的方式来传递参数。参数用 –xxx=xxx 的形式传入。如果我们想禁用命令行参数,可以使用SpringApplication.setAddCommandLineProperties(false)的方法禁止命令行配置参数传入。

spring 配置优先级 springboot 优先级_优先级_04


上一篇:SpringBoot 入门篇(三) SpringApplication
下一篇:SpringBoot 入门篇(五) SpringBoot的配置文件