Merge pull request #9227 from maciejglowka/BAEL-3829

BAEL-3829: example of dynamic bean autowiring in Spring
This commit is contained in:
rpvilao 2020-05-09 11:10:54 +02:00 committed by GitHub
commit 3d0645f9d8
7 changed files with 134 additions and 0 deletions

View 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;
}
}

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 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);
}
}

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,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";
}
}

View File

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

View File

@ -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";
}
}

View 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));
}
}