org.springframework.boot.context.properties.source.ConfigurationPropertyName#of ( )源码实例Demo

下面列出了org.springframework.boot.context.properties.source.ConfigurationPropertyName#of ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

public static void main(String[] args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(BinderBootstrap.class)
            .web(WebApplicationType.NONE) // 非 Web 应用
            .properties("user.city.postCode=0731")
            .run(args);
    ConfigurableEnvironment environment = context.getEnvironment();
    // 从 Environment 中获取 ConfigurationPropertySource 集合
    Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
    // 构造 Binder 对象,并使用 ConfigurationPropertySource 集合作为配置源
    Binder binder = new Binder(sources);
    // 构造 ConfigurationPropertyName(Spring Boot 2.0 API)
    ConfigurationPropertyName propertyName = ConfigurationPropertyName.of("user.city.post-code");
    // 构造 Bindable 对象,包装 postCode
    Bindable<String> postCodeBindable = Bindable.of(String.class);
    BindResult<String> result = binder.bind(propertyName, postCodeBindable);
    String postCode = result.get();
    System.out.println("postCode = " + postCode);
    // 关闭上下文
    context.close();
}
 
public static void main(String[] args) {
    // 配置属性名抽象:ConfigurationPropertyName
    ConfigurationPropertyName configurationPropertyName = ConfigurationPropertyName.of("a.b.c");
    // 获取 "." 分割后的元素数量
    int numberOfElements = configurationPropertyName.getNumberOfElements();
    for (int i = 0; i < numberOfElements; i++) {
        // 获取劈断大小
        int size = i + 1;
        ConfigurationPropertyName chopped = configurationPropertyName.chop(size);
        // 输出
        System.out.printf("劈断[大小 : %d]的配置属性名['%s'] 是否为配置属性名['%s'] 的父属性名 : %b , 祖属性名 : %b \n",
                size, chopped, configurationPropertyName, chopped.isParentOf(configurationPropertyName),
                chopped.isAncestorOf(configurationPropertyName)
        );
    }
}
 
public static void main(String[] args) {
    ConfigurableApplicationContext context =
            new SpringApplicationBuilder(
                    ConfigurationPropertySourcesBootstrap.class)
                    .properties("user.city.postCode=0571")
                    .web(WebApplicationType.NONE) // 非 Web 应用
                    .run(args);
    ConfigurableEnvironment environment = context.getEnvironment();
    // 从 Environment 中获取 ConfigurationPropertySource 集合
    Iterable<ConfigurationPropertySource> configurationPropertySources = ConfigurationPropertySources.get(environment);
    // 构造 ConfigurationPropertyName(Spring Boot 2.0 API)
    ConfigurationPropertyName propertyName = ConfigurationPropertyName.of("user.city.post-code");
    // 迭代 ConfigurationPropertySource
    configurationPropertySources.forEach(configurationPropertySource -> {
        // 通过 ConfigurationPropertyName 获取 ConfigurationProperty
        ConfigurationProperty configurationProperty = configurationPropertySource.getConfigurationProperty(propertyName);
        if (configurationProperty != null) {
            String postCode = (String) configurationProperty.getValue();
            System.out.println("postCode = " + postCode + " , from : " + configurationProperty.getOrigin());
        }
    });
    // 关闭上下文
    context.close();
}
 
private static void displayElementForm(String propertyName) {
    // 配置属性名抽象:ConfigurationPropertyName
    ConfigurationPropertyName configurationPropertyName = ConfigurationPropertyName.of(propertyName);
    // 获取 "." 分割后的元素数量
    int numberOfElements = configurationPropertyName.getNumberOfElements();
    for (int i = 0; i < numberOfElements; i++) {
        // 原始格式
        String originalElement = configurationPropertyName.getElement(i, ConfigurationPropertyName.Form.ORIGINAL);
        // 统一格式
        String uniformElement = configurationPropertyName.getElement(i, ConfigurationPropertyName.Form.UNIFORM);
        // 输出
        System.out.printf("配置属性名['%s']的元素[%d] 原始格式 : '%s' , 统一格式 : '%s' \n",
                configurationPropertyName, i, originalElement, uniformElement);
    }
}
 
public static void main(String[] args) {
//        ConfigurationPropertyName one = ConfigurationPropertyName.of("user.home-page");
//        ConfigurationPropertyName aonther = ConfigurationPropertyName.of("user.homepage");
//        System.out.printf("配置属性名['%s'] 与 配置属性名['%s'] 相等 : %b", one, aonther, one.equals(aonther));

        // 错误配置属性名,仅允许小写字母 "a-z"、"0-9" 以及 "-"(横划线)
        ConfigurationPropertyName.of("user.HOMEP-AGE");
    }