@Resource 注入map

今天遇到场景,需要把某个类型的bean组成一个Map注入到另一个bean中。恰好spring的@Resource支持。 

不过如果要注入一个普通的Map,在不使用@bean的情况下,还没有找到解决方法。 
 

Java代码  收藏代码

  1. @Resource  
  2. private Map<String,Service> serviceMap;  



这样把所有Service的类型(实现类)的bean全部注入到servcieMap中。 
serviceMap的key为bean的名字,比如下面的aService,value为ServiceA对应的bean. 
 

Java代码  收藏代码

  1. @Service("aService")  
  2. public class ServiceA implements Service{  
  3.     ......  
  4. }  

 

下面的例子,为一个工厂模式的自动注入

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 类下面可以有很多的实现:

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值