@ConfigurationProperties 注解使用

依赖和文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
##jwt配置
audience:
  # 代表这个JWT的接收对象,存入audience
  clientId: 098f6bcd4621d373cade4e832627b4f6
  # 密钥, 经过Base64加密, 可自行替换
  base64Secret: MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=
  # JWT的签发主体,存入issuer
  name: restapiuser
  # 过期时间,时间戳
  expiresSecond: 172800

测试

package com.mozq.sb.jwt01.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @description:
 * @author: [email protected]
 * @date: 2019/10/24 15:59
 */
@ConfigurationProperties("audience")
//@ConfigurationProperties(prefix = "audience", ignoreInvalidFields = true)
/*
@ConfigurationProperties 注解的bean必须先是spring中注册的bean。
如果不是则可以使用 @EnableConfigurationProperties(Audience.class) 来注册这个类。
或者使用@Component等组件将这个bean注册到spring中
@Import注解
*/
//@EnableConfigurationProperties(Audience.class)
@RestController
@Data
public class Audience {
    String clientId;
    String base64Secret;
    String name;
    String expiresSecond;
    String password;//没有对应属性的则会是null。
    /*
    Failed to bind properties under 'audience' to com.mozq.sb.jwt01.config.Audience:

    Property: audience.clientid
    Value: 098f6bcd4621d373cade4e832627b4f6
    Origin: class path resource [application.yml]:6:13
    Reason: No setter found for property: client-id
     */

    @RequestMapping("/clientId")
    public String clientId(){
        System.out.println(clientId);
        System.out.println(base64Secret);
        System.out.println(name);
        System.out.println(expiresSecond);
        return clientId;
        /* 运行结果:
        098f6bcd4621d373cade4e832627b4f6
        MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=
        restapiuser
        172800
         */
    }
}

@ConfigurationProperties

public @interface ConfigurationProperties {

    @AliasFor("prefix")
    String value() default "";
    /**
     * The prefix of the properties that are valid to bind to this object. Synonym for
     * {@link #value()}. A valid prefix is defined by one or more words separated with
     * dots (e.g. {@code "acme.system.feature"}).
     * @return the prefix of the properties to bind
     */
    @AliasFor("value")
    String prefix() default "";

    /**
     * Flag to indicate that when binding to this object invalid fields should be ignored.
     * Invalid means invalid according to the binder that is used, and usually this means
     * fields of the wrong type (or that cannot be coerced into the correct type).
     * @return the flag value (default false)
     * 是否忽略无效的字段,如果比如无法进行类型转换,则默认不忽略将报错。
     */
    boolean ignoreInvalidFields() default false;

    /**
     * Flag to indicate that when binding to this object unknown fields should be ignored.
     * An unknown field could be a sign of a mistake in the Properties.
     * @return the flag value (default true)
     */
    boolean ignoreUnknownFields() default true;

}

bugs

Failed to bind properties under 'audience.expires-second' to java.util.Date:

    Property: audience.expiressecond
    Value: 172800
    Origin: class path resource [application.yml]:12:18
    Reason: No converter found capable of converting from type [java.lang.Integer] to type [java.util.Date]
01-13 21:27