Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 245
Karan Avatar asked Jan 08 '18 21:01

Karan


4 Answers

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.

like image 133
Karan Avatar answered Oct 17 '22 08:10

Karan


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

like image 26
Suman Avatar answered Oct 17 '22 08:10

Suman


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

like image 7
flopoe Avatar answered Oct 17 '22 08:10

flopoe


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.

like image 5
Paul Croarkin Avatar answered Oct 17 '22 09:10

Paul Croarkin