gpt4 book ai didi

java - 启动SpringBoot应用时出现No Qualifying Bean(NoSuchBeanDefinitionException)Available错误

转载 作者:行者123 更新时间:2023-12-02 01:52:05 25 4
gpt4 key购买 nike

我正在向现有 SpringBoot 应用程序添加一些新组件,但在启动应用程序时遇到 bean 定义异常。

所有bean/服务和其他组件都是通过注释来配置的,而不是通过spring xml配置(我更熟悉基于xml的spring配置)。不幸的是,为了解释我的问题,我必须在下面稍微混淆一下,而不是提供真实的代码。

在应用程序中,我添加了一个新的工厂组件,将其命名为 FooSheetFactory:

package some.package;  

@Component
public class FooSheetFactory {

private final List<FooSheet> fooSheetList;

@autowired
public FooSheetFactory(List<FooSheet> fooSheetList) {
this. fooSheetList = fooSheetList;
}

.
.
(other stuff)
.
}

此类使用名为 FooSheet 的组件:

package some.package;

@Component
public interface FooSheet {

public Foo getFoo(int param1, String param2);
}

工厂在应用程序的其他地方实例化,方式如下:

.
.
@Autowire
FooSheetFactory fsf;
.
.

启动SpringBoot应用程序时,出现以下错误:

Error creating bean with name "FooSheetFactory " defined 
in file [ ~path/target/classes/..../FooSheetFactory.class]: Unsatisfied dependency
expressed through constructor parameter 0; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type 'java.util.List<some.package.FooSheet>' available. Expected at least 1 bean which
qualifies as autowire candidate. Dependency annotations: {}

从表面上看,这个实例化对我来说类似于我们在应用程序的其他地方使用 spring 的方式。类和接口(interface)都在同一个包中,并且该包中使用类似注释的其他类在启动时不会抛出错误。如果您能了解我的问题可能是什么,我将不胜感激。谢谢。

最佳答案

不要将@Component注解放在接口(interface)上,而是放在实现类上。您的异常是提示它找不到您的接口(interface)的任何实现。你应该有:

@Component
class FooSheetImpl implements FooSheet {
...
}

关于java - 启动SpringBoot应用时出现No Qualifying Bean(NoSuchBeanDefinitionException)Available错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com