路泰机电科技体检——数据平台后端
zhaowenxuan
2024-12-19 0c6ebd4a8ce66f3862ce9fba90dbbb38e59f8bc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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_Qin_XiAn_MeiJi":
                // 业务Bean的name
                return "ShanXiQinXiAnMeiJi";
            default:
                throw new RuntimeException("找不到对应的医院编码服务层配置:" + userId);
        }
    }
}