Skip to content

Instantly share code, notes, and snippets.

@mangan77
Last active February 18, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mangan77/6b37f3159fafbc2c90a50f118c8ca426 to your computer and use it in GitHub Desktop.
Save mangan77/6b37f3159fafbc2c90a50f118c8ca426 to your computer and use it in GitHub Desktop.
Spring Boot: Populate configuration bean from external YAML config file.
database:
host: mydbhost
@Configuration
@PropertySource(name = "databaseConfig", value = "classpath:database.yml", factory = YamlPropertySourceFactory.class)
@ConfigurationProperties(prefix = "database")
@Getter
@Setter
public class DatabaseConfiguration {
private String host;
}
@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