1. Introduction

When we read properties from files,Sometimes, you would get exception of reading the properties.

This article would demo how to resolve IllegalArgumentException:Could not resolve placeholder when using @Value in Spring app or SpringBoot apps.

2. Environments

  • SpringBoot 1.x and 2.x

3. The Exception

When we use @Value like this:

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

But, most importantly, there is no myProp property in sysconfig.properties. So we got this exception:

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

4. How to fix it

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

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, just use @Value’s default value:

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

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;
    ...
}

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 exception ,just provide the default values of the @Value.