当前栏目
SpringBoot配置文件你了解多少?
环境:springboot2.2.13
SpringBoot 中有以下两种配置文件
- bootstrap (.yml 或者 .properties)
- application (.yml 或者 .properties)
接下来说下这两个配置文件有什么区别!
bootstrap/ application 的区别
bootstrap.yml(bootstrap.properties)先加载
application.yml(application.properties)后加载
bootstrap.yml 用于应用程序上下文的引导阶段,由父Spring ApplicationContext加载。父ApplicationContext 被加载到使用application.yml的之前。
在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖也就是说bootstrap中的配置不能被覆盖。
bootstrap/ application 的应用场景
application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。
bootstrap 配置文件有以下几个应用场景。
- 使用 Spring Cloud Config 配置中心时,这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;
- 一些固定的不能被覆盖的属性
- 一些加密/解密的场景;
以下是来自官方对bootstrap.[yml/properties]的一个说明:
- A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. This context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. The two contexts share an Environment, which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration.
- The bootstrap context uses a different convention for locating external configuration than the main application context. Instead of application.yml (or .properties), you can use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate
自定义配置文件
- @Configuration
- @ConfigurationProperties(prefix = "pack")
- @PropertySource(value = "classpath:config.properties")
- public class PackProperties {
- private String name;
- private Integer age ;
- }
- pack.name=xxxx
- pack.age=10
注意:@PropertySource只能加载properties文件不能加载yml文件。
导入Spring XML文件
- @Configuration
- @ImportResource(locations = {"classpath:application-jdbc.xml", "classpath:application-aop.xml"})
- public class ResourceConfig {
- }
application-jdbc.xml,application-aop.xml两个配置文件必须是spring的配置文件。
配置文件默认值
有如下代码:
- @Value("${pack.name}")
- private String name ;
当配置文件中没有配置pack.name时,这时候服务启动会报错,这时候我们可以通过设置默认值来防止错误。
- @Value("${pack.name:xx}")
- private String name ;
这里冒号 “:” 后面就是设置的默认值,这里也可以什么也不写${pack.name:}。
加载制定环境的配置文件
一般我们都会为测试环境,开发环境,生产环境分别设置不同的配置文件,不同的环境加载不同的配置文件。
现有如下配置文件:
生成环境加载prod文件,测试环境加载test文件,开发环境加载dev文件,只需在application.yml配置文件中加入如下配置。
- spring:
- profiles:
- active:
- - dev
这是在配置文件中写死,我们也可以在命令行制定
- java -jar xxxxx.jar --spring.profiles.active=dev
通过include包含多个配置文件,include是与环境无关的(也就是不管是什么环境都会加载)
- spring:
- profiles:
- active:
- - dev
- include:
- - cloud
- - secret
配置文件加载顺序
查看
ConfigFileApplicationListener.java源码,该文件中定义了从哪些地方加载配置文件。
加载顺序:
- file:./config/
- file:./config/*/
- file:./
- classpath:config/
- classpath:
优先级从高到低,高的覆盖低的。
默认情况下加载的是application.yml或application.properties文件。
在启动应用程序的时候可以制定配置文件的名称
- java -jar xxxxx.jar --spring.config.name=myapp
源码:
通过代码读取配置文件
- @SpringBootApplication
- public class SpringBootJwtApplication implements ApplicationRunner{
- public static void main(String[] args) {
- SpringApplication.run(SpringBootJwtApplication.class, args);
- }
- @Override
- public void run(ApplicationArguments args) throws Exception {
- ClassPathResource resource = new ClassPathResource("config.properties");
- try {
- Properties properties = PropertiesLoaderUtils.loadProperties(resource);
- System.out.println(properties) ;
- } catch (IOException e) {
- e.printStackTrace() ;
- }
- }
- }
这里通过PropertiesLoaderUtils工具类来加载资源
完毕!!!
相关文章
- 鲜为人知但很有用的 HTML 属性
- 翻转再翻转!有意思的水平横向溢出滚动
- 自定义计数器小技巧!CSS 实现长按点赞累加动画
- 过五关!React高频面试题指南
- 软件开发中的十个认知偏差
- 不需要 JS!仅用 CSS 也能达到监听页面滚动的效果!
- 一文读懂TypeScript类型兼容性
- Vue 的响应式原则与双向数据绑定
- 快速掌握 TypeScript 新语法:Infer Extends
- JWT教你如何证明你是我的人!
- 一篇带给你 V8 GC 的实现
- 面试官:请使用JS完成一个LRU缓存?
- 通过可视化来学习JavaScript事件循环
- 新的跨域策略:使用 COOP、COEP 为浏览器创建更安全的环境
- 为什么有人说 vite 快,有人却说 vite 慢?
- 种草 Vue3 中几个好玩的插件和配置
- 超全面的前端工程化配置指南
- Vue 状态管理未来样子
- Volatile关键字能保证原子性么?
- 面试突击:SpringBoot 有几种读取配置文件的方法?