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

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

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

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

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

@ -90,7 +90,7 @@ class PropertySourceIterableConfigurationPropertySource
@Override @Override
public Optional<Boolean> containsDescendantOf(ConfigurationPropertyName name) { 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() { private List<ConfigurationPropertyName> getConfigurationPropertyNames() {

Loading…
Cancel
Save