![](/img/trans.png)
[英]@Autowired : No qualifying bean of type available SpringBoot application
[英]No Qualifying Bean (NoSuchBeanDefinitionException) Available Error When Starting a SpringBoot Application
我正在向现有的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的方式相似。 该类和接口都在同一个程序包中,并且该程序包中使用类似批注的其他类在启动时不会引发错误。 我很高兴对我的问题有任何见解。 谢谢。
不要在接口上放置@Component
批注,而@Component
其放在实现类上。 您的例外是抱怨它找不到您的界面的任何实现。 你应该有:
@Component
class FooSheetImpl implements FooSheet {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.