๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOGjava.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)

Solution 1โœ“ Most common cause

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;
Solution 2

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/mydb
Solution 3

Inject 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"); } }
Solution 4

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 aligned
Solution 5

Ensure 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

Spring Boot 2.x

Standard property resolution.

Spring Boot 3.x

Stricter handling of nested placeholders.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Always provide default values in `@Value` for non-critical properties: `${key:default}`.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase