@ConfigurationProperties注解对数据的自动封装

@ConfigurationProperties注解对数据的自动封装

@ConfigurationProperties可以对基本数据类型实现自动封装,可以封装格式为yyyy/MM/dd的日期

测试代码:

package aat;

import java.util.Date;

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

import lombok.Data;

/**
 * 使用@ConrigurationProperties注解封装配置文件中的数据
 */
@Component
@Data
@ConfigurationProperties(prefix="author")
public class TestProperties {

    private String name;
    private Integer age;
    private String phone;
    private Boolean isboy;
    private Float ppt;
    
    private Date birthday;
    private Date birthday1;
    private Date birthday2;
    
}

配置文件信息:

author:
  name: xixihaha
  age: 1112
  phone: 1234556666
  isboy: true
  ppt: 1.3
#  birthday: 2018-1-1
#  birthday1: 20180101
#  birthday2: 2018/1/1

如果封装日期类型,需要格式为 yyyy/MM/dd,可以自动封装,如果为yyyy-MM-dd或者为yyyyMMdd都会报错:

Description:

Failed to bind properties under 'author.birthday' to java.util.Date:

    Property: author.birthday
    Value: 2018-11-11
    Origin: class path resource [application.yml]:12:13
    Reason: failed to convert java.lang.String to java.util.Date

Action:

Update your application's configuration
上一篇:如何使用阿里云ECS服务器建网站----新人超详细教程


下一篇:用Random类输出验证码