【发布时间】:2018-09-27 13:25:18
【问题描述】:
The Spring Boot documentation 表示使用@ConfigurationProperties 注释
您还需要列出要在
@EnableConfigurationProperties注解,如下图 示例:
并给出以下代码:
@Configuration
@EnableConfigurationProperties(AcmeProperties.class)
public class MyConfiguration {
}
但在下一段中说:
即使前面的配置为 AcmeProperties,我们建议
@ConfigurationProperties只处理 与环境,特别是不注入其他bean 从上下文。话虽如此,@EnableConfigurationProperties注释也会自动应用于您的项目,以便任何 existing 配置了@ConfigurationProperties注释的 bean 来自Environment。
建议不必在@EnableConfigurationProperties 注释下列出@ConfigurationProperties bean。
那是什么?实验上,我发现如果我用@ConfigurationProperties 注释一个bean,它会按预期将属性注入它,而无需在@EnableConfigurationProperties 中列出它,但如果是这种情况,那么为什么要列出任何具有@ConfigurationProperties 的东西@EnableConfigurationProperties 下的注释,如文档中所示?有什么不同吗?
【问题讨论】:
-
在这里查看我的答案。 stackoverflow.com/questions/48111941/… 这是一个示例,您可能会强制使用
@EnableConfigurationProperties -
如果我理解正确,这解释了为什么你需要在配置类上使用
@EnableConfigurationProperties,但不是为什么你需要给它一个用@Configurationproperties注释的类列表,例如@EnableConfigurationProperties(AcmeProperties.class)当 AcmeProperties 已经用@ConfigurationProperties注释时
标签: java spring spring-boot