forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-7521 - dynamically registering spring beans
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
spring-di-4/src/main/java/com/baeldung/registrypostprocessor/ApiClientConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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 { | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...-4/src/main/java/com/baeldung/registrypostprocessor/RegistryPostProcessorApplication.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
45 changes: 45 additions & 0 deletions
45
spring-di-4/src/main/java/com/baeldung/registrypostprocessor/bean/ApiClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
spring-di-4/src/test/java/com/baeldung/registrypostprocessor/ApiClientConfigurationTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |