Created
October 8, 2019 12:45
-
-
Save reyabreu/2f9e682b284385bdb9d696468b4eea25 to your computer and use it in GitHub Desktop.
Binding Spring Boot 2+ environment properties to a Map of entries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ConfigurationProperties("") | |
public class CustomProperties { | |
private Map<String, Object> mongodb = new HashMap<>(); | |
public Map<String, Object> getMongodb() { | |
return ImmutableMap.copyOf(mongodb); | |
} | |
public void setMongodb(Map<String, Object> mongodb) { | |
this.mongodb = mongodb; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongodb: | |
connectionURI: "uri" | |
dbname: "reyabreu-web" | |
linkagedbname: "links" | |
countersdbname: "counters" | |
credential_dbname: "admin" | |
username: "reyabreu_webServer" | |
password: "password" | |
truststore: | |
location: "/reyabreu/ssl/reyabreu-web-server/client.truststore.mongo.jks" | |
password: "123456" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
public class TestCustomProperties { | |
@Autowired | |
private CustomProperties _customProperties; | |
@Test | |
public void dbProperties_validFileEntries_areLoaded() { | |
assertThat(_customProperties.getMongodb()).containsEntry("connectionURI", "uri"); | |
assertThat(_customProperties.getMongodb()).containsEntry("dbname", "reyabreu-web"); | |
assertThat(_customProperties.getMongodb()).containsEntry("linkagedbname", "links"); | |
assertThat(_customProperties.getMongodb()).containsEntry("countersdbname", "counters"); | |
assertThat(_customProperties.getMongodb()).containsEntry("credential_dbname", "admin"); | |
assertThat(_customProperties.getMongodb()).containsEntry("username", "reyabreu_webServer"); | |
assertThat(_customProperties.getMongodb()).containsEntry("password", "password"); | |
// assertThat(_customProperties.getMongodb()).containsEntry("foo", "bar"); | |
} | |
@Configuration | |
@EnableConfigurationProperties({ CustomProperties.class}) | |
static class TestConfig { | |
@Bean | |
public static YamlPropertiesFactoryBean yamlPropertiesFactoryBean() { | |
final YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean(); | |
factoryBean.setResources(new ClassPathResource("MongoProperties.yml")); | |
return factoryBean; | |
} | |
@Bean | |
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(YamlPropertiesFactoryBean yamlPropertiesFactoryBean) { | |
final PropertySourcesPlaceholderConfigurer propertiesConfigurer = new PropertySourcesPlaceholderConfigurer(); | |
propertiesConfigurer.setProperties(yamlPropertiesFactoryBean.getObject()); | |
return propertiesConfigurer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment