Support path discovery for main dispatcher servlet

Add an `DispatcherServletPath` interface which provides a much more
consistent way to discover the path of the main dispatcher servet.

Prior to this commit, auto-configurations would often make use of the
`ServerProperties` class to discover the dispatcher servlet path. This
mechanism isn't very explicit and also makes it hard for us to relocate
that property in Spring Boot 2.1.

This commit also reverts most of fddc9e9c7e since it is now clear that
the supporting multiple dispatcher servlet paths will be much more
involved that we originally anticipated.

Closes gh-13834
This commit is contained in:
Phillip Webb
2018-07-24 16:35:37 +01:00
parent d37e717500
commit 9a9111af21
22 changed files with 501 additions and 260 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -22,6 +22,8 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -48,19 +50,28 @@ import org.springframework.web.servlet.DispatcherServlet;
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(WebMvcProperties.class)
@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })
public class MockMvcAutoConfiguration {
private final WebApplicationContext context;
private final ServerProperties serverProperties;
private final WebMvcProperties webMvcProperties;
MockMvcAutoConfiguration(WebApplicationContext context,
WebMvcProperties webMvcProperties) {
ServerProperties serverProperties, WebMvcProperties webMvcProperties) {
this.context = context;
this.serverProperties = serverProperties;
this.webMvcProperties = webMvcProperties;
}
@Bean
@ConditionalOnMissingBean
public DispatcherServletPath dispatcherServletPath() {
return () -> this.serverProperties.getServlet().getPath();
}
@Bean
@ConditionalOnMissingBean(MockMvcBuilder.class)
public DefaultMockMvcBuilder mockMvcBuilder(