public interface TestService {
void doTest();
String getTestServiceType();
}
@Service
public class Test1ServiceImpl implements TestService{
@Override
public void doTest() {
System.out.println("111111111111");
}
@Override
public String getTestServiceType() {
return "01";
}
}
@Service
public class Test2ServiceImpl implements TestService{
@Override
public void doTest() {
System.out.println("222222");
}
@Override
public String getTestServiceType() {
return "02";
}
}
@Component
public class TestServceLocator implements ApplicationContextAware {
public Map<String, TestService> map = new HashMap<String, TestService>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, TestService> mapHandle = new HashMap<String, TestService>();
Map<String, TestService> retMap = applicationContext.getBeansOfType(TestService.class);
for (Map.Entry<String, TestService> map : retMap.entrySet()) {
mapHandle.put(map.getValue().getTestServiceType(), map.getValue());
}
map = mapHandle;
}
public Map<String, TestService> getMap() {
return map;
}
public TestService getService(String key) {
return map.get(key);
}
}
@RestController
@RequestMapping("/system")
@Api("测试API")
public class TestSystemApiController {
@Autowired
private TestServceLocator testServceLocator;
@RequestMapping(value = "/hello3" ,method = RequestMethod.POST)
public APIResponse<String> hello3(String name) {
Map<String, TestService> retMap = testServceLocator.getMap();
retMap.get(name).doTest();
return APIResponse.success("say hello to" + name);
}
}