Merge pull request #9227 from maciejglowka/BAEL-3829
BAEL-3829: example of dynamic bean autowiring in Spring
This commit is contained in:
commit
3d0645f9d8
spring-core-4/src
main/java/com/baeldung/dynamic/autowire
BeanFactoryDynamicAutowireService.javaCustomMapFromListDynamicAutowireService.javaDynamicAutowireConfig.javaGBRegionService.javaRegionService.javaUSRegionService.java
test/java/com/baeldung/dynamic/autowire
27
spring-core-4/src/main/java/com/baeldung/dynamic/autowire/BeanFactoryDynamicAutowireService.java
Normal file
27
spring-core-4/src/main/java/com/baeldung/dynamic/autowire/BeanFactoryDynamicAutowireService.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class BeanFactoryDynamicAutowireService {
|
||||
private static final String SERVICE_NAME_SUFFIX = "regionService";
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
public BeanFactoryDynamicAutowireService(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public boolean isServerActive(String isoCountryCode, int serverId) {
|
||||
RegionService service = beanFactory.getBean(getRegionServiceBeanName(isoCountryCode), RegionService.class);
|
||||
|
||||
return service.isServerActive(serverId);
|
||||
}
|
||||
|
||||
private String getRegionServiceBeanName(String isoCountryCode) {
|
||||
return isoCountryCode + SERVICE_NAME_SUFFIX;
|
||||
}
|
||||
|
||||
}
|
26
spring-core-4/src/main/java/com/baeldung/dynamic/autowire/CustomMapFromListDynamicAutowireService.java
Normal file
26
spring-core-4/src/main/java/com/baeldung/dynamic/autowire/CustomMapFromListDynamicAutowireService.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class CustomMapFromListDynamicAutowireService {
|
||||
private final Map<String, RegionService> servicesByCountryCode;
|
||||
|
||||
@Autowired
|
||||
public CustomMapFromListDynamicAutowireService(List<RegionService> regionServices) {
|
||||
servicesByCountryCode = regionServices.stream()
|
||||
.collect(Collectors.toMap(RegionService::getISOCountryCode, Function.identity()));
|
||||
}
|
||||
|
||||
public boolean isServerActive(String isoCountryCode, int serverId) {
|
||||
RegionService service = servicesByCountryCode.get(isoCountryCode);
|
||||
|
||||
return service.isServerActive(serverId);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.dynamic.autowire")
|
||||
public class DynamicAutowireConfig {
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("GBregionService")
|
||||
public class GBRegionService implements RegionService {
|
||||
@Override
|
||||
public boolean isServerActive(int serverId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getISOCountryCode() {
|
||||
return "GB";
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
public interface RegionService {
|
||||
boolean isServerActive(int serverId);
|
||||
|
||||
String getISOCountryCode();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("USregionService")
|
||||
public class USRegionService implements RegionService {
|
||||
@Override
|
||||
public boolean isServerActive(int serverId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getISOCountryCode() {
|
||||
return "US";
|
||||
}
|
||||
}
|
33
spring-core-4/src/test/java/com/baeldung/dynamic/autowire/DynamicAutowireIntegrationTest.java
Normal file
33
spring-core-4/src/test/java/com/baeldung/dynamic/autowire/DynamicAutowireIntegrationTest.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.baeldung.dynamic.autowire;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = DynamicAutowireConfig.class)
|
||||
public class DynamicAutowireIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private BeanFactoryDynamicAutowireService beanFactoryDynamicAutowireService;
|
||||
|
||||
@Autowired
|
||||
private CustomMapFromListDynamicAutowireService customMapFromListDynamicAutowireService;
|
||||
|
||||
@Test
|
||||
public void givenDynamicallyAutowiredBean_whenCheckingServerInGB_thenServerIsNotActive() {
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("GB", 101), is(false));
|
||||
assertThat(customMapFromListDynamicAutowireService.isServerActive("GB", 101), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDynamicallyAutowiredBean_whenCheckingServerInUS_thenServerIsActive() {
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("US", 101), is(true));
|
||||
assertThat(customMapFromListDynamicAutowireService.isServerActive("US", 101), is(true));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user