javaAndBeyond
javaAndBeyond

Reputation: 540

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 ?

Upvotes: 14

Views: 16704

Answers (3)

su45
su45

Reputation: 313

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.

Upvotes: 10

Michael Lihs
Michael Lihs

Reputation: 8220

Your question doesn't really say what you intend to do, but if you want to have a different configuration for different environments (e.g. development, test, production), there is a simple solution for that.

Place your config files in a file hierarchy like this inside your project:

src/
  main/
    resources/
      application.yml
      application-development.yml
      application-test.yml
      application-production.yml

When you now start your application with

java -jar mySpringApplication.jar -Dspring.profiles.active=development

the configuration from application.yml will be taken as a "base layer", overridden by the configuration in application-development.yml. By this, you can have "default" settings for all environments in application.yml and environment-specific configuration in the application-ENV.yml files. The same works for test and production.

Upvotes: 10

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

No.

You'll be in a much better position if you avoid hard-coding file path like that within your code base. @ConfigurationProperties used to have a locations attribute but it's deprecated and already removed in 1.5.

In Spring Boot, you configure the Environment which is a single source of truth for your configuration. Rather than having settings buried in code, you should configure Spring Boot to read the files that you want. Read the documentation for spring.config.location. If you want to do this in a more transparent manner, perhaps EnvironmentPostProcessor is what you need

Upvotes: 4

Related Questions