Spring引导无法解析字符串

时间:2018-01-08 21:22:46

标签: java spring maven spring-mvc tomcat

我通过maven与mvn clean install spring-boot:run在嵌入式tomcat服务器上运行spring-boot。但是每次我运行它都会出现这个错误:

  

引起:java.lang.IllegalArgumentException:无法解析字符串值“$ {language}”中的占位符“language”       在org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)〜[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)〜[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)〜[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)〜[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.context.support.PropertySourcesPlaceholderConfigurer $ 2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)〜[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]       ...省略了35个常见帧

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

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

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

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。有什么方法可以强迫它读取文件或者它可能没有读取它的原因?部署到外部应用服务器而不是嵌入式应用服务器时,它可以正常工作。

提前感谢您的帮助。

12 个答案:

答案 0 :(得分:8)

通过将这些行添加到Kernel部分

下的pom来修复
<resources>

我不完全理解的是需要这样做。

a)我可以在外部应用服务器上运行它,而无需添加此行,应用程序只需<resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> </includes> </resource> 即可。

b)我可以在eclipse中将应用程序作为一个独立的java应用程序运行(即,无需通过maven构建应用程序)并且它可以正常读取application.properties

c)不是spring-boot应该默认读取它吗? (如上面的两个案例所示?)

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

答案 1 :(得分:5)

从IntelliJ运行时,我也面临类似的问题。 这为我工作: 生成->重建项目。

答案 2 :(得分:3)

IntelliJ 也有同样的问题。来自 File -> Invalidate Caches / Restart ... 的无效缓存为我修复了它。

答案 3 :(得分:1)

您是不是偶然从Eclipse运行此程序?

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

答案 4 :(得分:1)

就我而言,在IntelliJ中,我使用了另一个配置文件。 因此,在我的情况下,选择开发人员资料可以解决问题。

答案 5 :(得分:1)

在我的情况下改变这个:

@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-or-SpringBoot-app.html

答案 6 :(得分:0)

我缺少以下依赖项

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

我需要读取集中化配置服务器的配置值。

答案 7 :(得分:0)

我也有类似的问题,我通过解决它

在您的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@

答案 8 :(得分:0)

这里有适用于我的情况的最简单的解决方案。

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

-Dspring.profiles.active=<env>

这是修改/触摸上述pom文件的替代方法。

答案 9 :(得分:0)

如果正确设置了值,则检查您的资源文件夹是否充当IDE标识的“资源”。在intelliJ中,您可以右键单击资源,然后选择“标记为资源根”。

答案 10 :(得分:0)

就我而言,我为其他目的设置了操作系统环境变量 spring.config.location。这覆盖了 Eclipse 中 spring 配置的项目路径。

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

答案 11 :(得分:-1)

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

在班级顶部添加@PropertySource注释

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

如果仍然无法解决,请尝试添加以下注释

@EnableAutoConfiguration

萨拉姆