1、属性生效的优先级

  1. 默认属性(硬编码)
  2. @PropertySource 绑定的属性
  3. JAR 包内的 application.properies
  4. JAR 包外的 application.properies
  5. JAR 包内的 application-{profiles}.properies
  6. JAR 包外的 application-{profiles}.properies
  7. RandomValuePropertySource 随机值属性
  8. OS 系统环境变量
  9. Java 系统属性
  10. JNDI 属性
  11. ServletContext 初始化参数
  12. ServletConfig 初始化参数
  13. SPRING_APPLICATION_JSON 属性
  14. 命令行参数
  15. 测试环境properties 属性
  16. 测试环境@Test
  17. Devtools 全局配置

上面列出的属性的优先级按序号从低到高,即1优先级最低,17优先级最高

2、动手实验

2.1 默认属性(硬编码)

@SpringBootApplication
public class PropertyTestMainSpringApplication {public static void main(String[] args) {SpringApplication springApplication = new SpringApplication(PropertyTestMainSpringApplication.class);springApplication.setDefaultProperties(getDefaultProperties());springApplication.run(args);}private static Properties getDefaultProperties() {Properties defaultProperties = new Properties();defaultProperties.setProperty(Payment.PAY_TYPE, "alipay from hardcode");return defaultProperties;}
}
@Component
public class ResultCommandLineRunner implements CommandLineRunner , ApplicationRunner, EnvironmentAware {private Environment environment;@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;String payType = environment.getProperty(Payment.PAY_TYPE);System.out.println(payType);}@Overridepublic void run(String... args) throws Exception {}@Overridepublic void run(ApplicationArguments args) throws Exception {}
}

2.2 @PropertySource

在配置类上加上注解 @PropertySource({"payment.properties"})

@SpringBootApplication
@PropertySource({"payment.properties"})
public class PropertyTestMainSpringApplication {....
}

文件 resources/payment.properties

pay.type=alipay form payment.properties file

2.3 application.[properties/yml]

2.5

8.1、Spring Boot 属性配置相关推荐

  1. Spring Boot 属性配置和使用

    spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot 系列 Spring Boot 入门 S ...

  2. Spring Boot 属性配置你所不知道的细节

    今天我们要聊的这个问题,可能工作5年的资深程序员也不一定搞得很清楚,但是我敢保证在开发 Web 应用过程中大家都遇到过. 这个问题就是: Spring Boot 应用程序读取配置属性时,不同配置源的优 ...

  3. JSP如何获取Spring Boot的配置属性

    在Spring Boot + JSP的应用中,一般使用Session作为用户身份的认证, 有时候在JSP页面中需要读取配置在Spring Boot 的配置文件(比如application.proper ...

  4. spring boot自动配置

    首先,一般的java Web项目需要很多配置,web配置(web.xml).spring配置(默认叫applicationContext.xml),非常繁琐 而spring-boot-starter是 ...

  5. Spring Boot自动配置原理

    要了解Spring Boot的自动配置首先我需要先了解Spring Boot的两个机制,一个是配置文件如何进入到系统变成属性,二是如何将这些属性加入到容器中. 首先我们需要有一个组件,这个组件中的属性 ...

  6. Spring Boot - 自动配置实例解读

    文章目录 Pre 启用 debug=true输出自动配置 HttpEncodingAutoConfiguration 什么情况下,Spring Boot 会自动装配 HttpEncodingAutoC ...

  7. Spring Boot自动配置原理分析

    一.写在前面 随着时间的迁移Spring Boot 越来越多的出现在Java 后端程序员的视野中,Spring Boot 之所以会那么流行,很大的一个原因是自身集成了很多的Bean,简化了传统Srin ...

  8. 芋道 Spring Boot 自动配置原理

    转载自  芋道 Spring Boot 自动配置原理 1. 概述 友情提示:因为本文是分享 Spring Boot 自动配置的原理,所以需要胖友有使用过 Spring Boot 的经验.如果还没使用过 ...

  9. Spring Boot 自动配置的 “魔法” 是如何实现的?

    转载自  Spring Boot 自动配置的 "魔法" 是如何实现的? Spring Boot是Spring旗下众多的子项目之一,其理念是约定优于配置,它通过实现了自动配置(大多数 ...

最新文章

  1. Spark Streaming笔记整理(二):案例、SSC、数据源与自定义Receiver
  2. python填写excel-Python向excel中写入数据的方法
  3. HDU_1075 What Are You Talking About(Trie 树)
  4. 人生苦短,我学python之python+selenium select下拉框
  5. c语言的运算答案,C语言运算符与表达式的练习题答案.doc
  6. jvm配置参数,查看大对象直接分配到老年代
  7. Python中os模块使用方法
  8. 进行数据清洗_[经验漫谈]使用Python进行数据表格清洗和分析
  9. 系统协调服务器,协调网络系统 Collaboration network system
  10. jQuery - Ajax解决跨域问题
  11. IIS安装程序无法复制文件的问题 安装IIS提示安装程序无法复制文件staxmem.d...
  12. SVN 安装与使用教程 2020年9月更新最新教程
  13. 机器学习实战(一):K-近邻算法(史上最全干货) 算法总结+案例实战
  14. Raid5数据恢复原理_两块盘离线数据恢复方法
  15. 电脑如何备份重要文件?教你两种简单方法
  16. Python的excel工作簿写入与读取操作
  17. 【JavaScript】 基础知识
  18. 基于ArcGIS Pro 的AI地图配色工具
  19. 鸟哥的Linux私房菜(服务器)- 第十一章、远程联机服务器SSH / XDMCP / VNC / RDP
  20. build-resource-filtering解释

热门文章

  1. 指出Linux内核中boot,uBoot和Linux内核中涉及到的几个地址参数的理解
  2. php xml 接口调用,php的SimpleXML方法读写XML接口文件实例解析
  3. Python中的@classmethod修饰符
  4. Python函数式编程之Map
  5. span标签的取值与赋值
  6. 中兴myos和鸿蒙,继华为鸿蒙系统以后!中兴再次发布新系统MyOS:可媲美苹果
  7. 玩游戏用什么轴的机械键盘好_机械键盘下的“轴”到底是什么?
  8. 【电脑帮助】解决Wind10系统每次运行软件时都要出现提示框的问题
  9. MysqL的root用户不允许远程连接
  10. 第二章 ---- spring注解开发