Skip to content

How to resolve IllegalArgumentException Could not resolve placeholder when using @Value in Spring Boot application

1. Introduction

When we read properties from files into Spring Boot applications, Sometimes, you will get exception of reading the properties.

This post will demo how to resolve the IllegalArgumentException:Could not resolve placeholder exception when using @Value in Spring app or Spring Boot apps.

2. Environments

  • Spring Boot

3. The Exception

When we use @Value like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp}")
private String myProp;
...
}

However, most importantly, if there is no myProp property in sysconfig.properties, we will get this exception:

Terminal window
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:846)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1087)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1067)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:364)
... 35 common frames omitted

The key error message Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}" indicated that there is no property named myProp in the custom properties file named sysconfig.properties.

4. How to fix it

4.1 Way 1: Add the property key to your properties file

src/main/resources/sysconfig.properties
myProp = test

4.2 Way 2: Provide a default value for the property

If you don’t want to provide the key in your file, you can just set @Value’s default value:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:defaultValue}")
private String myProp;
...
}

You can change the defaultValue to your real default value.

4.3 Way 3: Provide an empty default value for the property

If you want to provide an empty default value, just do like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:}")
private String myProp;
...
}

Notice the myProp: , the colon is important.

Just leave empty after the colon, then you would get an empty default value for property myProp.

5. Summary

You can see that @Value must be used carefully, if you get the above exceptions ,just provide the default values of the @Value.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!