看源码的时候,碰到这个注解@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;
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐