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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -32,6 +32,7 @@ import org.springframework.util.ClassUtils;
* enabled. * enabled.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb
* @see ConditionalOnEnabledResourceChain * @see ConditionalOnEnabledResourceChain
*/ */
class OnEnabledResourceChainCondition extends SpringBootCondition { class OnEnabledResourceChainCondition extends SpringBootCondition {
@ -43,10 +44,10 @@ class OnEnabledResourceChainCondition extends SpringBootCondition {
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
ConfigurableEnvironment environment = (ConfigurableEnvironment) context ConfigurableEnvironment environment = (ConfigurableEnvironment) context
.getEnvironment(); .getEnvironment();
ResourceProperties properties = new ResourceProperties(); boolean fixed = getEnabledProperty(environment, "strategy.fixed.", false);
RelaxedDataBinder binder = new RelaxedDataBinder(properties, "spring.resources"); boolean content = getEnabledProperty(environment, "strategy.content.", false);
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources())); Boolean chain = getEnabledProperty(environment, "", null);
Boolean match = properties.getChain().getEnabled(); Boolean match = ResourceProperties.Chain.getEnabled(fixed, content, chain);
ConditionMessage.Builder message = ConditionMessage ConditionMessage.Builder message = ConditionMessage
.forCondition(ConditionalOnEnabledResourceChain.class); .forCondition(ConditionalOnEnabledResourceChain.class);
if (match == null) { if (match == null) {
@ -63,4 +64,11 @@ class OnEnabledResourceChainCondition extends SpringBootCondition {
return ConditionOutcome.noMatch(message.because("disabled")); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * settings are present.
*/ */
public Boolean getEnabled() { public Boolean getEnabled() {
Boolean strategyEnabled = getStrategy().getFixed().isEnabled() return getEnabled(getStrategy().getFixed().isEnabled(),
|| getStrategy().getContent().isEnabled(); getStrategy().getContent().isEnabled(), this.enabled);
return (strategyEnabled ? Boolean.TRUE : this.enabled);
} }
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
@ -223,6 +222,11 @@ public class ResourceProperties implements ResourceLoaderAware {
this.gzipped = gzipped; this.gzipped = gzipped;
} }
static Boolean getEnabled(boolean fixedEnabled, boolean contentEnabled,
Boolean chainEnabled) {
return (fixedEnabled || contentEnabled ? Boolean.TRUE : chainEnabled);
}
} }
/** /**

Loading…
Cancel
Save