首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >春季自动标注使用中的问题

春季自动标注使用中的问题
EN

Stack Overflow用户
提问于 2022-04-16 17:52:02
回答 1查看 330关注 0票数 0

在下面的代码中,我的问题是,尽管BaseBallCoach中的字段具有@Autowired和@Qualifiere,但它仍然给出了一个错误,并指出了预期的单个匹配bean,但是找到了2。问题是什么?

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

代码语言:javascript
复制
public interface Coach {
    String getDailyWorkout();
    String getDailyFortune();

}

baseballCoach类:

代码语言:javascript
复制
@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();
}

}

fortuneService:

代码语言:javascript
复制
    public interface FortuneService {
    String getFortune();
}

happyFortuneService:

代码语言:javascript
复制
@Component
public class HappyFortuneService implements FortuneService{
    @Override
    public String getFortune() {
        return "Today is your lucky day!";
    }
}

RadnomFortuneService:

代码语言:javascript
复制
    @Component
    public class RandomFortuneService implements FortuneService{
        @Override
        public String getFortune() {
            return "This is a random fortune service implementation";
        }
    }

产出:

代码语言:javascript
复制
"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$0(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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-16 18:14:56

删除@Autowired并对BaseBallCoach类的构造函数参数使用@Qualifier("happyFortuneService")

在您的情况下,Spring似乎将构造函数注入优先于字段注入,因此@Qualifer实际上被忽略了。这就是为什么你要得到这个错误。

构造函数注入是当前依赖注入的首选方法。请查看以下答案以了解原因:Spring @Autowire on Properties vs Constructor

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71896206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档