简体   繁体   English

使用Spring Boot时无法解析@PropertySource中的$ {…}

[英]Unable to resolve ${…} in @PropertySource when using Spring boot

I am using Spring's Java configuration to configure my application. 我正在使用Spring的Java配置来配置我的应用程序。 Below is a snippet of the annotations. 以下是注释的片段。 There is no problem with the application deployed to the Tomcat container. 部署到Tomcat容器的应用程序没有问题。 The ${CONFIG_PATH} resolves correctly as the CONFIG_PATH is defined in the system/environment properties. ${CONFIG_PATH}可以正确解析,因为CONFIG_PATH是在系统/环境属性中定义的。

@Configuration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = { Configuration.class }) })
@PropertySource(value = { "file:${CONFIG_PATH}/app.properties" })
@EnableAutoConfiguration
@EnableMongoRepositories
public class AppConfig { ... }

I recently started using Spring-Boot so that I can build a standalone executable jar with Tomcat embedded. 我最近开始使用Spring-Boot,以便可以构建嵌入了Tomcat的独立可执行jar。 However, now the @PropertySource(value = { "file:${CONFIG_PATH}/app.properties" }) does not work; 但是,现在@PropertySource(value = { "file:${CONFIG_PATH}/app.properties" })无法正常工作; it results in the following error. 它导致以下错误。

Exception in thread "main" java.lang.IllegalStateException: Resource not found: file:${CONFIG_PATH}/app.properties

My temporary solution is to hardcode the path as follows. 我的临时解决方案是按如下所示对路径进行硬编码。

//@PropertySource(value = { "file:${CONFIG_PATH}/app.properties" })
@PropertySource(value = { "file:/usr/local/etc/app.properties" })

I'm still relatively new to the Spring camp. 我对春季训练营还比较陌生。 So I'm sure that I'm missing something. 所以我确定我缺少一些东西。 I greatly appreciate any help on this problem. 我非常感谢您对这个问题的帮助。

Thanks in advance. 提前致谢。

UPDATE 1 : I looked the at the default appplication.properties and found the 4 location given to be wanting. 更新1 :我查看了默认的appplication.properties ,发现想要的4个位置。 For reasons of clean deployment, properties file should be not located in the classpath. 出于干净部署的原因,属性文件不应位于类路径中。 And having the properties file located relative to where the application is located makes it harder to manage the configuration if we have more than one application deployed. 而且,如果我们部署了多个应用程序,则相对于应用程序所在的位置放置属性文件将使管理配置变得更加困难。

From the Spring Boot README 从Spring Boot 自述文件

If you don't like application.properties as the configuration file name you can switch to another by specifying spring.config.name environment property. 如果您不喜欢application.properties作为配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个。 You can also refer to an explicit location using the spring.config.location environment property. 您还可以使用spring.config.location环境属性引用显式位置。

I would probably just use this startup parameter (in a bootscript or something) to specify the configuration file. 我可能只是使用此启动参数(在启动脚本之类的东西中)来指定配置文件。

I guess the reason the replacement doesn't work is due to the nature of spring boot. 我想替换不起作用的原因是弹簧靴的性质。 The application context is also used to bootstrap/configure the servlet container and as such the normal rules for @PropertySource probably doesn't apply. 应用程序上下文还用于引导/配置servlet容器,因此@PropertySource的常规规则可能不适用。 You could (or maybe should) register an issue for this. 您可以(或应该)为此注册一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM