package com.sptg.gateway.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WFT
 * @date 2020/6/11
 * description:
 */
@Component
public class CsvServiceFactory implements ApplicationContextAware {

    private static Map<String, CsvService> MAP;

    public CsvService getService(String flag){
        Assert.notNull(MAP.get(flag),"参数异常");
        return MAP.get(flag);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        Map<String, CsvService> beansMap = context.getBeansOfType(CsvService.class);
        MAP = new HashMap<>(beansMap.size());
        beansMap.forEach((beanName,bean) -> MAP.put(bean.getFlag(),bean));
    }
}
