简体   繁体   中英

Spring Boot - Load application.properties/yml from dependent jar

I have a Spring Boot application where I would like to inject values into a @ConfigurationProperties bean in a specific order.

For example,

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

base-application.yml

myproperties: property1: some-value

The above class and properties file is in a jar file. In the mail application of my Spring Boot app (which has the above jar as dependency) I used @PropertySource(value = { "application.yml", "base-application.yml"}) but got null values in MyProperties .

I tried

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

}) 

as well, but that didn't work either.

If I add the myproperties.property1 value in application.yml then it works fine. Is it possible to inject property values from a property file which is inside of another jar? If so, what am I doing wrong here?

YAML files can't be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file. Check the docs here

如果属性文件在 jar 的 META-INF 目录中,则可以按如下方式加载它们:“classpath*:/META-INF/*.properties”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM