简体   繁体   中英

DI: No qualifying bean of type available

I start to learn Spring. In my beginner project I want to inject dependency of beans. https://github.com/anatoliy19/2.1.4.git

    To do that, 1) I add in AppConfig method   
    @Bean
    public static Duck5 getDuck(Egg6 egg) {return new Duck5(egg);}

    and 2)
    add a private field in class Duck5: 
     
     @Autowired   
     private Egg6 egg;
    
    
    To my regret, an error has come out:
    [No qualifying bean of type 'koschei.models.Duck5' available][1]: expected single matching bean but found 2: duck5,getDuck
    
    Project is here: https://github.com/anatoliy19/2.1.4.git

So from the exception found 2 I assume you have the bean defined and taking from the Autowired field egg Duck5 is annotated with something like Component .

So the Component annotation already tells spring that it should create a bean for this class and then you create another one in your configuration and thereby Spring doesn't know which bean to take

Notice the bean names in the exception getDuck (your own bean) and duck5 (just the class name in lower case)

So now you have the options to either just remove the bean you create, remove the annotation and use your bean or use a Qualifer to identify the bean so Spring knows which to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM