我有一个组件-子组件关系。每个都有不同的作用域,并使用自己的模块,该模块提供相同类型的缺陷。我需要的是基于范围的不同对象实例化。Dagger不会允许这样做,因为我将有“多个绑定”。如何在不@Named资格赛的情况下解决问题?例如,有没有办法覆盖子组件?
//Higher scoped object (in component)
@Provides
@ClientScope
ISupResRankStrategy iSupResRankStrategy(@Named("GlobalModelConfig") JsonNode configSubTree,
Lazy<SortByMagnitudeSum> strat1,
Lazy<SortByShadowPercentage> strat2) {
@SuppressWarnings("rawtypes")
Map<String, Lazy> availableStrategies = new HashMap<>();
availableStrategies.put(SortByMagnitudeSum.class.getSimpleName(), strat1);
availableStrategies.put(SortByShadowPercentage.class.getSimpleName(), strat2);
String configuredStrategy = configSubTree.findValue("ISupResRankStrategy").asText();
return (ISupResRankStrategy) availableStrategies.get(configuredStrategy).get();
}
//lower scoped object (in subcomponent)
@Provides
@ModelScope
ISupResRankStrategy iSupResRankStrategy(@Named("TradeModelConfig") JsonNode configSubTree,
Lazy<SortByMagnitudeSum> strat1,
Lazy<SortByShadowPercentage> strat2) {
@SuppressWarnings("rawtypes")
Map<String, Lazy> availableStrategies = new HashMap<>();
availableStrategies.put(SortByMagnitudeSum.class.getSimpleName(), strat1);
availableStrategies.put(SortByShadowPercentage.class.getSimpleName(), strat2);
String configuredStrategy = configSubTree.findValue("ISupResRankStrategy").asText();
return (ISupResRankStrategy) availableStrategies.get(configuredStrategy).get();
}
慕哥6287543
明月笑刀无情
相关分类