Read From Properties File Spring Boot Using @value

In this article , we will learn how to read values from properties file using @value annotation in spring boot.

Suppose you have kept some fields like this in application.properties file

to get key field value, we can use

@Value("${key:someDefault}")
private String key;

Here we have used Default value as well. This is good practise. Just incase key field is missing in application properties your code wont break as default value will be assigned.

Similarly if I want to use rate or delay I can use like this .

@Value("${key:someDefault}")
private Integer key;

SpringBoot is smart enough to understand the type based on value in application properties.

  • Java Streams anyMatch vs Streams noneMatch

  • Streams Summary Statistics in Java

  • 5 Different Ways To Sum All Elements in Array Java

  • Print Only Odd Numbers in Array Using Streams in Java

Loading


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *