我正在通过 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
。有什么方法可以强制它读取文件或它可能不读取文件的原因吗?当部署到外部应用程序服务器而不是嵌入式应用程序服务器时,它可以正常工作。
通过将这些行添加到
<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 不应该默认读取它吗? (如上面两个案例所示?)
感谢大家的帮助。希望这会帮助其他人。
File -> Invalidate Caches / Restart ...
为我解决了这个问题。
我遇到了同样的问题,并注意到该项目不具有 Maven 性质。右键单击项目->配置->转换为 Maven 项目。然后右键项目->Maven->更新项目解决了问题。
在你的
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@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
我需要读取集中配置服务器的配置值。
BIN/src/main/resource/application.properties 正确的 src/main/resource/application.properties
无论是来自 Java 命令提示符,还是来自 IDE(例如 eclipse 或 IntelliJ)的 VM 选项,请尝试提供以下内容:
-Dspring.profiles.active=<env>
这是修改/触摸上述 pom 文件的替代方法。
spring.config.location
。这覆盖了 eclipse 中 spring 配置的项目路径。删除环境变量后,重启并在eclipse中重新导入项目,上述问题得到解决
SpringApplication
defaultProperties
的财产😑。我必须添加属性
"spring.profiles.default"
以及值
"dev"
。
pom.xml
文件中的上述更改仍然无法解决问题,请尝试以下选项。在类顶部添加
@PropertySource
注释
@PropertySource(
{
"classpath:/application.properties",
"classpath:/${spring.profiles.active}/application-${spring.profiles.active}.properties"
}
)
如果仍然不能解决请尝试添加以下注释
@EnableAutoConfiguration