I want to configure properties for different profiles (production and dev).
That's my configuration:
@Configuration
public class PropertyPlaceholderConfig {
@Profile(Profiles.PRODUCTION)
static class ProductionConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigure()
throws IOException {
PropertySourcesPlaceholderConfigurer propertySources =
new PropertySourcesPlaceholderConfigurer();
propertySources.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
Resource[] resources =
ObjectArrays.concat(getCommonResources(), getProductionResources(), Resource.class);
propertySources.setLocations(resources);
retu propertySources;
}
private static Resource[] getProductionResources() throws IOException {
retu new PathMatchingResourcePatteResolver()
.getResources("classpath:properties/production/*.properties");
}
}
@Profile(Profiles.DEV)
static class DevConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigure()
throws IOException {
PropertySourcesPlaceholderConfigurer propertySources =
new PropertySourcesPlaceholderConfigurer();
propertySources.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
Resource[] resources =
ObjectArrays.concat(getCommonResources(), getDevResources(), Resource.class);
propertySources.setLocations(resources);
retu propertySources;
}
private static Resource[] getDevResources() throws IOException {
retu new PathMatchingResourcePatteResolver()
.getResources("classpath:properties/dev/*.properties");
}
}
private static Resource[] getCommonResources() throws IOException {
retu new PathMatchingResourcePatteResolver()
.getResources("classpath:properties/common/*.properties");
}
}
I printed current profile (dev) and loaded files (all have been correcly loaded into Resouce array). I want to use this loaded properties in database config. How to do it? I used:
@Import(PropertyPlaceholderConfig.class)
over the class, and:
@Autowired
private Environment environment;
but environment.getProperty("h2.driverClass") or environment.resolvePlaceholders("${h2.driverClass}") doesn't work.
How to use properties loaded by PropertySourcesPlaceholderConfigurer?
