1.接口
public interface ZeroTestService {
/**
* 校验
*
* @param map
*/
void checkData(Map<String, String> map);
/**
* 当前实现对应类型
*
* @return
*/
String getType();
}
2.实现类
@Service
public class ZeroTest01ServiceImpl implements ZeroTestService {
@Override
public void checkData(Map<String, String> map) {
}
@Override
public String getType() {
return null;
}
}
@Service
public class ZeroTest02ServiceImpl implements ZeroTestService {
@Override
public void checkData(Map<String, String> map) {
}
@Override
public String getType() {
return null;
}
}
3.实现IOC容器
@Component
public class ZeroTestServiceFactory implements InitializingBean {
@Autowired
private List<ZeroTestService> zeroTestServiceList;
public Map<String, ZeroTestService> serviceMap = new HashMap<>();
public ZeroTestService getZeroTestService(String type) {
return serviceMap.get(type);
}
@Override
public void afterPropertiesSet() throws Exception {
zeroTestServiceList.forEach(zeroTestService -> serviceMap.put(zeroTestService.getType(), zeroTestService));
}
}