Spring Boot - 从依赖的 jar 加载 application.properties/yml

adi*_*adi 5 java spring

我有一个 Spring Boot 应用程序,我想在其中以特定顺序将值注入 @ConfigurationProperties bean。

例如,

@ConfigurationProperties("myproperties") class MyProperties { private String property1; .... }

基础应用程序.yml

myproperties: property1: some-value

上面的类和属性文件在一个 jar 文件中。在我的 Spring Boot 应用程序(具有上述 jar 作为依赖项)的邮件应用程序中,我使用了@PropertySource(value = { "application.yml", "base-application.yml"})nullMyProperties.

我试过

@PropertySources({
    @PropertySource("classpath:application.yml"),
    @PropertySource("classpath*:base-application.yml")

}) 
Run Code Online (Sandbox Code Playgroud)

同样,但这也不起作用。

如果我在 application.yml 中添加 myproperties.property1 值,那么它工作正常。是否可以从另一个 jar 内的属性文件中注入属性值?如果是这样,我在这里做错了什么?

小智 5

\n

YAML 文件可以通过 @PropertySource 注解加载\xe2\x80\x99。因此,如果您需要以这种方式加载值,则需要使用属性文件。\n 请在此处查看文档

\n
\n