因为项目实际需求,即多个类实现同一个接口,不同策略走不同的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