package com.example.factory; import com.example.config.ConfigValue; import com.example.service.HisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; /** * @Company: 西安路泰科技有限公司 * @Author: zhaowenxuan * @Date: 2024/12/18 09:32 */ @Component public class ServiceFactory { private final ConfigValue configValue; private final ApplicationContext applicationContext; private final String userId; @Autowired public ServiceFactory(ApplicationContext applicationContext, ConfigValue configValue) { this.applicationContext = applicationContext; this.configValue = configValue; try { this.userId = configValue.getConfigValue("hosp_service"); } catch (Exception e) { throw new RuntimeException("配置文件中没有配置hosp_service医院编码"); } } public HisService getService() { String beanName = getServiceBeanName(userId); return (HisService) applicationContext.getBean(beanName); } private String getServiceBeanName(String userId) { switch (userId) { case "ShanXi_XiAn_MeiJi": return "ShanXiXiAnMeiJi"; default: throw new RuntimeException("找不到对应的医院编码服务层配置:" + userId); } } }