今天遇到场景,需要把某个类型的bean组成一个Map注入到另一个bean中。恰好spring的@Resource支持。
不过如果要注入一个普通的Map,在不使用@bean的情况下,还没有找到解决方法。
Java代码
- @Resource
- private Map<String,Service> serviceMap;
这样把所有Service的类型(实现类)的bean全部注入到servcieMap中。
serviceMap的key为bean的名字,比如下面的aService,value为ServiceA对应的bean.
Java代码
- @Service("aService")
- public class ServiceA implements Service{
- ......
- }
下面的例子,为一个工厂模式的自动注入
eg:
工厂类
@Service
public class ActionHandlerFactory {
@Resource
private Map<String, ActionHandler> actionHandlerMap;
public ActionHandler getActionHandler(Integer actionId) {
ActionTypeEnum actionTypeEnum = ActionTypeEnum.getByActionId(actionId);
return actionHandlerMap.get(actionTypeEnum.getServiceName());
}
}
其中,ActionHandler 是一个接口类
public interface ActionHandler<T extends Serializable> {
void process(EventContext<T> eventContext) throws CscPacificProxyException;
ActionTypeEnum getActionTypeEnum();
}
ActionHandler 是一个接口类 可以有很多的抽象类,如下,
public abstract class AbstractActionHandler implements ActionHandler {
@Override
public abstract ActionTypeEnum getActionTypeEnum();
@Autowired
protected CscEventActionInvokeLogService cscEventActionInvokeLogService;
@Autowired
protected CscEventActionParamMappingService eventActionParamMappingService;
@Reference(timeout = 3000)
protected CaseManagerRemoteService caseManagerRemoteService;
@Reference(timeout = 3000)
protected CaseActionRemoteFacade caseActionRemoteFacade;
@Reference
protected CaseTypeRemoteService caseTypeRemoteService;
@Reference
protected CaseActionProxyRemoteFacade caseActionProxyRemoteFacade;
public void configInvokeLogBasic(EventContext eventContext, CscEventActionInvokeLog cscEventActionInvokeLog) {
cscEventActionInvokeLog.setEventInvokeId(Long.valueOf(eventContext.getEventParamAttribute(EventParamKeyEnum.eventInvokeLogId.name())));
cscEventActionInvokeLog.setEventId(Integer.valueOf(eventContext.getEventParamAttribute(EventParamKeyEnum.eventId.name())));
cscEventActionInvokeLog.setEventCode(Integer.valueOf(eventContext.getEventParamAttribute(EventParamKeyEnum.eventCode.name())));
cscEventActionInvokeLog.setEventType(Byte.valueOf(eventContext.getEventParamAttribute(EventParamKeyEnum.eventType.name())));
cscEventActionInvokeLog.setActionId(getActionTypeEnum().getActionId());
cscEventActionInvokeLog.setAppkeyId(Integer.valueOf(eventContext.getEventParamAttribute(EventParamKeyEnum.appkeyId.name())));
cscEventActionInvokeLog.setAppkeyName(eventContext.getEventParamAttribute(EventParamKeyEnum.appkeyName.name()));
cscEventActionInvokeLog.setCreatorId(0L);
cscEventActionInvokeLog.setAddTime(new Date());
}
public void configInvokeLogResult(CscEventActionInvokeLog cscEventActionInvokeLog, ActionInvokeStatusEnum actionInvokeStatusEnum, String request, String responseJson) {
cscEventActionInvokeLog.setResponseResultStatus(actionInvokeStatusEnum.getId());
cscEventActionInvokeLog.setResponseJson(responseJson);
cscEventActionInvokeLog.setRequestJson(request);
}
public String buildCaseMainJson(CaseMainDTO caseMainDTO, String operationNameStr) {
Map<String, Object> caseMap = CaseModelUtil.convertCaseObj2Map(caseMainDTO, CaseDataModelTypeEnum.CASE.code);
String jsonStr = "{\"caseData\":" + JacksonUtil.toJsonStrWithEmptyDefault(caseMap) + ",\"params\":{\"operationName\":\"" + operationNameStr + "\"}}";
return jsonStr;
}
protected ServiceProxyParam initServiceProxyParam() {
ServiceProxyParam serviceProxyParam = new ServiceProxyParam();
List<String> commentList = Lists.newArrayList();
Map<String, String> extendsFieldMap = Maps.newHashMap();
serviceProxyParam.setComments(commentList);
serviceProxyParam.setExtendFields(extendsFieldMap);
return serviceProxyParam;
}
}
AbstractActionHandler 类下面可以有很多的实现: