Optimize OnEnabledResourceChainCondition

Optimize OnEnabledResourceChainCondition by removing the DataBinder.
Properties are now read directly from the Environment.

See gh-7573
pull/8038/merge
Phillip Webb 8 years ago
parent f8ded6de63
commit ccf964eb9a

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,11 +19,11 @@ package org.springframework.boot.autoconfigure.web;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils;
@ -32,6 +32,7 @@ import org.springframework.util.ClassUtils;
* enabled.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @see ConditionalOnEnabledResourceChain
*/
class OnEnabledResourceChainCondition extends SpringBootCondition {
@ -43,10 +44,10 @@ class OnEnabledResourceChainCondition extends SpringBootCondition {
AnnotatedTypeMetadata metadata) {
ConfigurableEnvironment environment = (ConfigurableEnvironment) context
.getEnvironment();
ResourceProperties properties = new ResourceProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(properties, "spring.resources");
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
Boolean match = properties.getChain().getEnabled();
boolean fixed = getEnabledProperty(environment, "strategy.fixed.", false);
boolean content = getEnabledProperty(environment, "strategy.content.", false);
Boolean chain = getEnabledProperty(environment, "", null);
Boolean match = ResourceProperties.Chain.getEnabled(fixed, content, chain);
ConditionMessage.Builder message = ConditionMessage
.forCondition(ConditionalOnEnabledResourceChain.class);
if (match == null) {
@ -63,4 +64,11 @@ class OnEnabledResourceChainCondition extends SpringBootCondition {
return ConditionOutcome.noMatch(message.because("disabled"));
}
private Boolean getEnabledProperty(ConfigurableEnvironment environment, String key,
Boolean defaultValue) {
PropertyResolver resolver = new RelaxedPropertyResolver(environment,
"spring.resources.chain." + key);
return resolver.getProperty("enabled", Boolean.class, defaultValue);
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -186,9 +186,8 @@ public class ResourceProperties implements ResourceLoaderAware {
* settings are present.
*/
public Boolean getEnabled() {
Boolean strategyEnabled = getStrategy().getFixed().isEnabled()
|| getStrategy().getContent().isEnabled();
return (strategyEnabled ? Boolean.TRUE : this.enabled);
return getEnabled(getStrategy().getFixed().isEnabled(),
getStrategy().getContent().isEnabled(), this.enabled);
}
public void setEnabled(boolean enabled) {
@ -223,6 +222,11 @@ public class ResourceProperties implements ResourceLoaderAware {
this.gzipped = gzipped;
}
static Boolean getEnabled(boolean fixedEnabled, boolean contentEnabled,
Boolean chainEnabled) {
return (fixedEnabled || contentEnabled ? Boolean.TRUE : chainEnabled);
}
}
/**

Loading…
Cancel
Save