Spring Boot 2.0 Projects
上QQ阅读APP看书,第一时间看更新

Configuration property binding

Configuration properties are a great feature of Spring Boot for reading properties with type safety. This section will explain the concept with the following DemoApplicationProperties class file:

@ConfigurationProperties(prefix = "demo")
public class DemoApplicationProperties {

private Integer number;

private String username;

private String telephoneNumber;

private List<String> emailAddresses =
Arrays.asList("shazin@techtalks.lk");

private String firstName;

private String lastName;

private Duration workingTime;

// Getters and Setters
}

The application.yml has the following configuration properties:

demo:
number: 10
user-Name: shazin
firstName: Shazin
lAsTNamE: Sadakath
telephone_number: "0094777329939"
workingTime: 8h
EMAILADDRESSES:
- shazin.sadakath@gmail.com
- shazin.swe@gmail.com
addresses:
- number: "B 22 2/3"
city: Colombo
street: "Ramya Place"
country: "Sri Lanka"
zipCode: "01000"
- number: "123"
city: Kandy
street: "Dalada Weediya"
country: "Sri Lanka"
zipCode: "01332"

For configuration properties, an application.properties file also can be used over an application.yml file. Lately, YML files are becoming famous among developers because they can provide a structure with indentations, the ability to use collections, and so on. Spring Boot supports both at the moment.