mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Use available Converters for @ConfigurationProperties binding
This commit is contained in:
parent
002c4e0ff4
commit
e1bac589fb
@ -18,11 +18,15 @@ package org.springframework.bootstrap.context.annotation;
|
|||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
|
import org.springframework.beans.factory.ListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
import org.springframework.bootstrap.bind.PropertiesConfigurationFactory;
|
import org.springframework.bootstrap.bind.PropertiesConfigurationFactory;
|
||||||
import org.springframework.bootstrap.context.annotation.EnableConfigurationPropertiesImportSelector.ConfigurationPropertiesHolder;
|
import org.springframework.bootstrap.context.annotation.EnableConfigurationPropertiesImportSelector.ConfigurationPropertiesHolder;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
import org.springframework.core.convert.support.DefaultConversionService;
|
||||||
import org.springframework.core.env.PropertySources;
|
import org.springframework.core.env.PropertySources;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
@ -33,7 +37,8 @@ import org.springframework.validation.Validator;
|
|||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
public class PropertySourcesBindingPostProcessor implements BeanPostProcessor {
|
public class PropertySourcesBindingPostProcessor implements BeanPostProcessor,
|
||||||
|
BeanFactoryAware {
|
||||||
|
|
||||||
private PropertySources propertySources;
|
private PropertySources propertySources;
|
||||||
|
|
||||||
@ -43,6 +48,10 @@ public class PropertySourcesBindingPostProcessor implements BeanPostProcessor {
|
|||||||
|
|
||||||
private DefaultConversionService defaultConversionService = new DefaultConversionService();
|
private DefaultConversionService defaultConversionService = new DefaultConversionService();
|
||||||
|
|
||||||
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
|
private boolean initialized = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param propertySources
|
* @param propertySources
|
||||||
*/
|
*/
|
||||||
@ -64,6 +73,11 @@ public class PropertySourcesBindingPostProcessor implements BeanPostProcessor {
|
|||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||||
throws BeansException {
|
throws BeansException {
|
||||||
@ -86,7 +100,7 @@ public class PropertySourcesBindingPostProcessor implements BeanPostProcessor {
|
|||||||
factory.setValidator(this.validator);
|
factory.setValidator(this.validator);
|
||||||
// If no explicit conversion service is provided we add one so that (at least)
|
// If no explicit conversion service is provided we add one so that (at least)
|
||||||
// comma-separated arrays of convertibles can be bound automatically
|
// comma-separated arrays of convertibles can be bound automatically
|
||||||
factory.setConversionService(this.conversionService == null ? this.defaultConversionService
|
factory.setConversionService(this.conversionService == null ? getDefaultConversionService()
|
||||||
: this.conversionService);
|
: this.conversionService);
|
||||||
String targetName = null;
|
String targetName = null;
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
@ -108,4 +122,17 @@ public class PropertySourcesBindingPostProcessor implements BeanPostProcessor {
|
|||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private ConversionService getDefaultConversionService() {
|
||||||
|
if (!this.initialized && this.beanFactory instanceof ListableBeanFactory) {
|
||||||
|
for (Converter<?, ?> converter : ((ListableBeanFactory) this.beanFactory)
|
||||||
|
.getBeansOfType(Converter.class).values()) {
|
||||||
|
this.defaultConversionService.addConverter(converter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.defaultConversionService;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user