欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单分析@NestedConfigurationProperty的作用

程序员文章站 2022-06-07 22:31:16
...

看源码的时候,碰到这个注解@NestedConfigurationProperty,之前知道@ConfigurationProperty是设置值的,但是nested就不清楚了。
分析如下
查看源码

/**
 * Indicates that a field in a {@link ConfigurationProperties @ConfigurationProperties}
 * object should be treated as if it were a nested type. This annotation has no bearing on
 * the actual binding processes, but it is used by the
 * {@code spring-boot-configuration-processor} as a hint that a field is not bound as a
 * single value. When this is specified, a nested group is created for the field and its
 * type is harvested.
 */

意思就是一个嵌套类型,使用它了之后就不是一个简单单值了,一般是比较复杂的类型。另外,使用了这个注解的类,代表是不在本文件中,而是在其他地方。

@ConfigurationProperties
public class Outer {
    private Inner qsm1;
    private Inner qsm2;
    // getter, setter ...
    public static class Inner {
        private String name;
        // getter, setter ...
    }
}

若Inner类是一个单独的文件,不在Outer 中,那么使用@NestedConfigurationProperty

@ConfigurationProperties
public class Outer {
    @NestedConfigurationProperty
    private Inner qsm1; 
    @NestedConfigurationProperty
    private Inner qsm2;
}