带有字符串列表的 NoSuchBeanDefinitionException - pom.xml
NoSuchBeanDefinitionException with list of string - pom.xml
我对 NoSuchBeanDefinitionException 有疑问。
@Component
public class Monitor {
private List<String> urlsToCheckState;
@Autowired
ServerConfig config;
public Monitor(config config, List<String> urlsToCheckState){
urlsToCheckState.add("state1");
}
我了解到 Spring 无法在 return String 列表中找到 Bean。我一直在寻找解决方案,但找不到任何解决我问题的方法。
这是我的错误。
Error creating bean with name 'Monitor' defined in file [C:\Users]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
因为你还没有在urlsToCheckState 上使用@Autowired。您必须使用构造函数注入来注入它。
在您的组件中创建一个构造函数,如下所示:-
Monitor(List<String> urlsToCheckState){
this.urlsToCheckState = urlsToCheckState;
}
这样你就可以使用构造函数进行注入了。
要在构建时注入依赖项,您需要使用 @Autowired 注释标记构造函数,如下所示。
@Autowired
Monitor(List<String> urlsToCheckState){
this.urlsToCheckState = urlsToCheckState;
}
当然,在注入之前,您必须创建 bean urlsToCheckState 实例。
我对 NoSuchBeanDefinitionException 有疑问。
@Component
public class Monitor {
private List<String> urlsToCheckState;
@Autowired
ServerConfig config;
public Monitor(config config, List<String> urlsToCheckState){
urlsToCheckState.add("state1");
}
我了解到 Spring 无法在 return String 列表中找到 Bean。我一直在寻找解决方案,但找不到任何解决我问题的方法。 这是我的错误。
Error creating bean with name 'Monitor' defined in file [C:\Users]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
因为你还没有在urlsToCheckState 上使用@Autowired。您必须使用构造函数注入来注入它。
在您的组件中创建一个构造函数,如下所示:-
Monitor(List<String> urlsToCheckState){
this.urlsToCheckState = urlsToCheckState;
}
这样你就可以使用构造函数进行注入了。
要在构建时注入依赖项,您需要使用 @Autowired 注释标记构造函数,如下所示。
@Autowired
Monitor(List<String> urlsToCheckState){
this.urlsToCheckState = urlsToCheckState;
}
当然,在注入之前,您必须创建 bean urlsToCheckState 实例。