Search in sources :

Example 1 with PropertiesConfigurationFactory

use of org.springframework.boot.bind.PropertiesConfigurationFactory in project spring-boot by spring-projects.

the class SpringApplication method bindToSpringApplication.

/**
	 * Bind the environment to the {@link SpringApplication}.
	 * @param environment the environment to bind
	 */
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
    PropertiesConfigurationFactory<SpringApplication> binder = new PropertiesConfigurationFactory<>(this);
    ConversionService conversionService = new DefaultConversionService();
    binder.setTargetName("spring.main");
    binder.setConversionService(conversionService);
    binder.setPropertySources(environment.getPropertySources());
    try {
        binder.bindPropertiesToTarget();
    } catch (BindException ex) {
        throw new IllegalStateException("Cannot bind to SpringApplication", ex);
    }
}
Also used : PropertiesConfigurationFactory(org.springframework.boot.bind.PropertiesConfigurationFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConversionService(org.springframework.core.convert.ConversionService) BindException(org.springframework.validation.BindException) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

Example 2 with PropertiesConfigurationFactory

use of org.springframework.boot.bind.PropertiesConfigurationFactory in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method postProcessBeforeInitialization.

private void postProcessBeforeInitialization(Object bean, String beanName, ConfigurationProperties annotation) {
    Object target = bean;
    PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(target);
    factory.setPropertySources(this.propertySources);
    factory.setValidator(determineValidator(bean));
    // If no explicit conversion service is provided we add one so that (at least)
    // comma-separated arrays of convertibles can be bound automatically
    factory.setConversionService(this.conversionService == null ? getDefaultConversionService() : this.conversionService);
    if (annotation != null) {
        factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
        factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
        factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
        if (StringUtils.hasLength(annotation.prefix())) {
            factory.setTargetName(annotation.prefix());
        }
    }
    try {
        factory.bindPropertiesToTarget();
    } catch (Exception ex) {
        String targetClass = ClassUtils.getShortName(target.getClass());
        throw new BeanCreationException(beanName, "Could not bind properties to " + targetClass + " (" + getAnnotationDetails(annotation) + ")", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertiesConfigurationFactory(org.springframework.boot.bind.PropertiesConfigurationFactory) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Aggregations

PropertiesConfigurationFactory (org.springframework.boot.bind.PropertiesConfigurationFactory)2 BeansException (org.springframework.beans.BeansException)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ConversionService (org.springframework.core.convert.ConversionService)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1 BindException (org.springframework.validation.BindException)1