Move spring.resources.* properties to spring.web.resources.*

Closes gh-23917
This commit is contained in:
Andy Wilkinson
2020-10-29 12:46:24 +00:00
parent 929d8fcd3c
commit c22e655848
23 changed files with 1168 additions and 519 deletions

View File

@@ -23,6 +23,8 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
import org.springframework.boot.devtools.restart.Restarter;
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
@@ -85,7 +87,9 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
if (canAddProperties(environment)) {
logger.info(LogMessage.format("Devtools property defaults active! Set '%s' to 'false' to disable",
ENABLED));
environment.getPropertySources().addLast(new MapPropertySource("devtools", PROPERTIES));
Map<String, Object> properties = new HashMap<>(PROPERTIES);
properties.putAll(getResourceProperties(environment));
environment.getPropertySources().addLast(new MapPropertySource("devtools", properties));
}
if (isWebApplication(environment) && !environment.containsProperty(WEB_LOGGING)) {
logger.info(LogMessage.format(
@@ -95,6 +99,28 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
}
}
private Map<String, String> getResourceProperties(Environment environment) {
Map<String, String> resourceProperties = new HashMap<>();
String prefix = determineResourcePropertiesPrefix(environment);
resourceProperties.put(prefix + "cache.period", "0");
resourceProperties.put(prefix + "chain.cache", "false");
System.out.println(resourceProperties);
return resourceProperties;
}
@SuppressWarnings("deprecation")
private String determineResourcePropertiesPrefix(Environment environment) {
if (ClassUtils.isPresent("org.springframework.boot.autoconfigure.web.ResourceProperties",
getClass().getClassLoader())) {
BindResult<org.springframework.boot.autoconfigure.web.ResourceProperties> result = Binder.get(environment)
.bind("spring.resources", org.springframework.boot.autoconfigure.web.ResourceProperties.class);
if (result.isBound() && result.get().hasBeenCustomized()) {
return "spring.resources.";
}
}
return "spring.web.resources.";
}
private boolean isLocalApplication(ConfigurableEnvironment environment) {
return environment.getPropertySources().get("remoteUrl") == null;
}

View File

@@ -35,7 +35,8 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.devtools.classpath.ClassPathChangedEvent;
import org.springframework.boot.devtools.classpath.ClassPathFileSystemWatcher;
@@ -113,7 +114,17 @@ class LocalDevToolsAutoConfigurationTests {
@Test
void resourceCachePeriodIsZero() throws Exception {
this.context = getContext(() -> initializeAndRun(WebResourcesConfig.class));
ResourceProperties properties = this.context.getBean(ResourceProperties.class);
Resources properties = this.context.getBean(WebProperties.class).getResources();
assertThat(properties.getCache().getPeriod()).isZero();
}
@SuppressWarnings("deprecation")
@Test
void deprecatedResourceCachePeriodIsZeroWhenDeprecatedResourcePropertiesAreInUse() throws Exception {
this.context = getContext(() -> initializeAndRun(WebResourcesConfig.class,
Collections.singletonMap("spring.resources.add-mappings", false)));
Resources properties = this.context
.getBean(org.springframework.boot.autoconfigure.web.ResourceProperties.class);
assertThat(properties.getCache().getPeriod()).isZero();
}
@@ -282,9 +293,10 @@ class LocalDevToolsAutoConfigurationTests {
}
@SuppressWarnings("deprecation")
@Configuration(proxyBeanMethods = false)
@Import({ ServletWebServerFactoryAutoConfiguration.class, LocalDevToolsAutoConfiguration.class,
ResourceProperties.class })
@Import({ ServletWebServerFactoryAutoConfiguration.class, LocalDevToolsAutoConfiguration.class, WebProperties.class,
org.springframework.boot.autoconfigure.web.ResourceProperties.class })
static class WebResourcesConfig {
}