How do I dynamically define beans based on the application.yml file?
For example, the YAML file looks like this:
service:
host: http://localhost:8080/
account:
url: /account
content-type: application/json
registry:
url: /registry
content-type: application/xml
And this would dynamically create two HttpHeaders
with the Content-Type
header set.
Here's how I define the beans now:
@Bean
public HttpHeaders accountHeaders(
@Value("${service.account.content-type}") String contentType
) {
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_TYPE, contentType);
return headers;
}
@Bean
public HttpHeaders registryHeaders(
@Value("${service.registry.content-type}") String contentType
) {
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_TYPE, contentType);
return headers;
}
If I need to add more endpoints, I would need to copy and paste these beans, which I would like to avoid.
Note: these dynamic beans do not require any other beans. I'm not sure if that makes a difference. It just needs to load the configuration.