1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
public class ReportAlarmServiceFactory {

    private final Map<String, ReportAlarmExecuteService> serviceMap;

    @Autowired
    public ReportAlarmServiceFactory(List<ReportAlarmExecuteService> services) {
        serviceMap = services.stream().collect(Collectors.toMap(
                service -> service.getClass().getSimpleName().replace("AlarmExecuteServiceImpl", "").toLowerCase(),
                Function.identity()
        ));
    }

    public ReportAlarmExecuteService getService(String type) {
        ReportAlarmExecuteService service = serviceMap.get(type.toLowerCase());
        if (service == null) {
            throw new IllegalArgumentException("获取服务异常: " + type);
        }
        return service;
    }
}
Licensed under CC BY-NC-SA 4.0
W' Docs/Blog
Built with Hugo
主题 StackJimmy 设计