
1. Overview
This article focuses on the Java default value for the @Value annotation in Spring. The @Value annotation resolves and injects property values into fields, constructor, or method arguments from property files. See the @Value article to know more about the annotation.
Here, we will discuss adding the default value to the annotation.
2. Default value annotation
It’s not always necessary to assign a value when you declare a field. The compiler will set fields that are declared but not initialized to a reasonable default. Usually, this default will be zero or null
based on the data type.
For example in Java, int has a default value 0, string - null, boolean - false, so on.
Similarly, It is possible to provide a default value to the @Value annotated field or argument. You had to add the default value after the colon inside the ${ } placeholder.
In this annotation @Value(“${reporting.required:false“}), false is the default value for reporting.required
. If the Spring cannot resolve the property, then assigns the default value false.
2.1. Default value for missing property file
You can also force the default value when the property file is missing by setting the ignore-resource-no-found
to true.
For Java configurations, specify true to the setIgnoreResourceNotFound
function.
package com.tedblob.value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @ComponentScan public class ApplicationConfiguration { @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceHolder = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceHolder.setIgnoreResourceNotFound(true); return propertySourcesPlaceHolder; } }
For XML based configurations, specify as below
<context:property-placeholder ignore-resource-not-found="true"/>
2.2. Default value for primitive types and String
You can set the default value to primitive types and strings. You can use zero-length string as default value whereas, for primitive types, you must specify default values and cannot leave it empty.
@Value(“${reporting.email:}” - zero-length string after the colon. So the default value is an empty string.
package com.tedblob.value; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component class Reporting { private final String emailAddress; private final boolean required; private final int count; public Reporting(@Value("${reporting.email:}") String emailAddress, @Value("${reporting.required:false}") boolean required, @Value("${reporting.reportCount:1}") int count) { this.emailAddress = emailAddress; this.required = required; this.count = count; } public void print() { System.out.println("Email Address : " + emailAddress); System.out.println("Reporting required : " + required); System.out.println("Reporting count : " + count); } }
2.3. Default value for array annotation
Similarly you can specify comma-separated array values as default to your array placeholder as:
public Admin(@Value("${admin.users:A,B,C}") String[] users, @Value("#{${admin.roles}}") Map<String,String> roles) { this.users = new ArrayList<String>(Arrays.asList(users)); /* [A, B, C] */ this.roles = roles; }
If the admin.users
are not present in the property, then the resolver injects the default array values A, B, C into the argument users
.
2.4. Default value for Map annotation
Similar to the list, you can also set a default value for your Map as below:
public Admin(@Value("${admin.users:A,B,C}") String[] users, @Value("#{${admin.roles:{role1: \"1\", role2: \"2\", role3: \"3\"}}}") Map<String,String> roles) { this.users = new ArrayList<String>(Arrays.asList(users)); /* {role1=1, role2=2, role3=3} */ this.roles = roles; }
2.5. Default value with SpEL expression
@Value annotation supports SpEL expression and you can also specify default value with SpEL expression.
You must use Elvis (?:) operator to specify the default value. The below code looks for system property and environment variables. If the resolver cannot resolve the property, then injects the specified default value DOWN
or NA
into the arguments.
public HealthCheck(@Value("#{systemProperties['healthcheck'] ?: 'DOWN'}") String healthcheck, @Value("#{systemEnvironment['JAVA_HOME'] ?: 'NA'}") String javaHome) { this.healthcheck = healthcheck; this.javaHome = javaHome; }
3. Conclusion
To sum up, we have seen examples for the topic Java default value annotation in Spring. We have learned to add a default value to the primitive, string, map, array, and SpEL expressions annotated with @Value.