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
3. The Exception
When we use @Value like this:
But, most importantly, there is no myProp property in sysconfig.properties. So we got this exception:
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, just use @Value’s 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:
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.