Last active
February 18, 2018 14:46
-
-
Save mangan77/6b37f3159fafbc2c90a50f118c8ca426 to your computer and use it in GitHub Desktop.
Spring Boot: Populate configuration bean from external YAML config file.
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
database: | |
host: mydbhost |
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
@Configuration | |
@PropertySource(name = "databaseConfig", value = "classpath:database.yml", factory = YamlPropertySourceFactory.class) | |
@ConfigurationProperties(prefix = "database") | |
@Getter | |
@Setter | |
public class DatabaseConfiguration { | |
private String host; | |
} |
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
@NoArgsConstructor | |
public class YamlPropertySourceFactory implements PropertySourceFactory { | |
@Override | |
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { | |
return new YamlPropertySourceLoader().load(name, resource.getResource(), null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment