@Component
public class CommonServiceFactory implements ApplicationContextAware {
private static Map<String, CommonService> commonServiceMap;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, CommonService> map = applicationContext.getBeansOfType(CommonService.class);
commonServiceMap = new HashMap<>();
map.forEach((key, value) -> commonServiceMap.put(value.getIdentify(), value));
}
@SuppressWarnings("unchecked")
public static <T extends CommonService> T getCommonService(String code) {
return (T) commonServiceMap.get(code);
}
}
public class CommonServiceFactory implements ApplicationContextAware {
private static Map<String, CommonService> commonServiceMap;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, CommonService> map = applicationContext.getBeansOfType(CommonService.class);
commonServiceMap = new HashMap<>();
map.forEach((key, value) -> commonServiceMap.put(value.getIdentify(), value));
}
@SuppressWarnings("unchecked")
public static <T extends CommonService> T getCommonService(String code) {
return (T) commonServiceMap.get(code);
}
}