Polish configuration properties and binder

Fix a few issues and try a few more things to improve performance.

See gh-9000
pull/9007/merge
Phillip Webb 8 years ago
parent 97dc2165b7
commit cd27737e42

@ -291,18 +291,21 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
ConfigurationProperties annotation = AnnotationUtils
.findAnnotation(bean.getClass(), ConfigurationProperties.class);
Object bound = bean;
ConfigurationProperties annotation = getAnnotation(bean, beanName);
if (annotation != null) {
bound = postProcessBeforeInitialization(bean, beanName, annotation);
postProcessBeforeInitialization(bean, beanName, annotation);
}
annotation = this.beans.findFactoryAnnotation(beanName,
return bean;
}
private ConfigurationProperties getAnnotation(Object bean, String beanName) {
ConfigurationProperties annotation = this.beans.findFactoryAnnotation(beanName,
ConfigurationProperties.class);
if (annotation != null) {
bound = postProcessBeforeInitialization(bean, beanName, annotation);
if (annotation == null) {
annotation = AnnotationUtils.findAnnotation(bean.getClass(),
ConfigurationProperties.class);
}
return bound;
return annotation;
}
@Override
@ -311,7 +314,7 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
return bean;
}
private Object postProcessBeforeInitialization(Object bean, String beanName,
private void postProcessBeforeInitialization(Object bean, String beanName,
ConfigurationProperties annotation) {
Binder binder = getBinder();
Validator validator = determineValidator(bean);
@ -319,7 +322,6 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
Bindable<?> bindable = Bindable.ofInstance(bean);
try {
binder.bind(annotation.prefix(), bindable, handler);
return bean;
}
catch (Exception ex) {
String targetClass = ClassUtils.getShortName(bean.getClass());

@ -235,11 +235,14 @@ public class Binder {
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target,
BindHandler handler, Context context) throws Exception {
ConfigurationProperty property = findProperty(name, context);
if (property == null && containsNoDescendantOf(context.streamSources(), name)) {
return null;
}
AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
if (aggregateBinder != null) {
return bindAggregate(name, target, handler, context, aggregateBinder);
}
ConfigurationProperty property = findProperty(name, context);
if (property != null) {
return bindProperty(name, target, handler, context, property);
}

@ -21,7 +21,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@ -74,7 +73,7 @@ public class ConfigurationPropertySources
public Iterator<ConfigurationPropertySource> iterator() {
return streamPropertySources(this.propertySources)
.filter(s -> !(s instanceof ConfigurationPropertySourcesPropertySource))
.map(this::adapt).collect(Collectors.toList()).iterator();
.map(this::adapt).iterator();
}
private Stream<PropertySource<?>> streamPropertySources(PropertySources sources) {

@ -52,6 +52,7 @@ class DefaultPropertyMapper implements PropertyMapper {
String convertedName = configurationPropertyName.toString();
mapping = Collections.singletonList(
new PropertyMapping(convertedName, configurationPropertyName));
this.configurationPropertySourceCache.put(configurationPropertyName, mapping);
}
return mapping;
}

@ -49,7 +49,7 @@ class FilteredIterableConfigurationPropertiesSource
@Override
public Optional<Boolean> containsDescendantOf(ConfigurationPropertyName name) {
return Optional.of(stream().filter(name::isAncestorOf).findFirst().isPresent());
return Optional.of(stream().anyMatch(name::isAncestorOf));
}
}

@ -63,7 +63,7 @@ public interface IterableConfigurationPropertySource
@Override
default Optional<Boolean> containsDescendantOf(ConfigurationPropertyName name) {
return Optional.of(stream().filter(name::isAncestorOf).findFirst().isPresent());
return Optional.of(stream().anyMatch(name::isAncestorOf));
}
@Override

@ -90,7 +90,7 @@ class PropertySourceIterableConfigurationPropertySource
@Override
public Optional<Boolean> containsDescendantOf(ConfigurationPropertyName name) {
return Optional.of(stream().filter(name::isAncestorOf).findFirst().isPresent());
return Optional.of(stream().anyMatch(name::isAncestorOf));
}
private List<ConfigurationPropertyName> getConfigurationPropertyNames() {

Loading…
Cancel
Save