上一篇的"JAVA 替代SWITCH 枚举值 CASE 的 策略模式 " 中使用new关键字实例化一个类时,这个类的实例不由Spring容器管理,因此@Autowired注解在这个实例中不会生效。Spring的依赖注入功能仅对其管理的bean有效,即那些通过Spring容器创建和管理的bean。
下面是spring托管其实现类方法:
java
@Service("checkAreaListItemService")
public class CheckAreaListItemServiceImpl extends ApiFrag implements CheckListItemService {
@Override
public void check(Long tenantId,List<SysLimitItemGroupmx> mxList) throws Exception{
String typeName= LimitItemTypeEnum.AREA.getInfo();
Terme term=new Terme();
term.setTenantId(tenantId);
term.setQydmlist(listField2StrList(mxList,"dxdm"));
List<Quyu> list = getQuyuList$api(term);
Map<String, String> map = new HashMap();;
for (Quyu quyu : list) {
map.put(quyu.getQydm(), quyu.getQymc());
}
for (SysLimitItemGroupmx itemGroupmx : mxList) {
String key=itemGroupmx.getDxdm();
boolean flag = map.containsKey(key);
if(flag) itemGroupmx.setDxmc(map.get(key));
else itemGroupmx.setBz(typeName + "编码对应的数据不存在!");
itemGroupmx.setFlag(flag);
}
}
@Service("checkBrandListItemService")
public class CheckBrandListItemServiceImpl extends ApiFrag implements CheckListItemService {
@Override
public void check(Long tenantId,List<SysLimitItemGroupmx> mxList) throws Exception{
String typeName= LimitItemTypeEnum.BRAND.getInfo();
Terme term=new Terme();
term.setTenantId(tenantId);
term.setPpdmlist(listField2StrList(mxList,"dxdm"));
List<Pinpai> list = getPinpaiList$api(term);
Map<String, String> map = new HashMap();;
for (Pinpai item : list) {
map.put(item.getPpdm(), item.getPpmc());
}
for (SysLimitItemGroupmx itemGroupmx : mxList) {
String key=itemGroupmx.getDxdm();
boolean flag = map.containsKey(key);
if(flag) itemGroupmx.setDxmc(map.get(key));
else itemGroupmx.setBz(typeName + "编码对应的数据不存在!");
itemGroupmx.setFlag(flag);
}
}
}
上下文类:
java
@Slf4j
@Component
public class CheckLimitItemProcessor {
private final Map<Integer, String> strategyMap;
public CheckLimitItemProcessor() {
strategyMap = new HashMap<>();
strategyMap.put(LimitItemTypeEnum.AREA.getNumber(), "checkAreaListItemService");
strategyMap.put(LimitItemTypeEnum.BRAND.getNumber(),"CheckBrandListItemService");
strategyMap.put(LimitItemTypeEnum.CUSTOMER.getNumber(), "CheckCustomerListItemService");
strategyMap.put(LimitItemTypeEnum.SHOP.getNumber(), "checkShopListItemService");
strategyMap.put(LimitItemTypeEnum.STORE.getNumber(), "checkStoreListItemService");
strategyMap.put(LimitItemTypeEnum.SUPPLIER.getNumber(), "checkSupplierListItemService");
}
public void checkItem( ApplicationContext context,Integer type,Long tenantId, List<SysLimitItemGroupmx> mxList)
throws Exception{
log.info("context:{}",context);
CheckListItemService strategy = context.getBean(strategyMap.get(type), CheckListItemService.class);
if (strategy == null) {
throw new IllegalArgumentException("Unsupported LimitItem type.");
}
strategy.check(tenantId,mxList);
}
}
使用类:
java
@Autowired private ApplicationContext context;
private void checkData(Integer type, List<SysLimitItemGroupmx> resList) throws Exception {
Long tenantId=getPresentTenantId();
CheckLimitItemProcessor processor = new CheckLimitItemProcessor();
processor.checkItem(context,type,tenantId,resList);
}