๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"โก Quick Fix Works 80% of the time
Add a default value to your @Value annotation or ensure the property exists in application.properties.
@Value("${spring.datasource.url:jdbc:mysql://localhost:3306/db}")
private String dbUrl;๐ง Why this Happens
Tap to expand the deep technical explanation
You used the `@Value` annotation to inject a property. Spring searched all configuration files and environment variables for that key, but couldn't find it, causing a fatal startup error.
The HITEC City Parking Spot Analogy:
A mailman is given a letter to deliver to '123 Main St', but that address doesn't exist in the city. The mailman (Spring) stops working because they cannot resolve the destination.
๐ How to Reproduce Confirm this is your error
In a `@Service` class, add `@Value("${nonexistent.property}") private String prop;`. Run the app without defining `nonexistent.property` in `application.properties`.
๐ ๏ธ Solutions (5 Ways to Fix)
Provide a default value in @Value
๐ Use this if the property is optional or you want a fallback for local development.
Adding a colon and a default value inside the @Value annotation prevents Spring from crashing if the property is missing.
@Value("${spring.datasource.url:jdbc:mysql://localhost:3306/defaultdb}")
private String dbUrl;Add the property to application.properties
๐ Use this if the property is mandatory and should be in your config file.
Ensure the property key exactly matches the placeholder string (case-sensitive).
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydbInject via Environment object instead
๐ Use this if you need to check multiple properties dynamically.
Instead of @Value, inject the Environment object and use getProperty, which returns null instead of crashing if missing.
@Autowired
private Environment env;
public void connect() {
String url = env.getProperty("spring.datasource.url");
if (url == null) {
throw new RuntimeException("Database URL must be set");
}
}Check YAML indentation
๐ Use this if you are using application.yml and are sure the URL is there.
YAML is strictly indented. If `url:` is not perfectly aligned under `datasource:`, Spring won't read it.
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb # Must be perfectly alignedEnsure environment variables are loaded
๐ Use this if you are deploying with environment variables.
If you pass properties via OS environment variables, ensure they are named correctly (e.g., SPRING_DATASOURCE_URL).
# Environment variables must use underscores instead of dots
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/mydb
java -jar my-app.jar๐ Version Notes
Standard property resolution.
Stricter handling of nested placeholders.
๐ก๏ธ How to Prevent This Next Time
Always provide default values in `@Value` for non-critical properties: `${key:default}`.