35

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Karan
  • 1,335
  • 2
  • 14
  • 29
  • Are you sure your embedded tomcat sees the properties file and loads it? – tsolakp Jan 08 '18 at 21:28
  • Yea - it looks like it is not reading the properties file. But I am not sure why. It can read it when deployed on an external tomcat server or when run as a java application so this is confusing me. – Karan Jan 08 '18 at 21:31
  • 1
    I am having exactly same issue but in opposite scenario. In my case, I have in my *application.properties* setting like ```logging.file.name=${log_dir} ``` and in Liberty *server.env* file like ```log_dir=C:\mydir". If I run it in Liberty server, I get this same error. The only way to resolve it I found is to hardcode the setting in my *application.properties* like ```logging.file.name=C:\mydir```. But that of course is not the great solution. – pixel Mar 01 '23 at 19:23

13 Answers13

40

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) I can run this on an external app server without having to add this line and the app reads application.properties just fine.

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

Karan
  • 1,335
  • 2
  • 14
  • 29
  • 2
    I'm guessing you do not inherit spring-boot-starter-parent in your pom.xml that's why. – user1455836 Jul 30 '18 at 08:30
  • 1
    for whatever reason `**/*` didn't work for me so I hard to add `src/main/resources/config` for my files located under the config dir – Archmede Apr 08 '20 at 22:32
  • This is true, but for deploying it as JAR file at remote server, We need properties file excluded from JAR file. Hence including them like this, defeat the purpose of having property file. Can you comment on this? I had them explicitly under build->resources->excludes TAG. – u tyagi Feb 03 '22 at 09:20
21

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

Suman
  • 818
  • 6
  • 17
12

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

flopoe
  • 165
  • 1
  • 9
7

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

TarangP
  • 2,711
  • 5
  • 20
  • 41
pserimer
  • 233
  • 4
  • 8
  • I don't understand why this worked but thank you! – Arthur Champs Dec 30 '21 at 22:49
  • This worked for me, if you are having multiple .properties files, then please add `:default` – Cipher Dec 12 '22 at 12:10
  • setting default like this means hard-coding it. In many cases you dont want to do that, for example , the value of `files.root` might change per invironment and you dont want to keep its value in application.property if you do not want to expose it to everyone once pushed to repo. So, you might want this value to come from server/container directly in access it in application.properties like `files.root=${files_root}`. The `files_root` could be set in the server env files but in that case, it will not be resolved by using @Value for some reason – pixel Mar 01 '23 at 22:52
5

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.

Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
3

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

dobrivoje
  • 848
  • 1
  • 9
  • 18
3

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@
Jaypee Tan
  • 101
  • 1
  • 10
2

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 .

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • In my case, when I got message "Could not resolve placeholder" while it was exist in configuration service - adding this dependency solve the problem. Also I add -Dspring.cloud.bootstrap.enabled=true to run script. – Kirill Mikhailov Nov 03 '21 at 04:46
1

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

SamA
  • 43
  • 6
1

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

aga menor
  • 11
  • 1
0

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.

jkim
  • 152
  • 1
  • 3
  • 15
0

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

Monish Sen
  • 1,773
  • 3
  • 20
  • 32
-4

If the above changes in pom.xml file still doesn't solve the problem try below option.

Add the @PropertySource annotation at the top of your class

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

)

If that still doesn't solve try adding the following annotation

@EnableAutoConfiguration

Salam

HA S
  • 1,129
  • 12
  • 10
  • Please include proper reasoning in your answers – Rishi Nov 21 '19 at 07:03
  • This is not a fix patch forum. Stack-overflow gives you a platform to learn more about what a specific problem is occurring and then find a solution. – Rishi Nov 22 '19 at 08:11