在 spring 中使用自动装配注释的问题

problem with using autowired annotation in spring

在下面的代码中,我的问题是,虽然BaseBallCoach中的字段有@Autowired和@Qualifiere,但它仍然报错,说expected single matching bean but found 2.请问是什么问题?

更新:我添加了FortuneService(RandomFortuneService)的第二个实现

public interface Coach {
    String getDailyWorkout();
    String getDailyFortune();

}

棒球教练class:

@Component
public class BaseBallCoach implements Coach{

    @Autowired
    @Qualifier("happyFortuneService")
    private FortuneService fortuneService;


public BaseBallCoach(FortuneService fortuneService) {
    this.fortuneService = fortuneService;
}

@Override
public String getDailyWorkout(){
    return "Spend 30 minutes on batting practice";
}

@Override
public String getDailyFortune() {
    return fortuneService.getFortune();
}

}

财富服务:

    public interface FortuneService {
    String getFortune();
}

happyFortuneService:

@Component
public class HappyFortuneService implements FortuneService{
    @Override
    public String getFortune() {
        return "Today is your lucky day!";
    }
}

RadnomFortuneService :

    @Component
    public class RandomFortuneService implements FortuneService{
        @Override
        public String getFortune() {
            return "This is a random fortune service implementation";
        }
    }

输出:

"C:\Program Files\Java\jdk-17.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=2701:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\User\IdeaProjects\spring_demo1\out\production\spring_demo1;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-aop-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-aspects-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-beans-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-context-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-context-indexer-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-context-support-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-core-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-expression-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-instrument-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-jcl-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-jdbc-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-jms-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-messaging-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-orm-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-oxm-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-r2dbc-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-test-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-tx-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-web-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-webflux-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-webmvc-5.3.9.jar;C:\Users\User\Downloads\Compressed\spring-framework-5.3.9\libs\spring-websocket-5.3.9.jar org.isoft.Main
Apr 16, 2022 10:07:33 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baseBallCoach' defined in file [C:\Users\User\IdeaProjects\spring_demo1\out\production\spring_demo1\org\isoft\BaseBallCoach.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.isoft.FortuneService' available: expected single matching bean but found 2: happyFortuneService,randomFortuneService
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baseBallCoach' defined in file [C:\Users\User\IdeaProjects\spring_demo1\out\production\spring_demo1\org\isoft\BaseBallCoach.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.isoft.FortuneService' available: expected single matching bean but found 2: happyFortuneService,randomFortuneService
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=16=](AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at org.isoft.Main.main(Main.java:9)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.isoft.FortuneService' available: expected single matching bean but found 2: happyFortuneService,randomFortuneService
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:220)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1358)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
    ... 15 more

Process finished with exit code 1

删除 @Autowired 并在 BaseBallCoach class.

的构造函数参数上使用 @Qualifier("happyFortuneService")

在您的案例中,Spring 似乎优先考虑构造函数注入而不是字段注入,因此 @Qualifer 被有效地忽略了。这就是您收到此错误的原因。

构造函数注入是当今依赖注入的首选方法。查看此答案以了解原因: