SpringBoot 如何优雅读取配置文件?10分钟教你搞定

SpringBoot 如何优雅读取配置文件?10分钟教你搞定,第1张

很多时候我们需要将一些常用的配置信息比如阿里云 oss 配置、发送短信的相关信息配置等等放到配置文件中。

下面我们来看一下 Spring 为我们提供了哪些方式帮助我们从配置文件中读取这些配置信息。

application.yml 内容如下:

wuhan2020: 2020年初武汉爆发了新型冠状病毒,疫情严重,但是,我相信一切都会过去!武汉加油!中国加油!my-profile:name: Guide哥email: koushuangbwcx@163.comlibrary:location: 湖北武汉加油中国加油books:    -name: 天才基本法description: 二十二岁的林朝夕在父亲确诊阿尔茨海默病这天,得知自己暗恋多年的校园男神裴之即将出国深造的消息——对方考取的学校,恰是父亲当年为她放弃的那所。    -name: 时间的秩序description: 为什么我们记得过去,而非未来?时间“流逝”意味着什么?是我们存在于时间之内,还是时间存在于我们之中?卡洛·罗韦利用诗意的文字,邀请我们思考这一亘古难题——时间的本质。    -name: 了不起的我description: 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?

1.通过 @value 读取比较简单的配置信息

使用 @Value("${property}") 读取比较简单的配置信息:

@Value("${wuhan2020}")String wuhan2020

需要注意的是 @value这种方式是不被推荐的,Spring 比较建议的是下面几种读取配置信息的方式。

2.通过@ConfigurationProperties读取并与 bean 绑定

LibraryProperties 类上加了 @Component 注解,我们可以像使用普通 bean 一样将其注入到类中使用。

importlombok.Getterimportlombok.Setterimportlombok.ToStringimportorg.springframework.boot.context.properties.ConfigurationPropertiesimportorg.springframework.context.annotation.Configurationimportorg.springframework.stereotype.Componentimportjava.util.List@Component@ConfigurationProperties(prefix ="library")@Setter@Getter@ToStringclassLibraryProperties{privateString locationprivateList books@Setter@Getter@ToStringstaticclassBook{        String name        String description    }}

这个时候你就可以像使用普通 bean 一样,将其注入到类中使用:

packagecn.javaguide.readconfigpropertiesimportorg.springframework.beans.factory.InitializingBeanimportorg.springframework.boot.SpringApplicationimportorg.springframework.boot.autoconfigure.SpringBootApplication/** *@authorshuang.kou */@SpringBootApplicationpublicclassReadConfigPropertiesApplicationimplementsInitializingBean{privatefinalLibraryProperties librarypublicReadConfigPropertiesApplication(LibraryProperties library){this.library = library    }publicstaticvoidmain(String[] args){        SpringApplication.run(ReadConfigPropertiesApplication.class,args)    }@OverridepublicvoidafterPropertiesSet(){        System.out.println(library.getLocation())        System.out.println(library.getBooks())    }}

控制台输出:

湖北武汉加油中国加油[LibraryProperties.Book(name=天才基本法, description........]

3.通过@ConfigurationProperties读取并校验

我们先将application.yml修改为如下内容,明显看出这不是一个正确的 email 格式:

my-profile:name: Guide哥email: koushuangbwcx@

ProfileProperties 类没有加 @Component 注解。我们在我们要使用ProfileProperties 的地方使用@

EnableConfigurationProperties注册我们的配置 bean:

importlombok.Getterimportlombok.Setterimportlombok.ToStringimportorg.springframework.boot.context.properties.ConfigurationPropertiesimportorg.springframework.stereotype.Componentimportorg.springframework.validation.annotation.Validatedimportjavax.validation.constraints.Emailimportjavax.validation.constraints.NotEmpty/***@authorshuang.kou*/@Getter@Setter@ToString@ConfigurationProperties("my-profile")@ValidatedpublicclassProfileProperties{@NotEmptyprivateString name@Email@NotEmptyprivateString email//配置文件中没有读取到的话就用默认值privateBooleanhandsome =Boolean.TRUE}

具体使用:

packagecn.javaguide.readconfigpropertiesimportorg.springframework.beans.factory.InitializingBeanimportorg.springframework.beans.factory.annotation.Valueimportorg.springframework.boot.SpringApplicationimportorg.springframework.boot.autoconfigure.SpringBootApplicationimportorg.springframework.boot.context.properties.EnableConfigurationProperties/** *@authorshuang.kou */@SpringBootApplication@EnableConfigurationProperties(ProfileProperties.class)publicclassReadConfigPropertiesApplicationimplementsInitializingBean{privatefinalProfileProperties profilePropertiespublicReadConfigPropertiesApplication(ProfileProperties profileProperties){this.profileProperties = profileProperties    }publicstaticvoidmain(String[] args){        SpringApplication.run(ReadConfigPropertiesApplication.class,args)    }@OverridepublicvoidafterPropertiesSet(){        System.out.println(profileProperties.toString())    }}

因为我们的邮箱格式不正确,所以程序运行的时候就报错,根本运行不起来,保证了数据类型的安全性:

Binding to target org.springframework.boot.context.properties.bind.BindException:Failedtobindpropertiesunder'my-profile'to cn.javaguide.readconfigproperties.ProfileProperties failed:Property:my-profile.emailValue:koushuangbwcx@Origin:classpathresource[application.yml]:5:10Reason:mustbeawell-formedemailaddress

我们把邮箱测试改为正确的之后再运行,控制台就能成功打印出读取到的信息:

ProfileProperties(name=Guide哥, email=koushuangbwcx@163.com, handsome=true)

4.@PropertySource读取指定 properties 文件

importlombok.Getterimportlombok.Setterimportorg.springframework.beans.factory.annotation.Valueimportorg.springframework.context.annotation.PropertySourceimportorg.springframework.stereotype.Component@Component@PropertySource("classpath:website.properties")@Getter@SetterclassWebSite{@Value("${url}")privateString url}

使用:

@Autowiredprivate WebSite webSiteSystem.out.println(webSite.getUrl())//https://javaguide.cn/

5.题外话:Spring 加载配置文件的优先级

Spring 读取配置文件也是有优先级的,直接上图:

原文链接:https://www.toutiao.com/a6791445278911103500/?log_from=7f5fb8f9b4b47_1640606437752

Springboot 读取配置文件(application.yaml, application.properties)的过程发生在SpringApplication#prepareEnvironment() 阶段,而prepareEnvironment又属于整个Springboot 应用启动的非常前置阶段,因为Environment的准备是后续bean创建的基础。让我们来一探启动是的详细code。除去StopWatch这些code,可以发现prepareEnvironment 发生在SpringApplication#run 这在整个应用启动的多步实质性 *** 作中几乎是第一步。

而prepareEnvironment中最重要的是通过触发listener(EventPublishingRunListener)来通过SimpleApplicationEventMulticaster#multicastEvent发出ApplicationEnvironmentPreparedEvent。

而SimpleApplicationEventMulticaster#multicastEvent的实现其实也很简单,找到相关的监听ApplicationEnvironmentPreparedEvent的listener,然后一个个的调用他们的Listener#onApplicationEvent(event)方法,而这其中就包括了处理configuration文件的listener。

在Springboot 2.4.0 之前这个处理configuration 文件的lister是ConfigFileApplicationListener,在2.4.0之后,处理configuration 文件的lister是EnvironmentPostProcessorApplicationListener,并且对configuration文件的加载做了较大的改变,导致一些行为可能出现了变化,这也就是下面要详细讲的内容。

Springboot 2.4.0之后,configuration 文件的load顺序按照优先级是如下顺序(序号大的会被小的覆盖):

和之前版本比较,整体的属性加载顺序并无调整,只有Application properties(14,15)这里有顺序的调整,具体调整为:

如果存在多个active的profiles,例如[Test, Dev], 那么对于同时存在两个profile 配置文件中的配置,后面的profile里的配置(Dev)会覆盖前面profile(Test)里配置的值。

前面讲了这么多,终于要引出Springboot 2.4之后配置文件加载的行为变化了。

考虑这样的情况,如果我想在跑Springboot test的时候指定特定的profile,那么可以在Test class中加入@ActiveProfile("Test")。 如果我的应用中存在ApplicationEnvironmentPreparedEvent的某个自定义listener中,会根据当前environment 设置profile,如env.addActiveProfile("Dev")。

当前就会有两个active profile,由于springboot-test会在调用application#run 前利用DefaultActiveProfilesResolver把@ActiveProfile注解定义的profile(Test)先加入了active的profile,等test run的时候 env.addActiveProfile("Dev") 又会把"Dev"也作为active profile 加入,这时候当前的active profile便为["Test", "Dev"]。

据上面介绍,后面的profile(Dev)对应的configuration 会覆盖前面的(Test)。可Springboot 2.4.0之前的版本为我们做了调整,让Test class中@ActiveProfile内定义的profile所对应的配置文件成为最高优先级。

刚才提到在Springboot 2.4.0 之前这个处理configuration 文件的lister是ConfigFileApplicationListener,我们

来看看ConfigFileApplicationListener的相关code。

查看initializeProfiles(),发现此时对profile的顺序做了调整,将activatedViaProperty (Test) 放在最后add,于是profile的顺序就变成了[Dev, Test]。

在profiles.poll()时原本profile的顺序已经倒了过来,已经变为[Dev, Test], 在load()方法中由于后置的Test profile,application-Test.yaml中的值最终生效了。

可是到了Springboot2.4.0之后,ConfigFileApplicationListener被deprecated了,取而代之的是EnvironmentPostProcessorApplicationListener,EnvironmentPostProcessorApplicationListener通过调用ConfigDataEnvironmentPostProcessor来完成configuration加载。

EnvironmentPostProcessorApplicationListener.java

ConfigDataEnvironmentPostProcessor.java

ConfigDataEnvironmentPostProcessor只是老老实实的set了active profile,并没有调换profile的顺序。最后调用定义在spring.factories中的resource loader class来load 配置文件。

YamlPropertySourceLoader.java

插一句,Springboot为我们提供了很好的yaml文件parse的code,当你需要解析yaml文件时不妨直接参考Springboot的YamlPropertySourceLoader

这样一旦应用升级到Springboot 2.4.0之后相同的test code会使用application-Dev.yaml中配置的值,造成了test结果的改变。

如果要解决这个问题,根据上面介绍的配置文件优先级顺序,可以在@SpringbootTest中设置properties 来作为最终的配置覆盖当前profile对应的配置。

了解一个框架很不容易,一个小小的变化都有可能造成应用的行为变化,唯有刨根问底,不断总结才是framework人解决一切问题的不变的方法论。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10024705.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存