0% found this document useful (0 votes)
15 views

Using Spring @value With Defaults

Uploaded by

Sudheer Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Using Spring @value With Defaults

Uploaded by

Sudheer Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sudheer Kumar Dachapalli

From: Sudheer Kumar Dachapalli


Sent: 21 July 2022 12:57
To: Abhishek DilipKumar Dhapade; Bharath Kumar Subramanian Kunuvalu; Giri Babu
Dasari; Hafeez Mohammad; Karthik Thangaraj; Kiran Goud Marupakula; Kuldip
Jahagirdar; Maraj Khan; Mithun Kumar Gupta; Mushtaq Aliuddin; Nikhil Gaur; Nikhila
Duvva; Prathap Gangavelli; Rohitha Gourabathuni; Sahithi Burra; Sai Spandhan
Reddy Pillutla; Sai Venkata Prasad Kotikalapudi; Samshitha Chippa; Santosh Reddy
Sadi; Sourav Banerjee; Sputhnika Karra; Srikanth Kundella; Srinivas Chanda; Subhash
Naidu Tirumalasetty; Sudheer Kumar Dachapalli; Sunil Chandra Kanchi; Swapna Rani
Valusa; Swetapadmasini Nayak; Uday Kumar Emkolla; Venkata Santoshi Lakshmi
Vetcha; Venkatesh Yada; Vikas Bollu; Vishal Ummadi
Subject: 📣Using Spring @Value With Defaults

// Best Practices

Hi Backend Team,
While declaring config values, pass default values as well, even if you miss in properties application will start. This is
stopping application to start.

Strings:
@Value("${some.key:my default value}")
private String stringWithDefaultValue;

ex:
Similarly, we can set a zero-length String as the default value:

@Value("${some.key:})"
private String stringWithBlankDefaultValue;

Primitives
To set a default value for primitive types such as boolean and int, we use the literal value:

@Value("${some.key:true}")
private boolean booleanWithDefaultValue;

@Value("${some.key:42}")
private int intWithDefaultValue;

Arrays
We can also inject a comma separated list of values into an array:

@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults;

@Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;

Using SpEL(Spring Expression Language (SpEL))

We can also use SpEL to specify an expression and a default.

1
In the example below, we expect some.system.key to be set as a system property, and if it is not set, we want to use
my default system property value as a default:

@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")


private String spelWithDefaultValue;

Ref:
https://www.baeldung.com/spring-value-defaults

Thanks and regards,


Sudheer Kumar Dachapalli
Senior Solution Architect
TOGAF® 9.2 Certified - Architect
AWS Certified Solutions Architect – Associate
Java EE 6 Enterprise Architect Certified
Mobile:+91 789 333 0552, +1 309 889 8687
ValueMomentum Software Services Pvt. Ltd.

You might also like