Java Code Examples for org.springframework.boot.context.properties.source.ConfigurationPropertyName#of()

The following examples show how to use org.springframework.boot.context.properties.source.ConfigurationPropertyName#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinderBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 7 votes vote down vote up
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();
}
 
Example 2
Source File: ConfigurationPropertyNameHierarchyBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 6 votes vote down vote up
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)
        );
    }
}
 
Example 3
Source File: ConfigurationPropertySourcesBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: ConfigurationPropertyNameFormBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 5
Source File: ConfigurationPropertyNameEqualityBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
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");
    }