Close

Spring Boot - @ConfigurationProperties Examples

Spring Boot 

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties("app")
public class MyAppProperties {
private List<String> adminEmails;
boolean sendEmailOnErrors;
boolean exitOnErrors;
private int refreshRate;
private Map<String, String> orderScreenProperties;
private Map<String, String> customerScreenProperties;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.refresh-rate=15
app.admin-emails[0]=jim@example.com
app.admin-emails[1]=gina@example.com
app.admin-emails[2]=tom@examle.com

src/main/resources/application.yml:

app:
send-email-on-errors: true
exit-on-errors: true
order-screen-properties:
title: Order Placement
item-label: Item
qty-label: Qunatity
customer-screen-properties:
title: Customer Profile
name-label: Customer Name
phone-label: Contact Number
Original Post




import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("app")
public class MyAppProperties {
private boolean sendEmailOnErrors;
private boolean exitOnErrors;
private OrderScreenProperties orderScreenProperties;
private CustomerScreenProperties customerScreenProperties;
private MainScreenProperties mainScreenProperties;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.main-screen-properties.refresh-rate=15
app.main-screen-properties.width=600
app.main-screen-properties.height=400

src/main/resources/application.yml:

app:
send-email-on-errors: true
exit-on-errors: true
order-screen-properties:
title: Order Placement
item-label: Item
qty-label: Qunatity
customer-screen-properties:
title: Customer Profile
name-label: Customer Name
phone-label: Contact Number

public class OrderScreenProperties {
private String title;
private String itemLabel;
private String qtyLabel;
.............
}

public class CustomerScreenProperties {
private String title;
private String nameLabel;
private String phoneLabel;
.............
}

public class MainScreenProperties {
private int refreshRate;
private int width;
private int height;
.............
}
Original Post




import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Currency;
import java.util.concurrent.TimeUnit;

@Component
@ConfigurationProperties("app")
public class MyAppProperties {
private int refreshRate;
private TimeUnit refreshTimeUnit;
private Currency tradeCurrency;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.trade-currency=USD
app.refresh-time-unit=seconds
app.refresh-rate=5
Original Post




import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@ConfigurationProperties("app")
public class MyAppProperties {
private boolean exitOnErrors;
private LocalDate tradeStartDate;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.exit-on-errors=true
app.trade-start-date=03-25-2016

Original Post

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;

@Component
@ConfigurationProperties("app")
@Validated
public class MyAppProperties {
@Pattern(regexp = "\\d{3}-\\d{3}-\\d{4}")
private String adminContactNumber;
@Min(1)
private int refreshRate;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.admin-contact-number=111-111-111
app.refresh-rate=0

Original Post




import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Component
@ConfigurationProperties("app")
@Validated
public class MyAppProperties {
@NotNull
@Valid
private MainScreenProperties mainScreenProperties;

@NotNull
@Valid
private PhoneNumber adminContactNumber;
.............
}

src/main/resources/application.properties:

spring.main.banner-mode=off 
spring.main.logStartupInfo=false
app.main-screen-properties.refresh-rate=15
app.main-screen-properties.width=10
app.main-screen-properties.height=400
app.admin-contact-number.value=111-111-111
app.admin-contact-number.type=personal

import javax.validation.constraints.Pattern;

public class PhoneNumber {
@Pattern(regexp = "\\d{3}-\\d{3}-\\d{4}")
private String value;
@Pattern(regexp = "(?i)cell|house|work")
private String type;
.............
}

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

public class MainScreenProperties {
@Min(1)
private int refreshRate;
@Min(50)
@Max(1000)
private int width;
@Min(50)
@Max(600)
private int height;
.............
}
Original Post




See Also