我正在向现有 SpringBoot 应用程序添加一些新组件,并且在启动应用程序时遇到 bean 定义异常。
所有的 bean/service 和其他组件都是通过注解而不是通过 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
class FooSheetImpl implements FooSheet {
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句