Java Spring Boot 无法解析字符串中的占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48158249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot could not resolve placeholder in string
提问by Karan
I am running spring-boot on an embedded tomcat server through maven with mvn clean install spring-boot:run. But every time I run it I get this error:
我通过 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.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
That error is regarding these two lines of code:
该错误与这两行代码有关:
@Value("${language}")
private String language;
That language flag is specified in my application.properties like this:
该语言标志在我的 application.properties 中指定如下:
application.properties
应用程序属性
language=java
logging.level.org.springframework=TRACE
This is the confusing part:When I run the build without the spring-boot:runcommand, it builds properly and I can run the built jar with no issues at all. It is only when I try to run on the embedded tomcat server I run into this issue.
这是令人困惑的部分:当我在没有spring-boot:run命令的情况下运行构建时,它构建正确,我可以毫无问题地运行构建的 jar。只有当我尝试在嵌入式 tomcat 服务器上运行时才会遇到这个问题。
I can sort of bypass this by doing this in my code:
我可以通过在我的代码中这样做来绕过这个:
@Value("${language:java}")
private String language;
But that doesn't make sense to me since spring is supposed to read the default value from the application.propertiesfile automatically.
但这对我来说没有意义,因为 spring 应该application.properties自动从文件中读取默认值。
EDIT: as people have pointed out, it is not reading application.propertiesat all when run on the embedded tomcat server. Any way to force it to read the file or a reason why it may not be reading it? It works fine when deployed to an external app server instead of the embedded one.
编辑:正如人们所指出的,application.properties在嵌入式 tomcat 服务器上运行时根本不读取。有什么方法可以强制它读取文件或它可能无法读取文件的原因?当部署到外部应用程序服务器而不是嵌入式应用程序服务器时,它可以正常工作。
Thanks in advance for your help.
在此先感谢您的帮助。
采纳答案by Karan
Fixed by adding these lines to the pom under the <resources>section
通过将这些行添加到<resources>部分下的 pom 来修复
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
What I don't fully understand is the need for doing this.
我不完全理解的是这样做的必要性。
a) I can run this on an external app server without having to add this line and the app reads application.propertiesjust fine.
a) 我可以在外部应用程序服务器上运行它而无需添加这一行,应用程序读取得application.properties很好。
b) I can run the app as a standalone java application in eclipse (i.e., without having to build the app through maven) and it reads application.propertiesjust fine
b) 我可以在 Eclipse 中将应用程序作为独立的 Java 应用程序运行(即,无需通过 maven 构建应用程序)并且它读取得application.properties很好
c) isn't spring-boot supposed to read it by default regardless? (as shown by the two cases above?)
c) 无论如何,spring-boot 不应该默认读取它吗?(如上面两个案例所示?)
Thanks everyone for their help. hopefully this will help others.
感谢大家的帮助。希望这会帮助其他人。
回答by HA S
If the above changes in pom.xmlfile still doesn't solve the problem try below option.
如果pom.xml文件中的上述更改仍然不能解决问题,请尝试以下选项。
Add the @PropertySourceannotation at the top of your class
@PropertySource在类的顶部添加注释
@PropertySource(
{
"classpath:/application.properties",
"classpath:/${spring.profiles.active}/application-${spring.profiles.active}.properties"
}
)
)
If that still doesn't solve try adding the following annotation
如果仍然无法解决,请尝试添加以下注释
@EnableAutoConfiguration
@EnableAutoConfiguration
Salam
萨拉姆
回答by Paul Croarkin
Were you, by chance, running this from Eclipse?
您是偶然从 Eclipse 运行的吗?
I had the same issue and noticed that the project did not have the Maven nature. Right-clicking on the project ->Configure->Convert to Maven Project. Then right-click on the project ->Maven->Update Project solved the issue.
我有同样的问题,并注意到该项目没有 Maven 性质。右击项目->Configure->Convert to Maven Project。然后右键单击项目->Maven->更新项目解决了问题。
回答by dobrivoje
In my case, in IntelliJ, I used a different profile. So choosing a dev profile in my case, solved an issue.
就我而言,在 IntelliJ 中,我使用了不同的配置文件。所以在我的情况下选择一个开发配置文件,解决了一个问题。
回答by Suman
I too faced a similar issue when running from IntelliJ. This worked for me : Build -> Rebuild Project.
从 IntelliJ 运行时,我也遇到了类似的问题。这对我有用:构建-> 重建项目。

