Can I set null as the default value for a Value in Spring?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Yes, you can set null as the default value for a @Value property in Spring, but not with the literal string null. The correct syntax uses Spring Expression Language (SpEL): @Value("${property.name:#{null}}"). This is the officially supported way to inject a true null reference when a property is missing.
The Problem: Why null Does Not Work Directly
When you write a default value in the @Value annotation, Spring treats it as a string literal. So "null" becomes the four-character string "null", not a Java null reference.
This catches many developers off guard because it looks correct at first glance.
The Solution: Use #{null} SpEL Expression
The #{null} syntax tells Spring to evaluate null as a SpEL expression, which produces an actual Java null:
This works because of how Spring processes the annotation:
- Spring first resolves
${some.property:#{null}}as a property placeholder. - If
some.propertyexists, its value is used. - If
some.propertydoes not exist, the default#{null}is evaluated as a SpEL expression. - SpEL evaluates
nullas Java'snull.
Complete working example
In this example, both emailOverride and webhookUrl default to null when not configured, so the send method skips them naturally. No special string comparison needed.
Works With All Types, Not Just Strings
The #{null} default works with any object type:
For primitive types (int, long, boolean), you cannot use #{null} because primitives cannot hold null. Use the wrapper types (Integer, Long, Boolean) instead.
Alternative Approaches
Using Optional<String> (Spring 4.3+)
Spring supports injecting Optional for properties that may not exist:
However, Optional as a field type is generally discouraged in Java. The #{null} approach with a nullable field is more idiomatic.
Using @ConfigurationProperties (recommended for complex configurations)
For applications with many optional properties, @ConfigurationProperties is cleaner than multiple @Value annotations:
With @ConfigurationProperties, fields default to null naturally because that is Java's default for uninitialized object fields. No special syntax needed.
Post-processing with @PostConstruct
If you are stuck with an older Spring version that does not support #{null}, you can post-process the string:
This is a workaround, not a recommendation. Use #{null} when possible.
Comparison of Approaches
| Approach | Syntax | Produces true null | Complexity | Spring version |
#{null} SpEL | @Value("${prop:#{null}}") | Yes | Low | 3.0+ |
@ConfigurationProperties | Class field defaults | Yes | Medium | 4.0+ |
@PostConstruct workaround | Custom sentinel string | Yes (after init) | High | Any |
Literal "null" string | @Value("${prop:null}") | No (it is a string) | Low | Any |
Optional field | @Value with Optional<T> | N/A (uses Optional) | Medium | 4.3+ |
How Spring Resolves @Value Defaults
Understanding the resolution order helps avoid surprises:
- Property sources are checked in order: system properties, environment variables,
application.properties,application.yml, and any custom property sources. - If the property exists, its value is used (converted to the field type via Spring's
ConversionService). - If the property does not exist, the default after the
:is used. - If no default is specified and the property is missing, Spring throws
IllegalArgumentExceptionat startup.
The empty default (${}:}) is different from #{null}. An empty default gives you an empty string "", not null. Check which one your code actually needs.
Common Pitfalls
- Using
:nullinstead of:#{null}. This is the most common mistake.:nullgives you the string"null".:#{null}gives you Javanull. - Using
#{null}with primitive types.int,long,booleancannot hold null. UseInteger,Long,Booleaninstead. Spring will throw aTypeMismatchExceptionat startup. - Comparing with
==instead of null check. If you accidentally use:null(wrong syntax), checkingsomeField == nullis always false because the field holds the string"null". This creates silent bugs. - Forgetting that an empty default is not null.
@Value("${prop:}")sets the field to"", notnull. If your code checksfield != null, it passes but the field is empty. - Not providing any default for optional properties. Without a default, Spring fails at startup when the property is missing. Always provide a default for truly optional configuration.
- Mixing
@Valueand@ConfigurationPropertiesin the same class. While technically allowed, it makes the configuration source confusing. Pick one approach per class.
Summary
- Use
@Value("${property:#{null}}")to get a true Javanulldefault. The#{null}part is a SpEL expression, not a string literal. - Never use
@Value("${property:null}")because that produces the string"null", notnull. - The
#{null}syntax works with any object type:String,Integer,LocalDate,URL, etc. - For primitive types (
int,long,boolean), use their wrapper classes since primitives cannot be null. - For complex configurations with many optional fields, prefer
@ConfigurationPropertieswhere fields arenullby default. - Always distinguish between "null" (property absent), empty string (property present but blank), and a default value (property absent, fallback provided).
Related reading
- Can I use Class.newInstance() with constructor arguments?
- Can I use Spring WebFlux to implement REST services which get data through Kafka request/response topics?
- Can I use SpringMvc and webflux together?
- Can Java 8 code be compiled to run on Java 7 JVM?
- Can java get the processor current information?
- Can Mockito capture arguments of a method called multiple times?
- Can Mockito stub a method without regard to the argument?
- Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.