Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:

因为项目实际需求,即多个类实现同一个接口,不同策略走不同的Service实现类。

接口类代码:

public interface IQueryIndexFieldnameService {

    Object queryIndexByFieldname(IndexDictionaryCoreDTO dto);

}

由于项目中,由于有10多个类同时实现该接口,在使用spring自动注入时,项目报错有多个实例而不能启动。
报错详细日志:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-06-04 22:04:05.001 ERROR 30773 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field queryIndexFieldnameService in com.otitan.forest.inspector.dm.service.impl.QueryIndexServiceImpl required a single bean, but 19 were found:
	- featureForestryIndustryOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/FeatureForestryIndustryOverviewServiceImpl.class]
	- forestGrassWetlandResource: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/ForestGrassWetlandResourceServiceImpl.class]
	- forestLandClassifiedArea: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/ForestLandClassifiedAreaServiceImpl.class]
	- forestryGrasslandPestControlOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/ForestryGrasslandPestControlOverviewServiceImpl.class]
	- forestryIndustryOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/ForestryIndustryOverviewServiceImpl.class]
	- landGreeningForestCultivationOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/LandGreeningForestCultivationOverviewServiceImpl.class]
	- nationForestFarmSeedlingDevelopmentOverviewForest: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NationForestFarmSeedlingDevelopmentOverviewForestServiceImpl.class]
	- nationForestFarmSeedlingDevelopmentOverviewSeedling: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NationForestFarmSeedlingDevelopmentOverviewSeedlingServiceImpl.class]
	- nationForestFarmSeedlingDevelopmentOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NationForestFarmSeedlingDevelopmentOverviewServiceImpl.class]
	- nationReserveForestConstructionOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NationReserveForestConstructionOverviewServiceImpl.class]
	- naturalForestProtectionForesterOverviewForest: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NaturalForestProtectionForesterOverviewForestServiceImpl.class]
	- naturalForestProtectionForesterOverviewPerson: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NaturalForestProtectionForesterOverviewPersonServiceImpl.class]
	- naturalForestProtectionForesterOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NaturalForestProtectionForesterOverviewServiceImpl.class]
	- natureReserveOverviewDistribution: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NatureReserveOverviewDistributionServiceImpl.class]
	- natureReserveOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/NatureReserveOverviewServiceImpl.class]
	- publicWelfareForestConstructionOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/PublicWelfareForestConstructionOverviewServiceImpl.class]
	- worldNaturalHeritageWildlifeResourceOverviewPlace: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/WorldNaturalHeritageWildlifeResourceOverviewPlaceServiceImpl.class]
	- worldNaturalHeritageWildlifeResourceOverview: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/WorldNaturalHeritageWildlifeResourceOverviewServiceImpl.class]
	- worldNaturalHeritageWildlifeResourceOverviewSpecies: defined in file [/Users/carter/Desktop/firm/GzForestInspection/dynamic-monitor/target/classes/com/otitan/forest/inspector/dm/service/impl/WorldNaturalHeritageWildlifeResourceOverviewSpeciesServiceImpl.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


Process finished with exit code 1

解决方案:
1.不要直接注入IQueryIndexFieldnameService,即不要使用如下注入代码

    @Autowired
    private IQueryIndexFieldnameService queryIndexFieldnameService;

2.使用如下注入代码

    @Autowired
    private ApplicationContext applicationContext;

	或者
    @Autowired
    private BeanFactory beanFactory;

3.获取Bean实例调用

		// 校验bean是否存在
        if (!applicationContext.containsBean(dto.getTypeCode())) {
            return null;
        }
        
        Object object = ((IQueryIndexFieldnameService)applicationContext.getBean(dto.getTypeCode())).queryIndexByFieldname(dto);
        if (object == null) {
            return null;
        }

相关大数据学习demo地址:
https://github.com/carteryh/big-data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值