Tristan
Tristan

Reputation: 9141

Spring boot property resolution

I am using this config code to initialize a datasource :

      @Bean(destroyMethod = "close")
      DataSource dataSource(Environment env) {
        HikariConfig dataSourceConfig = new HikariConfig();

        dataSourceConfig.setDriverClassName(env.getRequiredProperty(PROP_DB_DRIVER_CLASS));    
        dataSourceConfig.setJdbcUrl(env.getRequiredProperty(PROP_DB_URL));
        dataSourceConfig.setUsername(env.getRequiredProperty(PROP_DB_USER));
        dataSourceConfig.setPassword(env.getRequiredProperty(PROP_DB_PASSWORD));

        return new HikariDataSource(dataSourceConfig);    
      }

"env" being : "org.springframework.core.env.Environment"

Now when I run my Spring Boot app in Eclipse, I expect this code to get properties from the application.properties which is in the classpath (in project/src/main/resources), but it is using an other application.properties which is in project/config, why is this happening and how can I prevent this behavior ? (the files in project/config are files for other environments, not dev).

Upvotes: 0

Views: 1111

Answers (3)

Andy Wilkinson
Andy Wilkinson

Reputation: 116281

You can't prevent the behaviour you're seeing. The order of precedence of application.properties files is described in the documentation:

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

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

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

Your file in project/config is 1 in the list above and your file in project/src/main/resources is 4 in the list. It is therefore to be expected that the values in the file in project/config are used.

the files in project/config are files for other environments, not dev

This would seem to imply that you consider the file in src/main/resources to be for development. In that location it will, by default, be packaged in your application's jar file. As such, that location is more suited to properties that will be used by your application beyond development.

You could reverse your use of the two locations. Place your development time properties in a file in project or project/config and your properties for other environments in a file in src/main/resources.

Upvotes: 1

Thiru
Thiru

Reputation: 2709

Spring’s @Value annotation provides a convenient way to inject property values into components.

Basic syntax:

@Value("${some.key:my default value}")
private String stringWithDefaultValue;

If some.key cannot be resolved, then stringWithDefaultValue will be set to the default value of “my default value”.

Similar way, you have to inject all the properties mentioned that you need and use it while creating the bean.

More details here

Upvotes: 0

Gideon Balaganesan
Gideon Balaganesan

Reputation: 55

In Spring Boot, to get the config values from project/src/main/resources/application.properties, we can use @Value

@Value("${PROP_DB_DRIVER_CLASS}")
private String drivercClass;
@Value("${PROP_DB_URL}")
private String dbUrl;
@Value("${PROP_DB_USER}")
private String dbUser;
@Value("${PROP_DB_PASSWORD}")
private String dbPassword;

@Bean(destroyMethod = "close")
DataSource dataSource() {
    HikariConfig dataSourceConfig = new HikariConfig();

    dataSourceConfig.setDriverClassName(drivercClass);
    dataSourceConfig.setJdbcUrl(dbUrl);
    dataSourceConfig.setUsername(dbUser);
    dataSourceConfig.setPassword(dbPassword);

    return new HikariDataSource(dataSourceConfig);
}

Upvotes: 0

Related Questions