From ccf964eb9a9e1b6dafe02853d2097eacd553e47c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 22 Jan 2017 18:45:37 -0800 Subject: [PATCH] Optimize OnEnabledResourceChainCondition Optimize OnEnabledResourceChainCondition by removing the DataBinder. Properties are now read directly from the Environment. See gh-7573 --- .../web/OnEnabledResourceChainCondition.java | 22 +++++++++++++------ .../autoconfigure/web/ResourceProperties.java | 12 ++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java index 04ab0800d83..52e702c5b6b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java @@ -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); + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java index 943edc202dd..71080b136b4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java @@ -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); + } + } /**