iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >SpringBoot配置文件是怎样的
  • 751
分享到

SpringBoot配置文件是怎样的

2024-04-02 19:04:59 751人浏览 八月长安
摘要

本篇文章为大家展示了SpringBoot配置文件是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。环境:springboot2.2.13SpringBoot 中

本篇文章为大家展示了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

 自定义配置文件

  1. @Configuration 

  2. @ConfigurationProperties(prefix = "pack") 

  3. @PropertySource(value = "classpath:config.properties") 

  4. public class PackProperties { 

  5.   private String name; 

  6.   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:}。

加载制定环境的配置文件

一般我们都会为测试环境,开发环境,生产环境分别设置不同的配置文件,不同的环境加载不同的配置文件。

现有如下配置文件:

SpringBoot配置文件是怎样的

生成环境加载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

源码:

SpringBoot配置文件是怎样的

通过代码读取配置文件

@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工具类来加载资源

SpringBoot配置文件是怎样的

上述内容就是SpringBoot配置文件是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网html频道。

--结束END--

本文标题: SpringBoot配置文件是怎样的

本文链接: https://www.lsjlt.com/news/82074.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作