我正在通过 Maven 使用 mvn clean install spring-boot:run
在嵌入式 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 中指定如下:
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
。有什么方法可以强制它读取文件或它可能不读取文件的原因?当部署到外部应用服务器而不是嵌入式应用服务器时,它工作正常。
最佳答案
通过将这些行添加到 <resources>
下的 pom 来修复节
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
我不完全理解的是这样做的必要性。
a) 我可以在外部应用程序服务器上运行它而无需添加此行,应用程序读取 application.properties
正好。
b) 我可以在 eclipse 中将应用程序作为独立的 java 应用程序运行(即,无需通过 maven 构建应用程序),它显示为 application.properties
就好了
c) 无论如何,spring-boot 不应该默认读取它吗? (如上两例所示?)
感谢大家的帮助。希望这会帮助其他人。
关于java - Spring Boot 无法解析字符串中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48158249/