Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two yaml files for configuration properties

We are using a spring boot application, where properties are loaded from application.yml file instead of application.properties, located at src/main/resources/ which looks like below:

config: 
  host: localhost:8080  
  server: 123  

And they are being pulled in a .java file like this

@ConfigurationProperties( prefix="config")  
public class ConnectionImpl implements Connection{
  @Value("${config.host}")
  private Stringhost;
} 

I am able to retrieve properties this way. But we are trying to move the config properties from application.yml to a different .yml file which is located at a different location. (src/main/resources/env-config).
Now I am not able to retrieve properties same way, i.e, using @Value annotation. Is there any other annotation I need to add ?

like image 535
javaAndBeyond Avatar asked Oct 07 '16 15:10

javaAndBeyond


1 Answers

From the documentation:

SpringApplication will load properties from application.properties (or application.yml) files in the following locations and add them to the Spring Environment:

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The class path root

If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).

The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.config.location. This search path is ordered from lowest to highest precedence (file:config/ wins). If you do specify your own locations, they take precedence over all of the default locations and use the same lowest to highest precedence ordering. In that way you can set up default values for your application in application.properties (or whatever other basename you choose with spring.config.name) and override it at runtime with a different file, keeping the defaults.

You need to supply a command line argument that tells SpringApplication where specifically to look. If everything in resources/ is added to the classpath root, then your command line would look like:

java -jar myproject.jar --Dspring.config.location=classpath:/env-config/service-config.yml

If you have a general application.yml under resources/, the properties in there will still be loaded but will take a lower precedence to the properties file specified on the command line.

like image 110
su45 Avatar answered Sep 21 '22 12:09

su45