如何解决当@ConfigurationProperties和@Value一起使用时,无法解决占位符错误
我们有一个使用yaml的springboot项目MainProject。主配置类看起来像这样
@SpringBootApplication(scanBasePackages={"com.xyz.platform"})
@EnableCircuitBreaker
@EnableHystrix
@ImportResource("classpath:spring/beans-xyz-abc.xml")
@ConfigurationProperties(prefix="config.prefix")
@Validated
// this is an external sdk jar. this contains more beans and used @Value
// annotaion for property binding
@Import(SDKConfiguration.class)
public class AuthorizationDelegationWsApplication extends SpringBootServletInitializer {
// class code here
}
以上配置类使用@Import导入了另一个外部SDK配置类。外部类SDKConfiguration看起来像这样
@Configuration
@PropertySources({
@PropertySource(value = { "classpath:client.properties" },ignoreResourceNotFound = false)
})
@EnableAutoConfiguration
public class SDKConfiguration {
@Value("${host:}")
private String host;
// class code here
}
外部SDK在类路径中具有client.properties文件,在其中也具有“ host”属性。 在添加外部配置类之前,一切正常。但是添加完之后,服务启动失败并显示以下错误
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'host' in value "${host}""
我尝试通过将外部SDK移到@ConfigurationProperties而不是@Value来对其进行编辑。它不会引发任何异常,但是由于未设置“主机”,所以我得到了NPE。
我尝试将“主机”更改为“ config.prefix.host”。但是没用。
我认为问题是因为同时使用@Value和@ConfigurationProperties。但是我找不到任何文档。
请让我知道我可以尝试哪些选择。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。