【发布时间】:2018-11-12 06:14:08
【问题描述】:
我是 Spring Boot 新手,我想使用占位符 @PropertySource("file:${application_properties}") 从外部文件中读取属性,因此在配置文件中使用了前面提到和定义的注释
@Bean,
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
但我得到了:
无法解析值“file:${application_properties}”中的占位符“application_properties”
我在tomcat server.xml/context.xml 中配置了applications_properties(不确定究竟是哪个文件)
注意:如果我使用@PropertySource("file:c:\filename"),它工作正常,但我想使用占位符并想在 tomcat 中定义该占位符(也想知道如何做到这一点)。您能否帮我使用@PropertySource 读取文件属性,这将读取tomcat 中定义的占位符。
谢谢
【问题讨论】:
-
另一个更新是我只想使用文件而不是类路径
-
你不能。在启动时替换占位符时仅考虑系统和环境变量。此外,您不需要
PropertySourcesPlaceholderConfigurer的 bean,因为 Spring Boot 已经添加了那个。 -
我们可以在任何 tomcat 文件中添加这些变量,以便当我们启动 tomcat 时它会加载所有这些系统和环境变量吗?(我知道我们可以从 eclipse 中作为参数传递,但任何添加这些变量的可能性归档,以便我们可以跟踪所有属性。
标签: spring spring-boot