Spring Boot @ConfigurationProperties 配置文件映射为对象

Spring Boot About 3,113 words

需求

application.yaml配置文件中的字段映射为DurationPeriodDateLocalTimeLocalDateTimeEnum、自定义对象。

yaml 配置

config1:
  duration: 10h
  period: 10d
  dataSize: 10MB
  localTime: "12:54:37"
  localDateTime: 2022-10-26 12:54:37
  date: 2022-10-26 12:54:37
  user: 张三,18,this is info
  databaseType: REDIS

config2:
  duration: 10h
  period: 10d
  dataSize: 10MB
  localTime: "12:54:37"
  localDateTime: 2022-10-26 12:54:37
  date: 2022-10-26 12:54:37
  user: 张三,18,this is info
  databaseType: REDIS

@ConfigurationProperties 配置

配置一

@Data
@Configuration
@ConfigurationProperties(prefix = "config1")
public class Config1 {

    @DurationUnit(ChronoUnit.MINUTES)
    private Duration duration;

    @PeriodUnit(ChronoUnit.DAYS)
    private Period period;

    @DataSizeUnit(DataUnit.MEGABYTES)
    private DataSize dataSize;

    @DateTimeFormat(pattern = "HH:mm:ss")
    private LocalTime localTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime localDateTime;

    private Date date;

    private User user;

    private DatabaseType databaseType;

}

public enum DatabaseType {

    MYSQL,
    REDIS,
    MONGODB

}

配置二

@Data
@Configuration
@ConfigurationProperties(prefix = "config2")
public class Config2 {

    private Duration duration;

    private Period period;

    private DataSize dataSize;

    @DateTimeFormat(pattern = "HH:mm:ss")
    private LocalTime localTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime localDateTime;

    private Date date;

    private User user;

    private DatabaseType databaseType;

}

public enum DatabaseType {

    MYSQL,
    REDIS,
    MONGODB

}

区别

配置一中添加了@DurationUnit@PeriodUnit@DataSizeUnit配置。

Date 处理

对于Date类型,需要注入Converter

@Component
@ConfigurationPropertiesBinding
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

Duration 单位

备注:大小写皆可。

  • ns:纳秒,nanoseconds
  • us:微秒,microseconds
  • ms:毫秒,milliseconds
  • s:秒,seconds
  • m:分,minutes
  • h:时
  • d:天
config1:
  duration1: 10h
  duration2: 2d10h30m5s

Period 单位

备注:大小写皆可。

  • y:年,years
  • m:月,months
  • w:周,weeks

DataSize 单位

备注:必须大写。

  • Bbytes
  • KBkilobytes
  • MBmegabytes
  • GBgigabytes
  • TBterabytes

LocalTime & LocalDateTime

必须添加@DateTimeFormat,否则会报错。

Enum

枚举类型可以直接使用,无需注解。

自定义对象处理

@Component
@ConfigurationPropertiesBinding
public class UserConverter implements Converter<String, User> {

    @Override
    public User convert(String source) {
        System.out.println("UserConverter#" + source);
        String[] data = source.split(",");
        return new User(data[0], Integer.parseInt(data[1]), data[2]);
    }

}

@Data
@AllArgsConstructor
public class User {

    private String name;

    private int age;

    private String info;

}

参考

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.external-config.typesafe-configuration-properties.conversion.durations

Views: 1,211 · Posted: 2023-01-30

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh