春季使用自动注释的问题

发布于 2025-01-21 18:25:44 字数 7788 浏览 0 评论 0原文

在以下代码中,我的问题是,尽管棒球棒上的字段具有@Autowired和@qualifiere,但它仍然给出一个错误,并说了预期的单个匹配的bean,但发现了2。有什么问题?

更新:我添加了FortuneService(RandomfortuneService)棒球棒类的第二次实现

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

}

@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:

    public interface FortuneService {
    String getFortune();
}

HappyFortunService:

@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$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

In the following code, my problem is that although the field in the BaseBallCoach has @Autowired and @Qualifiere, it still gives an error and says expected single matching bean but found 2. What is the problem?

Update: I added the second implementation of FortuneService (RandomFortuneService)

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

}

baseballCoach 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();
}

}

fortuneService:

    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";
        }
    }

output:

"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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2025-01-28 18:25:44

删除@Autowired,然后使用@qualifier(“ HappyFortuneservice”)base> baseballcooch class的构造方参数上。

在您的情况下,Spring似乎优先考虑构造函数注入而不是现场注入,因此有效地忽略了@qualifer。这就是为什么您会遇到此错误。

当今,构造函数注入是依赖注入的首选方法。查看此答案以了解原因:
spring @autowire on Properties vs constructor

Remove the @Autowired and use the @Qualifier("happyFortuneService") on the constructor parameter of the BaseBallCoach class.

It appears Spring is prioritizing constructor injection over field injection in your case, so the @Qualifer is effectively ignored. That's why you are getting this error.

Constructor injection is the preferred method for dependency injection nowadays. Check out this answer to learn why:
Spring @Autowire on Properties vs Constructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文