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:
However, most importantly, if there is no myProp property in sysconfig.properties
, we will get this exception:
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
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:
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:
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!