Merge pull request #16029 from abh1navv/BAEL-7521_register_beans
Dynamically register Spring Beans based on properties
This commit is contained in:
commit
846396c6f7
|
@ -24,5 +24,15 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.registrypostprocessor.RegistryPostProcessorApplication</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.registrypostprocessor;
|
||||
|
||||
import com.baeldung.registrypostprocessor.bean.ApiClient;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ApiClientConfiguration implements BeanDefinitionRegistryPostProcessor {
|
||||
private static final String API_CLIENT_BEAN_NAME = "apiClient_";
|
||||
List<ApiClient> clients;
|
||||
|
||||
public ApiClientConfiguration(Environment environment) {
|
||||
Binder binder = Binder.get(environment);
|
||||
List<HashMap> properties = binder.bind("api.clients", Bindable.listOf(HashMap.class)).get();
|
||||
clients = properties.stream().map(client -> new ApiClient(String.valueOf(client.get("name")),
|
||||
String.valueOf(client.get("url")), String.valueOf(client.get("key")))).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||
clients.forEach(client -> {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ApiClient.class);
|
||||
builder.addPropertyValue("name", client.getName());
|
||||
builder.addPropertyValue("url", client.getUrl());
|
||||
builder.addPropertyValue("key", client.getKey());
|
||||
registry.registerBeanDefinition(API_CLIENT_BEAN_NAME + client.getName(), builder.getBeanDefinition());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.registrypostprocessor;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class RegistryPostProcessorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RegistryPostProcessorApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApiClientConfiguration apiClientConfiguration(ConfigurableEnvironment environment) {
|
||||
return new ApiClientConfiguration(environment);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.registrypostprocessor.bean;
|
||||
|
||||
public class ApiClient {
|
||||
private String name;
|
||||
private String url;
|
||||
private String key;
|
||||
|
||||
public ApiClient(String name, String url, String key) {
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public ApiClient() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getConnectionProperties() {
|
||||
return "Connecting to " + name + " at " + url;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +1,9 @@
|
|||
ambiguous-bean: 'A'
|
||||
ambiguous-bean: 'A'
|
||||
api:
|
||||
clients:
|
||||
- name: example
|
||||
url: https://api.example.com
|
||||
key: 12345
|
||||
- name: anotherexample
|
||||
url: https://api.anotherexample.com
|
||||
key: 67890
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.registrypostprocessor;
|
||||
|
||||
import com.baeldung.registrypostprocessor.bean.ApiClient;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@SpringBootTest(classes = RegistryPostProcessorApplication.class)
|
||||
public class ApiClientConfigurationUnitTest {
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenBeansRegistered_whenConnect_thenConnected() {
|
||||
ApiClient exampleClient = (ApiClient) context.getBean("apiClient_example");
|
||||
Assertions.assertEquals("Connecting to example at https://api.example.com", exampleClient.getConnectionProperties());
|
||||
|
||||
ApiClient anotherExampleClient = (ApiClient) context.getBean("apiClient_anotherexample");
|
||||
Assertions.assertEquals("Connecting to anotherexample at https://api.anotherexample.com", anotherExampleClient.getConnectionProperties());
|
||||
}
|
||||
}
|
|
@ -1 +1,9 @@
|
|||
ambiguous-bean: 'B'
|
||||
ambiguous-bean: 'B'
|
||||
api:
|
||||
clients:
|
||||
- name: example
|
||||
url: https://api.example.com
|
||||
apiKey: 12345
|
||||
- name: anotherexample
|
||||
url: https://api.anotherexample.com
|
||||
apiKey: 67890
|
Loading…
Reference in New Issue