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