spring注入多个实现类
写一个接口
public interface AccRuleFieldConvertProcessor {
Object execute();
}
写三个实现类
一、
@Service(AccRuleField.WAY_CONST)
public class AccRuleFieldConvertConstImpl implements AccRuleFieldConvertProcessor {
}
二、
@Service(AccRuleField.WAY_COTRAST)
public class AccRuleFieldConvertConstImpl implements AccRuleFieldConvertProcessor {
}
三、
@Service(AccRuleField.WAY_FUNC)
public class AccRuleFieldConvertConstImpl implements AccRuleFieldConvertProcessor {
}
spring使用多个实现类
@Service
public class AccRuleFieldServiceImpl implements AccRuleFieldService {
@Autowired(required = false)
private Map<String, AccRuleFieldConvertProcessor> processorMap;
@Override
public Object convertFieldByWay(AccRuleFieldConvertDTO dto, AccRuleField accRuleField) {
//accRuleField.getWay()的值为: 1.AccRuleField.WAY_CONST 2.AccRuleField.WAY_COTRAST 3.AccRuleField.WAY_FUNC
return processorMap.get(accRuleField.getWay()).execute(dto);
}
}
标签:Service,多个,spring,class,WAY,AccRuleField,public,注入
From: https://www.cnblogs.com/WangJingjun/p/16877370.html