I am using a combination of @PropertySource and @ConfigurationProperties but I want to overwrite them with an external properties file

删除回忆录丶 提交于 2019-12-12 10:15:29

问题


I have built a spring boot project.

Within it, I have a properties class that looks like this:

@Configuration
@PropertySource(value = "classpath:abc.properties")
@ConfigurationProperties
public class ABCProperties {

private Map<String, String> someUrls;

@Bean
public Map<String, String> someUrls() {
    return someUrls;
}

public Map<String, String> getSomeUrls() {
    return someUrls;
}

public void setSomeUrls(Map<String, String> someUrls) {
    this.someUrls = someUrls;
}
}

The contents of the abc.properties file look like this:

someUrls.urlA=someURL someUrls.urlB=someOtherURL

Within the project, in the resources folder (and test resources folder), I have provided an abc.properties file. Once built and run, the program can access the properties in the abc.properties file as expected (also can be accessed correctly in tests) - they map nicely to my url map.

So far so good.

However, the URLs to which I'm referring in the properties class aren't always the same. In some deployments, I want to overwrite them. With properties that are specified in my application.properties file, this is easy; simply having another, external application.properties file in the same (root) folder from which I've run the application will overwrite any properties that I put in it automatically.

What I would like to do is sometimes also put an external abc.properties file in the application's root folder and have it overwrite the abc.properties properties in the same way. Of course I tried this, but the properties from the abc.properties file that is built into the jar are retained and not overwritten. In fact, I'm not sure that the spring boot program recognises that there is another, external abc.properties file in its root folder at all.

I have read seemingly endlessly about how to achieve this without success. I have also tried specifying the following in my application.properties but it has made no difference (it also wouldn't be ideal even if it did work, because I'd rather not specify the direct path in all circumstances).

spring.config.location=/path/to/application/root/ spring.config.name=abc.properties

If I had to guess, the problem is that in the properties class I am pointing to 'classpath:..' and the external file is not actually on the classpath because it's.... well external.

but spring boot knows how to look in its root application folder for an overwriting application.properties so I'm hoping it is also able to look for other properties files to overwrite? I just cannot fathom how to achieve this.

and yes, I specifically want my properties in this abc.properties file rather than in the application properties.

Many thanks in advance for any help!


回答1:


Have you looked into PropertySources?

@PropertySources({
    @PropertySource("classpath:abc.properties"),
    @PropertySource(value="file:abc.properties", ignoreResourceNotFound=true)
})

From their doc:

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will 'win' and override.

Basically what you are saying here: load abc.properties from classpath, but also load abc.properties from this external location, just ignore the latter if it doesn't find it. It should override the properties you declared in the external file.

Haven't tested this, so hope it works out.



来源:https://stackoverflow.com/questions/47042237/i-am-using-a-combination-of-propertysource-and-configurationproperties-but-i-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!