BAEL-3829: example of dynamic bean autowiring in Spring

This commit is contained in:
Maciej Glowka 2020-05-03 22:05:04 +02:00
parent 31462c6b49
commit ba42ca2dbb
7 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,22 @@
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 final BeanFactory beanFactory;
@Autowired
public BeanFactoryDynamicAutowireService(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public boolean isServerActive(String countryCode, int serverId) {
RegionService service = beanFactory.getBean(countryCode, RegionService.class);
return service.isServerActive(serverId);
}
}

View File

@ -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 {
}

View 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 InterfaceDynamicAutowireService {
private final Map<String, RegionService> servicesByCountryCode;
@Autowired
public InterfaceDynamicAutowireService(List<RegionService> regionServices) {
servicesByCountryCode = regionServices.stream()
.collect(Collectors.toMap(RegionService::getCountryCode, Function.identity()));
}
public boolean isServerActive(String countryCode, int serverId) {
RegionService service = servicesByCountryCode.get(countryCode);
return service.isServerActive(serverId);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.dynamic.autowire;
public interface RegionService {
boolean isServerActive(int serverId);
String getCountryCode();
}

View File

@ -0,0 +1,16 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("usa")
public class USARegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return true;
}
@Override
public String getCountryCode() {
return "usa";
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("uk")
public class UkRegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return false;
}
@Override
public String getCountryCode() {
return "uk";
}
}

View File

@ -0,0 +1,30 @@
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 InterfaceDynamicAutowireService interfaceDynamicAutowireService;
@Test
public void testConstructWorkerByJava() {
assertThat(beanFactoryDynamicAutowireService.isServerActive("uk", 101), is(false));
assertThat(interfaceDynamicAutowireService.isServerActive("uk", 101), is(false));
assertThat(beanFactoryDynamicAutowireService.isServerActive("usa", 101), is(true));
assertThat(interfaceDynamicAutowireService.isServerActive("usa", 101), is(true));
}
}