Search in sources :

Example 1 with NoUnboundElementsBindHandler

use of org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler 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)

Example 2 with NoUnboundElementsBindHandler

use of org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler in project dubbo by alibaba.

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 3 with NoUnboundElementsBindHandler

use of org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler in project cas by apereo.

the class CasConfigurationPropertiesValidator method validateConfiguration.

private void validateConfiguration(final Class clazz, final List<String> validationResults) {
    val beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext.getBeanFactory(), clazz);
    beans.values().forEach(bean -> {
        val configBean = ConfigurationPropertiesBean.get(this.applicationContext, bean, UUID.randomUUID().toString());
        val target = configBean.asBindTarget();
        val annotation = configBean.getAnnotation();
        val handler = new NoUnboundElementsBindHandler(new IgnoreTopLevelConverterNotFoundBindHandler(), new UnboundElementsSourceFilter());
        val configBinder = new Binder(ConfigurationPropertySources.from(applicationContext.getEnvironment().getPropertySources()), new PropertySourcesPlaceholdersResolver(applicationContext.getEnvironment().getPropertySources()), applicationContext.getEnvironment().getConversionService(), null, null, null);
        try {
            configBinder.bind(annotation.prefix(), target, handler);
        } catch (final BindException e) {
            var message = "\n".concat(e.getMessage()).concat("\n");
            if (e.getCause() != null) {
                val cause = (UnboundConfigurationPropertiesException) e.getCause();
                if (cause != null) {
                    message += cause.getUnboundProperties().stream().map(property -> String.format("%n\t%s = %s (Origin: %s)", property.getName(), property.getValue(), property.getOrigin())).collect(Collectors.joining("\n"));
                }
            }
            validationResults.add(message);
        }
    });
}
Also used : lombok.val(lombok.val) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) UnboundConfigurationPropertiesException(org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) UUID(java.util.UUID) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) Collectors(java.util.stream.Collectors) ConfigurationPropertiesBean(org.springframework.boot.context.properties.ConfigurationPropertiesBean) ArrayList(java.util.ArrayList) PropertySourcesPlaceholdersResolver(org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver) LoggingUtils(org.apereo.cas.util.LoggingUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CasVersion(org.apereo.cas.util.CasVersion) BindException(org.springframework.boot.context.properties.bind.BindException) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertySources(org.springframework.boot.context.properties.source.ConfigurationPropertySources) Binder(org.springframework.boot.context.properties.bind.Binder) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) BindException(org.springframework.boot.context.properties.bind.BindException) PropertySourcesPlaceholdersResolver(org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)

Example 4 with NoUnboundElementsBindHandler

use of org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler 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)

Aggregations

NoUnboundElementsBindHandler (org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler)4 UnboundElementsSourceFilter (org.springframework.boot.context.properties.source.UnboundElementsSourceFilter)4 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)3 IgnoreErrorsBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler)3 IgnoreTopLevelConverterNotFoundBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 lombok.val (lombok.val)1 CasVersion (org.apereo.cas.util.CasVersion)1 LoggingUtils (org.apereo.cas.util.LoggingUtils)1 BeanFactoryUtils (org.springframework.beans.factory.BeanFactoryUtils)1 ConfigurationPropertiesBean (org.springframework.boot.context.properties.ConfigurationPropertiesBean)1 AbstractBindHandler (org.springframework.boot.context.properties.bind.AbstractBindHandler)1 BindException (org.springframework.boot.context.properties.bind.BindException)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 BoundPropertiesTrackingBindHandler (org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler)1