A Practical Guide to Correctly Specifying Default Values in Spring @Value Annotation

Dec 05, 2025 · Programming · 9 views · 7.8

Keywords: Spring | @Value annotation | default value

Abstract: This article delves into the proper usage of the @Value annotation in the Spring framework, focusing on how to specify default values using property placeholder syntax (${...}) rather than SpEL expressions (#{...}). It explains common errors, such as expression parsing failures, and provides solutions for both XML and Java configurations, including setting ignore-resource-not-found to ensure default values take effect. Through code examples and step-by-step explanations, it helps developers avoid configuration pitfalls and achieve flexible and robust property injection.

In the Spring framework, the @Value annotation is a core tool for dependency injection, used to inject values from external sources, such as property files, into bean fields. However, many developers encounter issues when specifying default values, especially when using incorrect syntax or configurations. Based on common error cases, this article explains in detail how to correctly use the @Value annotation to specify default values, ensuring applications run smoothly even when properties are missing.

Analysis of Common Errors

In initial attempts, developers use SpEL (Spring Expression Language) expressions like #{props.isFPL} to inject boolean values, which correctly reads from property files. But when trying to add a default value, such as #{props.isFPL:false}, it leads to a parsing error: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1041E:(pos 28): After parsing a valid expression, there is still more data in the expression: 'colon(:)'. This occurs because SpEL expressions do not support using colons to specify default values within property placeholders; their syntax is more suited for complex expression evaluation, not simple fallback mechanisms.

Correct Approach: Using Property Placeholder Syntax

To specify default values, use the property placeholder syntax ${...} instead of SpEL expressions. For example: @Value("${props.isFPL:true}"). Here, true after the colon is the default value; if the property props.isFPL is undefined, Spring injects true. However, developers report that with this method, while the default value works, the correct value from the property file is not retrieved. This is often due to configuration issues preventing proper loading of property sources.

Configuration Solutions

To ensure property files are correctly read and default values take effect when missing, configure Spring to ignore resource-not-found errors. In XML configuration, add <context:property-placeholder ignore-resource-not-found="true"/>. In Java configuration, define a PropertySourcesPlaceholderConfigurer bean and set setIgnoreResourceNotFound(true). For example:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
    p.setIgnoreResourceNotFound(true);
    return p;
}

This way, if the property file is missing or a property is undefined, Spring falls back to the default value specified in the annotation without throwing exceptions.

Additional Notes and Other Types

For other data types, such as integers, the property placeholder syntax applies similarly. For example: @Value("${my.int.config: #{100}}"). Note that SpEL expressions can be included after the colon as default values, but ensure correct syntax to avoid spacing issues. In practice, it is recommended to use simple literals as defaults to reduce complexity.

Summary and Best Practices

The key to correctly specifying default values with the @Value annotation lies in: using property placeholder syntax ${property:defaultValue}, not SpEL expressions; configuring Spring to ignore resource-not-found errors; and adjusting default value formats based on data types. By following these steps, developers can build more robust applications and handle property injection scenarios flexibly.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.