Search in sources :

Example 16 with ConfigurationPropertySource

use of org.springframework.boot.context.properties.source.ConfigurationPropertySource in project spring-boot by spring-projects.

the class ConfigDataEnvironmentContributors method getBinder.

private Binder getBinder(ConfigDataActivationContext activationContext, Predicate<ConfigDataEnvironmentContributor> filter, Set<BinderOption> options) {
    boolean failOnInactiveSource = options.contains(BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
    Iterable<ConfigurationPropertySource> sources = () -> getBinderSources(filter.and((contributor) -> failOnInactiveSource || contributor.isActive(activationContext)));
    PlaceholdersResolver placeholdersResolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(this.root, activationContext, null, failOnInactiveSource);
    BindHandler bindHandler = !failOnInactiveSource ? null : new InactiveSourceChecker(activationContext);
    return new Binder(sources, placeholdersResolver, null, null, bindHandler);
}
Also used : Kind(org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) BindContext(org.springframework.boot.context.properties.bind.BindContext) Bindable(org.springframework.boot.context.properties.bind.Bindable) Arrays(java.util.Arrays) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) ConfigurableBootstrapContext(org.springframework.boot.ConfigurableBootstrapContext) ObjectUtils(org.springframework.util.ObjectUtils) Set(java.util.Set) Collectors(java.util.stream.Collectors) ImportPhase(org.springframework.boot.context.config.ConfigDataEnvironmentContributor.ImportPhase) ArrayList(java.util.ArrayList) LogMessage(org.springframework.core.log.LogMessage) List(java.util.List) PlaceholdersResolver(org.springframework.boot.context.properties.bind.PlaceholdersResolver) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) Map(java.util.Map) Log(org.apache.commons.logging.Log) Binder(org.springframework.boot.context.properties.bind.Binder) DeferredLogFactory(org.springframework.boot.logging.DeferredLogFactory) Collections(java.util.Collections) EnumSet(java.util.EnumSet) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) Binder(org.springframework.boot.context.properties.bind.Binder) PlaceholdersResolver(org.springframework.boot.context.properties.bind.PlaceholdersResolver) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) BindHandler(org.springframework.boot.context.properties.bind.BindHandler)

Example 17 with ConfigurationPropertySource

use of org.springframework.boot.context.properties.source.ConfigurationPropertySource in project spring-boot by spring-projects.

the class InactiveConfigDataAccessException method throwIfPropertyFound.

/**
 * Throw an {@link InactiveConfigDataAccessException} if the given
 * {@link ConfigDataEnvironmentContributor} contains the property.
 * @param contributor the contributor to check
 * @param name the name to check
 */
static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor, ConfigurationPropertyName name) {
    ConfigurationPropertySource source = contributor.getConfigurationPropertySource();
    ConfigurationProperty property = (source != null) ? source.getConfigurationProperty(name) : null;
    if (property != null) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        ConfigDataResource location = contributor.getResource();
        throw new InactiveConfigDataAccessException(propertySource, location, name.toString(), property.getOrigin());
    }
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource)

Example 18 with ConfigurationPropertySource

use of org.springframework.boot.context.properties.source.ConfigurationPropertySource in project spring-boot by spring-projects.

the class WebMvcPropertiesTests method bind.

private void bind(Map<String, String> map) {
    ConfigurationPropertySource source = new MapConfigurationPropertySource(map);
    new Binder(source).bind("spring.mvc", Bindable.ofInstance(this.properties));
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource)

Example 19 with ConfigurationPropertySource

use of org.springframework.boot.context.properties.source.ConfigurationPropertySource in project spring-boot by spring-projects.

the class BinderTests method bindWithEmptyPrefixShouldIgnorePropertiesWithEmptyName.

@Test
void bindWithEmptyPrefixShouldIgnorePropertiesWithEmptyName() {
    Map<String, Object> source = new HashMap<>();
    source.put("value", "hello");
    source.put("", "bar");
    Iterable<ConfigurationPropertySource> propertySources = ConfigurationPropertySources.from(new MapPropertySource("test", source));
    propertySources.forEach(this.sources::add);
    Bindable<JavaBean> target = Bindable.of(JavaBean.class);
    JavaBean result = this.binder.bind("", target).get();
    assertThat(result.getValue()).isEqualTo("hello");
}
Also used : HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 20 with ConfigurationPropertySource

use of org.springframework.boot.context.properties.source.ConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinder method bindAggregate.

@Override
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target, AggregateElementBinder elementBinder) {
    Map<Object, Object> map = CollectionFactory.createMap((target.getValue() != null) ? Map.class : target.getType().resolve(Object.class), 0);
    Bindable<?> resolvedTarget = resolveTarget(target);
    boolean hasDescendants = hasDescendants(name);
    for (ConfigurationPropertySource source : getContext().getSources()) {
        if (!ConfigurationPropertyName.EMPTY.equals(name)) {
            ConfigurationProperty property = source.getConfigurationProperty(name);
            if (property != null && !hasDescendants) {
                return getContext().getConverter().convert(property.getValue(), target);
            }
            source = source.filter(name::isAncestorOf);
        }
        new EntryBinder(name, resolvedTarget, elementBinder).bindEntries(source, map);
    }
    return map.isEmpty() ? null : map;
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) IterableConfigurationPropertySource(org.springframework.boot.context.properties.source.IterableConfigurationPropertySource) Map(java.util.Map)

Aggregations

ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)24 Binder (org.springframework.boot.context.properties.bind.Binder)17 MapConfigurationPropertySource (org.springframework.boot.context.properties.source.MapConfigurationPropertySource)11 Test (org.junit.jupiter.api.Test)6 ArrayList (java.util.ArrayList)4 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)4 Bindable (org.springframework.boot.context.properties.bind.Bindable)4 HashMap (java.util.HashMap)3 PlaceholdersResolver (org.springframework.boot.context.properties.bind.PlaceholdersResolver)3 MapPropertySource (org.springframework.core.env.MapPropertySource)3 PropertySource (org.springframework.core.env.PropertySource)3 ConfigurationBeanBinder (com.alibaba.spring.context.config.ConfigurationBeanBinder)2 List (java.util.List)2 Map (java.util.Map)2 DubboConfigBinder (org.apache.dubbo.config.spring.context.properties.DubboConfigBinder)2 Test (org.junit.Test)2 PropertySourcesPlaceholdersResolver (org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver)2 IgnoreErrorsBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler)2 NoUnboundElementsBindHandler (org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler)2 ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)2