Reputation: 9141
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
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 fromapplication.properties
files in the following locations and adds them to the Spring Environment:
- A
/config
subdirectory of the current directory- The current directory
- A classpath
/config
package- 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
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
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