BAEL-3829: added more distinct bean names, changed country codes to ISO codes
This commit is contained in:
parent
ba42ca2dbb
commit
13f2b10c8b
|
@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
@Service
|
||||
public class BeanFactoryDynamicAutowireService {
|
||||
private static final String SERVICE_NAME_SUFFIX = "regionService";
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
|
@ -13,10 +14,14 @@ public class BeanFactoryDynamicAutowireService {
|
|||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public boolean isServerActive(String countryCode, int serverId) {
|
||||
RegionService service = beanFactory.getBean(countryCode, RegionService.class);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,17 +9,17 @@ import java.util.function.Function;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class InterfaceDynamicAutowireService {
|
||||
public class CustomMapFromListDynamicAutowireService {
|
||||
private final Map<String, RegionService> servicesByCountryCode;
|
||||
|
||||
@Autowired
|
||||
public InterfaceDynamicAutowireService(List<RegionService> regionServices) {
|
||||
public CustomMapFromListDynamicAutowireService(List<RegionService> regionServices) {
|
||||
servicesByCountryCode = regionServices.stream()
|
||||
.collect(Collectors.toMap(RegionService::getCountryCode, Function.identity()));
|
||||
.collect(Collectors.toMap(RegionService::getISOCountryCode, Function.identity()));
|
||||
}
|
||||
|
||||
public boolean isServerActive(String countryCode, int serverId) {
|
||||
RegionService service = servicesByCountryCode.get(countryCode);
|
||||
public boolean isServerActive(String isoCountryCode, int serverId) {
|
||||
RegionService service = servicesByCountryCode.get(isoCountryCode);
|
||||
|
||||
return service.isServerActive(serverId);
|
||||
}
|
|
@ -2,15 +2,15 @@ package com.baeldung.dynamic.autowire;
|
|||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("uk")
|
||||
public class UkRegionService implements RegionService {
|
||||
@Service("GBregionService")
|
||||
public class GBRegionService implements RegionService {
|
||||
@Override
|
||||
public boolean isServerActive(int serverId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCountryCode() {
|
||||
return "uk";
|
||||
public String getISOCountryCode() {
|
||||
return "GB";
|
||||
}
|
||||
}
|
|
@ -3,5 +3,5 @@ package com.baeldung.dynamic.autowire;
|
|||
public interface RegionService {
|
||||
boolean isServerActive(int serverId);
|
||||
|
||||
String getCountryCode();
|
||||
String getISOCountryCode();
|
||||
}
|
||||
|
|
|
@ -2,15 +2,15 @@ package com.baeldung.dynamic.autowire;
|
|||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("usa")
|
||||
public class USARegionService implements RegionService {
|
||||
@Service("USregionService")
|
||||
public class USRegionService implements RegionService {
|
||||
@Override
|
||||
public boolean isServerActive(int serverId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCountryCode() {
|
||||
return "usa";
|
||||
public String getISOCountryCode() {
|
||||
return "US";
|
||||
}
|
||||
}
|
|
@ -17,14 +17,14 @@ public class DynamicAutowireIntegrationTest {
|
|||
private BeanFactoryDynamicAutowireService beanFactoryDynamicAutowireService;
|
||||
|
||||
@Autowired
|
||||
private InterfaceDynamicAutowireService interfaceDynamicAutowireService;
|
||||
private CustomMapFromListDynamicAutowireService customMapFromListDynamicAutowireService;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("uk", 101), is(false));
|
||||
assertThat(interfaceDynamicAutowireService.isServerActive("uk", 101), is(false));
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("GB", 101), is(false));
|
||||
assertThat(customMapFromListDynamicAutowireService.isServerActive("GB", 101), is(false));
|
||||
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("usa", 101), is(true));
|
||||
assertThat(interfaceDynamicAutowireService.isServerActive("usa", 101), is(true));
|
||||
assertThat(beanFactoryDynamicAutowireService.isServerActive("US", 101), is(true));
|
||||
assertThat(customMapFromListDynamicAutowireService.isServerActive("US", 101), is(true));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue