我正在使用 mvn clean install spring-boot:run
的 maven 在嵌入式 tomcat 服务器上运行 spring-boot。但每次我运行它时,我都会收到这个错误:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'language' in string value "${language}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
... 35 common frames omitted
该错误与这两行代码有关:
@Value("${language}")
private String language;
该语言标志在我的 application.properties 中指定,如下所示:
应用程序属性
language=java
logging.level.org.springframework=TRACE
这是令人困惑的部分: 当我在没有 spring-boot:run 命令的情况下运行构建时,它会正确构建,我可以毫无问题地运行构建的 jar。只有当我尝试在嵌入式 tomcat 服务器上运行时,我才会遇到这个问题。
我可以通过在我的代码中这样做来绕过这个:
@Value("${language:java}")
private String language;
但这对我来说没有意义,因为 spring 应该自动从 application.properties
文件中读取默认值。
编辑:正如人们所指出的,当在嵌入式 tomcat 服务器上运行时,它根本没有读取 application.properties
。有什么方法可以强制它读取文件或它可能不读取文件的原因?当部署到外部应用服务器而不是嵌入式应用服务器时,它工作正常。
在此先感谢您的帮助。
原文由 Karan 发布,翻译遵循 CC BY-SA 4.0 许可协议
通过将这些行添加到
<resources>
部分下的 pom 来修复我不完全理解的是这样做的必要性。
a) 我可以在外部应用程序服务器上运行它而无需添加此行,并且应用程序读取
application.properties
就好了。b) 我可以在 Eclipse 中将应用程序作为独立的 java 应用程序运行(即,无需通过 maven 构建应用程序),它显示
application.properties
很好c) 无论如何,spring-boot 不应该默认读取它吗? (如上面两个案例所示?)
感谢大家的帮助。希望这会对其他人有所帮助。