【发布时间】:2017-10-08 21:33:45
【问题描述】:
我知道,在 SO 中有很多类似的问题,但我无法使用它们摆脱这种情况。
我有一个 Spring Boot 应用程序。
@SpringBootApplication
@EnableConfigurationProperties
public class Application implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
那我有下面的课。
@Component
@ConfigurationProperties(prefix = "somePrefix")
public class AClass {
private final AnotherClass anotherClass;
private final Map<String, Double> aMap;
@Autowired
public AffinityChecks(AnotherClass anotherClass,
Map<String, Double> aMap) {
this.anotherClass = anotherClass;
this.aMap = aMap;
}
// Omissis
最后我有了下面application.yml的配置文件。
somePrefix:
aMap:
key1: 0.6
key2: 0.2
key3: 0.2
我想要的只是 Spring 在构建过程中将地图注入类型为AClass 的对象中。我得到的错误如下。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.String, java.lang.Double>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
这到底是怎么回事?
提前致谢。
【问题讨论】:
-
尝试从构造函数中删除“Map
aMap”。 docs.spring.io/spring-boot/docs/current/reference/html/… -
如果我从构造函数中删除地图,错误就会消失,因为
aMap不再被 Spring 初始化;) -
您可能需要为您的地图添加 getter
-
阅读文档应该会有所帮助:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… - 我们不支持构造函数注入,您的属性必须是 javabean 属性(因此除了映射和嵌套之外,getter 和 setter 即非标量,只需要 getter 的值)。
-
@StephaneNicoll 你是对的。这解决了我的问题。如果你想回答这个问题,我会接受。谢谢
标签: java spring spring-boot properties