当前位置:网站首页>Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:

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

2022-06-11 21:03:00 qq_ twenty-one million four hundred and eighty thousand three h

Because of the actual needs of the project , That is, multiple classes implement the same interface , Different strategies take different Service Implementation class .

Interface class code :

public interface IQueryIndexFieldnameService {

    Object queryIndexByFieldname(IndexDictionaryCoreDTO dto);

}

Because in the project , Because of 10 Multiple classes implement the interface at the same time , In the use of spring Automatic injection , The project fails to start because there are multiple instances reporting an error .
Error report detailed log :

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

Solution :
1. Do not inject directly IQueryIndexFieldnameService, That is, do not use the following injection code

    @Autowired
    private IQueryIndexFieldnameService queryIndexFieldnameService;

2. Use the following injection code

    @Autowired
    private ApplicationContext applicationContext;

	 perhaps 
    @Autowired
    private BeanFactory beanFactory;

3. obtain Bean The instance

		//  check bean Whether there is 
        if (!applicationContext.containsBean(dto.getTypeCode())) {
            return null;
        }
        
        Object object = ((IQueryIndexFieldnameService)applicationContext.getBean(dto.getTypeCode())).queryIndexByFieldname(dto);
        if (object == null) {
            return null;
        }

Relevant big data learning demo Address :
https://github.com/carteryh/big-data

原网站

版权声明
本文为[qq_ twenty-one million four hundred and eighty thousand three h]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112055443262.html