Spring boot 无法解析字符串中的占位符

问题描述 投票:0回答:14

我正在通过 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 中指定,如下所示:

应用程序属性

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

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

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

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

但这对我来说没有意义,因为 spring 应该自动从

application.properties
文件中读取默认值。

编辑:正如人们所指出的,当在嵌入式tomcat服务器上运行时,它根本不读取

application.properties
。有什么方法可以强制它读取文件或它可能不读取文件的原因吗?当部署到外部应用程序服务器而不是嵌入式应用程序服务器时,它可以正常工作。

java spring maven spring-mvc tomcat
14个回答
40
投票

通过将这些行添加到

<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 不应该默认读取它吗? (如上面两个案例所示?)

感谢大家的帮助。希望这会帮助其他人。


21
投票
从 IntelliJ 运行时我也遇到了类似的问题。 这对我有用: 构建 -> 重建项目。


12
投票
IntelliJ 也遇到同样的问题。使缓存失效

File -> Invalidate Caches / Restart ...

 为我解决了这个问题。


7
投票
就我而言,改变这一点:

@Value("${files.root}")
对此:

@Value("${files.root:default}")
成功了。

我的解决方案请参阅以下内容:

https://www.bswen.com/2019/06/springboot-How-to-resolve-IllegalArgumentException-Could-not-resolve-placeholder-when-use-@Value-in-Spring -或-SpringBoot-app.html


5
投票
您是偶然从 Eclipse 运行此程序吗?

我遇到了同样的问题,并注意到该项目不具有 Maven 性质。右键单击项目->配置->转换为 Maven 项目。然后右键项目->Maven->更新项目解决了问题。


3
投票
就我而言,在 IntelliJ 中,我使用了不同的配置文件。 因此,在我的案例中选择开发配置文件解决了一个问题。


3
投票
我和你有类似的问题,我通过去解决了它

在你的

pom.xml

如果您有多个配置文件或属性,请选择 1 个配置文件作为默认选择

<profiles> <profile> <id>dev</id> <properties> <activatedProperties>dev</activatedProperties> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <activatedProperties>prod</activatedProperties> </properties> </profile> </profiles>
然后转到您的

application.properties

,然后插入此代码

spring.profiles.active=@activatedProperties@
    

2
投票
我缺少以下依赖项

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
我需要读取集中配置服务器的配置值。


1
投票
如果您已正确设置这些值,请检查您的资源文件夹是否充当 IDE 识别的“资源”。在 intelliJ 中,您可以右键单击资源并选择“标记为资源根”。


1
投票
我也发生过。就我而言,问题是由于我将值放入错误的文件中而引起的。 错误的

BIN/src/main/resource/application.properties 正确的 src/main/resource/application.properties


0
投票
这里有适合我的情况的最简单的解决方案。

无论是来自 Java 命令提示符,还是来自 IDE(例如 eclipse 或 IntelliJ)的 VM 选项,请尝试提供以下内容:

-Dspring.profiles.active=<env>
这是修改/触摸上述 pom 文件的替代方法。


0
投票
就我而言,我出于其他目的设置了操作系统环境变量

spring.config.location

。这覆盖了 eclipse 中 spring 配置的项目路径。

删除环境变量后,重启并在eclipse中重新导入项目,上述问题得到解决


0
投票
我丢失了

SpringApplication

 
defaultProperties
 的财产😑。

我必须添加属性

"spring.profiles.default"

 以及值 
"dev"


-4
投票
如果

pom.xml

 文件中的上述更改仍然无法解决问题,请尝试以下选项。

在类顶部添加

@PropertySource

 注释

@PropertySource( { "classpath:/application.properties", "classpath:/${spring.profiles.active}/application-${spring.profiles.active}.properties" }

如果仍然不能解决请尝试添加以下注释

@EnableAutoConfiguration



萨拉姆

© www.soinside.com 2019 - 2024. All rights reserved.