Search in sources :

Example 1 with BindHandler

use of org.springframework.boot.context.properties.bind.BindHandler in project incubator-dubbo-spring-boot-project by apache.

the class BinderDubboConfigBinder method getBindHandler.

private BindHandler getBindHandler(boolean ignoreUnknownFields, boolean ignoreInvalidFields) {
    BindHandler handler = BindHandler.DEFAULT;
    if (ignoreInvalidFields) {
        handler = new IgnoreErrorsBindHandler(handler);
    }
    if (!ignoreUnknownFields) {
        UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
        handler = new NoUnboundElementsBindHandler(handler, filter);
    }
    return handler;
}
Also used : IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) BindHandler(org.springframework.boot.context.properties.bind.BindHandler)

Example 2 with BindHandler

use of org.springframework.boot.context.properties.bind.BindHandler in project apollo by ctripcorp.

the class ApolloConfigDataLoader method load.

@Override
public ConfigData load(ConfigDataLoaderContext context, ApolloConfigDataResource resource) throws IOException, ConfigDataResourceNotFoundException {
    ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext();
    Binder binder = bootstrapContext.get(Binder.class);
    BindHandler bindHandler = this.getBindHandler(context);
    bootstrapContext.registerIfAbsent(ApolloConfigDataLoaderInitializer.class, InstanceSupplier.from(() -> new ApolloConfigDataLoaderInitializer(this.log, binder, bindHandler, bootstrapContext)));
    ApolloConfigDataLoaderInitializer apolloConfigDataLoaderInitializer = bootstrapContext.get(ApolloConfigDataLoaderInitializer.class);
    // init apollo client
    List<PropertySource<?>> initialPropertySourceList = apolloConfigDataLoaderInitializer.initApolloClient();
    // load config
    bootstrapContext.registerIfAbsent(ConfigPropertySourceFactory.class, InstanceSupplier.from(() -> SpringInjector.getInstance(ConfigPropertySourceFactory.class)));
    ConfigPropertySourceFactory configPropertySourceFactory = bootstrapContext.get(ConfigPropertySourceFactory.class);
    String namespace = resource.getNamespace();
    Config config = ConfigService.getConfig(namespace);
    ConfigPropertySource configPropertySource = configPropertySourceFactory.getConfigPropertySource(namespace, config);
    List<PropertySource<?>> propertySourceList = new ArrayList<>();
    propertySourceList.add(configPropertySource);
    propertySourceList.addAll(initialPropertySourceList);
    log.debug(Slf4jLogMessageFormatter.format("apollo client loaded namespace [{}]", namespace));
    return new ConfigData(propertySourceList);
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) Config(com.ctrip.framework.apollo.Config) ConfigPropertySource(com.ctrip.framework.apollo.spring.config.ConfigPropertySource) ConfigData(org.springframework.boot.context.config.ConfigData) ArrayList(java.util.ArrayList) ConfigPropertySourceFactory(com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) ConfigurableBootstrapContext(org.springframework.boot.ConfigurableBootstrapContext) PropertySource(org.springframework.core.env.PropertySource) ConfigPropertySource(com.ctrip.framework.apollo.spring.config.ConfigPropertySource)

Example 3 with BindHandler

use of org.springframework.boot.context.properties.bind.BindHandler in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenImportingIncludesParentOrigin.

@Test
void runWhenImportingIncludesParentOrigin() {
    ConfigurableApplicationContext context = this.application.run("--spring.config.location=classpath:application-import-with-placeholder.properties");
    Binder binder = Binder.get(context.getEnvironment());
    List<ConfigurationProperty> properties = new ArrayList<>();
    BindHandler bindHandler = new BindHandler() {

        @Override
        public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
            properties.add(context.getConfigurationProperty());
            return result;
        }
    };
    binder.bind("my.value", Bindable.of(String.class), bindHandler);
    assertThat(properties).hasSize(1);
    Origin origin = properties.get(0).getOrigin();
    assertThat(origin.toString()).contains("application-import-with-placeholder-imported");
    assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) BindContext(org.springframework.boot.context.properties.bind.BindContext) Origin(org.springframework.boot.origin.Origin) Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ArrayList(java.util.ArrayList) Bindable(org.springframework.boot.context.properties.bind.Bindable) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) Test(org.junit.jupiter.api.Test)

Example 4 with BindHandler

use of org.springframework.boot.context.properties.bind.BindHandler in project spring-boot by spring-projects.

the class ConfigurationPropertiesBinder method bind.

BindResult<?> bind(ConfigurationPropertiesBean propertiesBean) {
    Bindable<?> target = propertiesBean.asBindTarget();
    ConfigurationProperties annotation = propertiesBean.getAnnotation();
    BindHandler bindHandler = getBindHandler(target, annotation);
    return getBinder().bind(annotation.prefix(), target, bindHandler);
}
Also used : IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) BoundPropertiesTrackingBindHandler(org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) AbstractBindHandler(org.springframework.boot.context.properties.bind.AbstractBindHandler)

Example 5 with BindHandler

use of org.springframework.boot.context.properties.bind.BindHandler in project spring-boot by spring-projects.

the class ConfigurationPropertiesBinder method getBindHandler.

private <T> BindHandler getBindHandler(Bindable<T> target, ConfigurationProperties annotation) {
    List<Validator> validators = getValidators(target);
    BindHandler handler = getHandler();
    handler = new ConfigurationPropertiesBindHandler(handler);
    if (annotation.ignoreInvalidFields()) {
        handler = new IgnoreErrorsBindHandler(handler);
    }
    if (!annotation.ignoreUnknownFields()) {
        UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
        handler = new NoUnboundElementsBindHandler(handler, filter);
    }
    if (!validators.isEmpty()) {
        handler = new ValidationBindHandler(handler, validators.toArray(new Validator[0]));
    }
    for (ConfigurationPropertiesBindHandlerAdvisor advisor : getBindHandlerAdvisors()) {
        handler = advisor.apply(handler);
    }
    return handler;
}
Also used : IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) BoundPropertiesTrackingBindHandler(org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) AbstractBindHandler(org.springframework.boot.context.properties.bind.AbstractBindHandler) Validator(org.springframework.validation.Validator) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)

Aggregations

BindHandler (org.springframework.boot.context.properties.bind.BindHandler)10 IgnoreErrorsBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler)7 NoUnboundElementsBindHandler (org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler)7 Binder (org.springframework.boot.context.properties.bind.Binder)5 Bindable (org.springframework.boot.context.properties.bind.Bindable)4 ArrayList (java.util.ArrayList)3 AbstractBindHandler (org.springframework.boot.context.properties.bind.AbstractBindHandler)3 BoundPropertiesTrackingBindHandler (org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler)3 IgnoreTopLevelConverterNotFoundBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)3 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)3 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)3 UnboundElementsSourceFilter (org.springframework.boot.context.properties.source.UnboundElementsSourceFilter)3 PropertySource (org.springframework.core.env.PropertySource)3 ConfigurationBeanBinder (com.alibaba.spring.context.config.ConfigurationBeanBinder)2 DubboConfigBinder (org.apache.dubbo.config.spring.context.properties.DubboConfigBinder)2 ConfigurableBootstrapContext (org.springframework.boot.ConfigurableBootstrapContext)2 BindContext (org.springframework.boot.context.properties.bind.BindContext)2 PropertySourcesPlaceholdersResolver (org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver)2 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2