'Spring boot could not resolve placeholder in string

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;

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

application.properties

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

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;

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

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.



Solution 1:[1]

I too faced a similar issue when running from IntelliJ. This worked for me : Build -> Rebuild Project.

Solution 2:[2]

Got the same issue with IntelliJ. Invalidating caches from File -> Invalidate Caches / Restart ... fixed it for me.

Solution 3:[3]

Were you, by chance, running this from 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.

Solution 4:[4]

In my case, in IntelliJ, I used a different profile. So choosing a dev profile in my case, solved an issue.

Solution 5:[5]

In my case changing this:

@Value("${files.root}")

to this:

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

worked.

See following for my solution: https://www.bswen.com/2019/06/springboot-How-to-resolve-IllegalArgumentException-Could-not-resolve-placeholder-when-use-@Value-in-Spring-or-SpringBoot-app.html

Solution 6:[6]

I have similar issue with you, I fixed it by going

In your pom.xml if you have multiple profile or properties, select 1 profile to be default selected

<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>

Then go to your application.properties then insert this code

spring.profiles.active=@activatedProperties@

Solution 7:[7]

I was missing the below dependency

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

I need read config values for ceneralized config server .

Solution 8:[8]

If you have set the values properly, then check if your resources folder is acting as "resources" identified by the IDE. In intelliJ you can right click on resources and select "Mark as resources root".

Solution 9:[9]

Happened with me too. in my case the problem was caused because I put the value in wrong file. wrong BIN/src/main/resource/application.properties right src/main/resource/application.properties

Solution 10:[10]

Here I have simplest solution that worked in my case.

Whether it be from Java command prompt, or VM options from IDE such as eclipse or IntelliJ, try supplying the following:

-Dspring.profiles.active=<env>

This is alternative to modifying/touching the pom file mentioned above.

Solution 11:[11]

In my case I had set an OS environment variable spring.config.location for some other purpose. This was over-riding the project path for spring config in eclipse.

After removing the environment variable, rebooting and re-importing projects in eclipse the above issue was resolved

Solution 12:[12]

How to create a new chat:

import axios from 'axios';

export const newChat = (title, projectId, username, secret, callback) => {
  axios
    .post(
        `https://api.chatengine.io/chats/`, 
        { title: title }, 
        { headers: { 
            "Public-Key": projectId, 
            "User-Name": username, 
            "User-Secret": secret 
        } }
    )

    .then((response) => {
      callback && callback(response.data);
    })

    .catch((e) => console.log('New Chat Error', e));
};

How to add a User to a chat:

import axios from 'axios';

export const invite: Invite = (chatId, inviteUsername, projectId, username, secret, callback) => {
  axios
    .post(
        `https://api.chatengine.io/chats/${chatId}/people/`, 
        { username: inviteUsername }, 
        { headers: { 
            "Public-Key": projectId, 
            "User-Name": username, 
            "User-Secret": secret 
        } }
    )

    .then((response) => {
      callback && callback(response.data);
    })

    .catch((e) => console.log('Invite Error', e));
};