diff --git a/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/ApiClientConfiguration.java b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/ApiClientConfiguration.java new file mode 100644 index 000000000000..db9d7f42f48c --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/ApiClientConfiguration.java @@ -0,0 +1,42 @@ +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; +import java.util.Map; + +public class ApiClientConfiguration implements BeanDefinitionRegistryPostProcessor { + private static final String API_CLIENT_BEAN_NAME = "apiClient_"; + List clients; + + public ApiClientConfiguration(Environment environment) { + Binder binder = Binder.get(environment); + List 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 { + } +} \ No newline at end of file diff --git a/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/RegistryPostProcessorApplication.java b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/RegistryPostProcessorApplication.java new file mode 100644 index 000000000000..311f72e0fa5b --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/RegistryPostProcessorApplication.java @@ -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); + } + +} + diff --git a/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/bean/ApiClient.java b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/bean/ApiClient.java new file mode 100644 index 000000000000..00d7f58a9ab8 --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/registrypostprocessor/bean/ApiClient.java @@ -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; + } + +} \ No newline at end of file diff --git a/spring-di-4/src/test/java/com/baeldung/registrypostprocessor/ApiClientConfigurationTest.java b/spring-di-4/src/test/java/com/baeldung/registrypostprocessor/ApiClientConfigurationTest.java new file mode 100644 index 000000000000..6e38152332b6 --- /dev/null +++ b/spring-di-4/src/test/java/com/baeldung/registrypostprocessor/ApiClientConfigurationTest.java @@ -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 ApiClientConfigurationTest { + @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()); + } +} \ No newline at end of file