Spring Boot无法解析字符串中的占位符 [英] Spring boot could not resolve placeholder in string

查看:571
本文介绍了Spring Boot无法解析字符串中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mvn clean install spring-boot:run通过maven在嵌入式tomcat服务器上运行spring-boot.但是每次运行它时,都会出现此错误:

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:

 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

该错误与这两行代码有关:

That error is regarding these two lines of code:

@Value("${language}")
private String language;

在我的application.properties中指定该语言标志,如下所示:

That language flag is specified in my application.properties like this:

application.properties

language=java
logging.level.org.springframework=TRACE

这是令人困惑的部分::当我在没有 spring-boot:run 命令的情况下运行构建时,它可以正确构建,并且我可以毫无问题地运行构建的jar完全没有.只有当我尝试在嵌入式tomcat服务器上运行时,我才遇到此问题.

This is the confusing part: When I run the build without the spring-boot:run command, 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.

我可以通过在我的代码中执行以下操作来绕过此操作:

I can sort of bypass this by doing this in my code:

@Value("${language:java}")
private String language;

但这对我来说没有意义,因为应该让spring自动从application.properties文件读取默认值.

But that doesn't make sense to me since spring is supposed to read the default value from the application.properties file automatically.

编辑:正如人们指出的那样,在嵌入式tomcat服务器上运行时,它根本不读取application.properties.有什么方法可以迫使它读取文件,或者是为什么它无法读取文件的原因?将其部署到外部应用服务器而不是嵌入式应用服务器时,它可以很好地工作.

EDIT: as people have pointed out, it is not reading application.properties at 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.

在此先感谢您的帮助.

Thanks in advance for your help.

推荐答案

通过在<resources>部分下的pom中添加这些行来解决

Fixed by adding these lines to the pom under the <resources> section

<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)我可以在外部应用服务器上运行此命令,而不必添加此行,并且应用程序读取application.properties就好了.

a) I can run this on an external app server without having to add this line and the app reads application.properties just fine.

b)我可以在eclipse中将应用程序作为独立的Java应用程序运行(即,无需通过maven来构建应用程序),并且读取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.properties just fine

c)不管怎样,spring-boot都不应该默认读取它吗? (如以上两种情况所示?)

c) isn't spring-boot supposed to read it by default regardless? (as shown by the two cases above?)

感谢大家的帮助.希望这对其他人有帮助.

Thanks everyone for their help. hopefully this will help others.

这篇关于Spring Boot无法解析字符串中的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆