使用 RequiredArgsConstructor 注入 ArrayList
Inject ArrayList using RequiredArgsConstructor
我尝试通过 lombok RequiredArgsConstructor 将 List 注入到构造函数中
@Slf4j
@Component
@RequiredArgsConstructor (onConstructor = @_(@Inject))
public class ClassA {
@NonNull private List<GoodSkill> skills;
......
}
然而又出现错误:
[tomcat:launchProperties]
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'ClassA' defined in URL
[jar:file:/XXXXX/ClassA.class]: Unsatisfied dependency expressed
through constructor parameter 0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type java.util.List<\GoodSkill>: expected at least 1
bean which qualifies as autowire candidate. Dependency annotations: {}
好像没有"List<\GoodSkill>"的bean?我想在注入 List 或其他 Collections 时有一些特殊规则?至于 GoodSkill class,我想我也应该添加像 @Component?
这样的注解
========
编辑:
更奇怪的是,我重新构建了包,现在它不再抱怨找不到 List<\GoodSkill> 的 bean,而是找不到 GoodSkill 的 bean。我很困惑。
只能对托管 bean 进行注入。所以,对于列表,你应该有类似的东西:
@Component
public class GoodSkillList extends ArrayList<GoodSkill>{}
用于查找容器。
也许你有?如果你有很多这样的 beans container/Spring 可能无法决定使用哪个。在这种情况下,您可以通过多种方式界定可能的备选方案,例如,您可以将 ClassA
设置为仅接受:
@NonNull private GoodSkillList skills;
或者您可以按名称研究 howto inject/autowire。
我尝试通过 lombok RequiredArgsConstructor 将 List 注入到构造函数中
@Slf4j
@Component
@RequiredArgsConstructor (onConstructor = @_(@Inject))
public class ClassA {
@NonNull private List<GoodSkill> skills;
......
}
然而又出现错误:
[tomcat:launchProperties] org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ClassA' defined in URL [jar:file:/XXXXX/ClassA.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type java.util.List<\GoodSkill>: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
好像没有"List<\GoodSkill>"的bean?我想在注入 List 或其他 Collections 时有一些特殊规则?至于 GoodSkill class,我想我也应该添加像 @Component?
这样的注解========
编辑: 更奇怪的是,我重新构建了包,现在它不再抱怨找不到 List<\GoodSkill> 的 bean,而是找不到 GoodSkill 的 bean。我很困惑。
只能对托管 bean 进行注入。所以,对于列表,你应该有类似的东西:
@Component
public class GoodSkillList extends ArrayList<GoodSkill>{}
用于查找容器。
也许你有?如果你有很多这样的 beans container/Spring 可能无法决定使用哪个。在这种情况下,您可以通过多种方式界定可能的备选方案,例如,您可以将 ClassA
设置为仅接受:
@NonNull private GoodSkillList skills;
或者您可以按名称研究 howto inject/autowire。