Spring策略模式抽象化
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class BackendJobApplyHandlerContext implements ApplicationContextAware {
private final Map<TradeOperationTypeEnum, BaseTradeService> tradeServiceMap = new ConcurrentHashMap<>();
@Autowired
private TradeFlowContext tradeFlowContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, BaseBackendJobApplyService> beansOfType = applicationContext.getBeansOfType(BaseBackendJobApplyService.class);
beansOfType.values().forEach(baseBackendJobApplyService -> tradeServiceMap.put(baseBackendJobApplyService.getTradeOperationType(), baseBackendJobApplyService));
}
public void doTrade(BackendJobApply backendJobApply) {
BaseTradeService tradeService = tradeServiceMap.get(backendJobApply.getBackendJobOperationType());
TradeProcessPayload tradeProcessPayload = new TradeProcessPayload();
String backendJobApplyCode = backendJobApply.getBackendJobApplyCode();
tradeProcessPayload.setJoinOrderCode(backendJobApplyCode);
tradeFlowContext.doTrade(tradeProcessPayload, tradeService);
}
}