On importing yaml config file , application.yml is getting overriden

Viewed 18

I am importing a yaml config file onto my main ServerConfig class. However when I am bringing up the server, it is setting the new yaml file as the applicationConfig & ignoring the properties set in application.yml. Ideally it should append the new config but without ignoring the base application.yml. How can I fix this?

Main config class

@Configuration
@EnableScheduling
@Import({
        NewPropertiesConfig.class
        })
@EnableConfigurationProperties({...})
public class ServerConfig extends AbstractConfig  {
...
}

NewPropertiesConfig.class

@Configuration
@PropertySource(
    value = {"classpath:abc-client.yaml"},
    factory = YamlPropertySourceFactory.class
)
public class NewPropertiesConfig {
    public NewPropertiesConfig() {
    }
}
1 Answers

Based on my understanding of the question, we are trying to load properties from two separate property files.

Based on the provided code example, the specified properties file is abc-client.yml, thus Spring will load properties from that file.

If we want Spring to load properties from multiple files, then we need to specify each file to use.

I have never tried loading properties from multiple yaml files, but here is how I do it with multiple .properties files.

@PropertySource("classpath:application.properties")
@PropertySource("classpath:abc-client.properties")
public class NewPropertiesConfig {
    //...
}

In the event of a name collision, the property from the last file wins, which is the desired behavior based on my understanding of the question. Not sure if this also works with yaml.

Related