Spring Boot 属性配置的方式有以下几种,优先级分别从高到底排列

1、Devtool全局配置

2、测试环境@TestPropertySouce注解

3、测试环境properties属性

4、命令行参数

5、SPRING_APPLICATION_JSON属性

6、ServletConfig初始化参数

7、ServletContext初始化参数

8、JNDI属性

9、JAVA系统属性

10、操作系统环境变量

11、RandomValuePropertySource随机值属性

12、jar包外的application-{profile}.properties

13、jar包内的application-{profile}.properties

14、jar包外的application.properties

15、jar包内的application.properties

16、@PropertySource绑定配置

17、默认属性

 

 下面分别从低优先级开始

17、默认属性

1)增加属性weburl的值为www.baidu.com

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Sb2Application {

	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(Sb2Application.class);
		Properties properties = new Properties();
		properties.setProperty("weburl","www.baidu.com");
		springApplication.setDefaultProperties(properties);
		springApplication.run(args);
	}

}

  

2)然后通过一个启动类加载器打印属性weburl

@Component
public class ResultCommandLineRunner implements CommandLineRunner, EnvironmentAware {

    private Environment env;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(env.getProperty("weburl"));
    }



    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}

  

3)输出结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

...

www.baidu.com

  

 

16、@PropertySource绑定配置

1)在前面的基础上,在resources文件夹下增加demo.properties文件

Spring Boot 属性配置

 

 

2) 然后使用PropertySource绑定配置

Spring Boot 属性配置

 

 

3) 运行程序

Spring Boot 属性配置

 

14、jar包外的application.properties

创建application.yml

Spring Boot 属性配置

 

 输出结果

Spring Boot 属性配置

 

 

然后在application.properties增加配置

Spring Boot 属性配置

 

 

输出结果为

Spring Boot 属性配置

 

 说明application.properties的优先级高于application.yml

 

13、jar包内的application-{profile}.properties

增加application-default.yml

Spring Boot 属性配置

 

 输出结果

Spring Boot 属性配置

 

 

增加application-default.properties

Spring Boot 属性配置

 

 输出结果

Spring Boot 属性配置

 

 

11、RandomValuePropertySource随机值属性

1)在application-default.properties中增加一个属性,值为随机数

Spring Boot 属性配置

 

 

2)在启动类加载器中输出这个属性

Spring Boot 属性配置

 

 3) 运行结果

Spring Boot 属性配置

 

 

10、操作系统环境变量

设置环境变量

Spring Boot 属性配置

 

 

 输出结果

Spring Boot 属性配置

 

 

 

9、JAVA系统属性

增加获取虚拟机名字

Spring Boot 属性配置

 

在启动类加载器中输出这个属性

Spring Boot 属性配置

 

 

 

 输出结果

Spring Boot 属性配置

 

 

 

 

6、ServletConfig初始化参数  7、ServletContext初始化参数

6、7两种主要是通过server.xxx设置一些属性

Spring Boot 属性配置

 

 

5、SPRING_APPLICATION_JSON属性

增加参数--SPRING_APPLICATION_JSON={\"weburl\":\"hello,SPRING_APPLICATION_JSON\"}

Spring Boot 属性配置

 

 

输出结果:

Spring Boot 属性配置

 

 

4、命令行参数

增加参数

Spring Boot 属性配置

 

 

输出结果

Spring Boot 属性配置

 

 

2、测试环境@TestPropertySouce注解  3、测试环境properties属性

这两个测试用的比较多

Spring Boot 属性配置

 

相关文章: