Spring boot 无法解析字符串中的占位符
我正在通过maven在嵌入式tomcat服务器上运行spring-boot。但是每次我运行它时,我都会得到这个错误:mvn clean install spring-boot:run
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
提前感谢您的帮助。