gpt4 book ai didi

java - Factory设计的实现,在Spring Boot中遇到这个错误

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

您好,我在 @DruidKuma 链接的一些指导下创建了一个工厂:Implement a simple factory pattern with Spring 3 annotations

我创建工厂如下:

@Component
public class ValidatorFactory {
@Autowired
private List<Validator> validators;

private static final Map<String, Validator> validatorMap = new ConcurrentHashMap<>();

@PostConstruct
public void setValidatorMap() {
for (Validator validator : validators) {
validatorMap.putIfAbsent(validator.getCountry().name(), validator);
}
}

public Validator getValidator(Value v) {
return validatorMap.get(v.name());
}
}

但是当我尝试启动应用程序时,我收到此错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field validators in com.abc.factory.ValidatorFactory required a bean of type 'java.util.List' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.util.List' in your configuration.

我从帮助我的链接中了解到,spring 应该自动创建 bean。有人可以帮我解决这个问题吗?

最佳答案

根据您的代码:

@Autowired
private List<Validator> validators;

Spring 尝试查找 Validator 类型的 bean,如果找不到任何 bean,则会抛出错误 required a bean of type 'java.util.List' that Could not be找到了。

要解决此问题,请确保类路径中有 Validator 类型的 bean,并且 Spring 容器正在扫描它们,例如使用 @ComponentScan

关于java - Factory设计的实现,在Spring Boot中遇到这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61843913/

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