Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
a1491be6
Commit
a1491be6
authored
Jun 25, 2018
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refine DispatcherServletPathProvider creation
Closes gh-13527
parent
04119585
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
9 deletions
+89
-9
DispatcherServletAutoConfiguration.java
...igure/web/servlet/DispatcherServletAutoConfiguration.java
+9
-6
DispatcherServletAutoConfigurationTests.java
.../web/servlet/DispatcherServletAutoConfigurationTests.java
+80
-3
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java
View file @
a1491be6
...
...
@@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type
;
import
org.springframework.boot.autoconfigure.condition.SpringBootCondition
;
...
...
@@ -116,12 +117,6 @@ public class DispatcherServletAutoConfiguration {
return
resolver
;
}
@Bean
public
DispatcherServletPathProvider
mainDispatcherServletPathProvider
()
{
return
()
->
DispatcherServletConfiguration
.
this
.
serverProperties
.
getServlet
()
.
getPath
();
}
}
@Configuration
...
...
@@ -161,6 +156,14 @@ public class DispatcherServletAutoConfiguration {
return
registration
;
}
@Bean
@ConditionalOnMissingBean
(
DispatcherServletPathProvider
.
class
)
@ConditionalOnSingleCandidate
(
DispatcherServlet
.
class
)
public
DispatcherServletPathProvider
dispatcherServletPathProvider
()
{
return
()
->
DispatcherServletRegistrationConfiguration
.
this
.
serverProperties
.
getServlet
().
getPath
();
}
}
@Order
(
Ordered
.
LOWEST_PRECEDENCE
-
10
)
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java
View file @
a1491be6
...
...
@@ -35,6 +35,7 @@ import org.springframework.web.multipart.MultipartResolver;
import
org.springframework.web.servlet.DispatcherServlet
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for {@link DispatcherServletAutoConfiguration}.
...
...
@@ -65,6 +66,8 @@ public class DispatcherServletAutoConfigurationTests {
.
run
((
context
)
->
{
assertThat
(
context
).
doesNotHaveBean
(
ServletRegistrationBean
.
class
);
assertThat
(
context
).
doesNotHaveBean
(
DispatcherServlet
.
class
);
assertThat
(
context
)
.
doesNotHaveBean
(
DispatcherServletPathProvider
.
class
);
});
}
...
...
@@ -73,7 +76,8 @@ public class DispatcherServletAutoConfigurationTests {
@Test
public
void
registrationOverrideWithDispatcherServletWrongName
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomDispatcherServletDifferentName
.
class
)
.
withUserConfiguration
(
CustomDispatcherServletDifferentName
.
class
,
CustomDispatcherServletPathProvider
.
class
)
.
run
((
context
)
->
{
ServletRegistrationBean
<?>
registration
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
...
...
@@ -86,8 +90,8 @@ public class DispatcherServletAutoConfigurationTests {
@Test
public
void
registrationOverrideWithAutowiredServlet
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomAutowiredRegistration
.
class
)
.
run
((
context
)
->
{
this
.
contextRunner
.
withUserConfiguration
(
CustomAutowiredRegistration
.
class
,
CustomDispatcherServletPathProvider
.
class
)
.
run
((
context
)
->
{
ServletRegistrationBean
<?>
registration
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registration
.
getUrlMappings
()).
containsExactly
(
"/foo"
);
...
...
@@ -112,6 +116,40 @@ public class DispatcherServletAutoConfigurationTests {
});
}
@Test
public
void
pathProviderNotCreatedWhenMultipleDispatcherServletsPresent
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomDispatcherServletDifferentName
.
class
)
.
run
((
context
)
->
assertThat
(
context
)
.
doesNotHaveBean
(
DispatcherServletPathProvider
.
class
));
}
@Test
public
void
pathProviderWhenCustomDispatcherServletSameNameShouldReturnConfiguredServletPath
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomDispatcherServletSameName
.
class
)
.
withPropertyValues
(
"server.servlet.path:/spring"
)
.
run
((
context
)
->
assertThat
(
context
.
getBean
(
DispatcherServletPathProvider
.
class
).
getServletPath
())
.
isEqualTo
(
"/spring"
));
}
@Test
public
void
pathProviderNotCreatedWhenDefaultDispatcherServletNotAvailable
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomDispatcherServletDifferentName
.
class
,
NonServletConfiguration
.
class
)
.
run
((
context
)
->
assertThat
(
context
)
.
doesNotHaveBean
(
DispatcherServletPathProvider
.
class
));
}
@Test
public
void
pathProviderNotCreatedWhenCustomRegistrationBeanPresent
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomDispatcherServletRegistration
.
class
)
.
run
((
context
)
->
assertThat
(
context
)
.
doesNotHaveBean
(
DispatcherServletPathProvider
.
class
));
}
@Test
public
void
multipartConfig
()
{
this
.
contextRunner
.
withUserConfiguration
(
MultipartConfiguration
.
class
)
...
...
@@ -198,6 +236,16 @@ public class DispatcherServletAutoConfigurationTests {
}
@Configuration
protected
static
class
CustomDispatcherServletPathProvider
{
@Bean
public
DispatcherServletPathProvider
dispatcherServletPathProvider
()
{
return
mock
(
DispatcherServletPathProvider
.
class
);
}
}
@Configuration
protected
static
class
CustomAutowiredRegistration
{
...
...
@@ -210,6 +258,11 @@ public class DispatcherServletAutoConfigurationTests {
return
registration
;
}
@Bean
public
DispatcherServletPathProvider
dispatcherServletPathProvider
()
{
return
mock
(
DispatcherServletPathProvider
.
class
);
}
}
@Configuration
...
...
@@ -232,6 +285,30 @@ public class DispatcherServletAutoConfigurationTests {
}
@Configuration
protected
static
class
CustomDispatcherServletSameName
{
@Bean
(
name
=
DispatcherServletAutoConfiguration
.
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
)
public
DispatcherServlet
dispatcherServlet
()
{
return
new
DispatcherServlet
();
}
}
@Configuration
protected
static
class
CustomDispatcherServletRegistration
{
@Bean
(
name
=
DispatcherServletAutoConfiguration
.
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
)
public
ServletRegistrationBean
<
DispatcherServlet
>
dispatcherServletRegistration
(
DispatcherServlet
dispatcherServlet
)
{
ServletRegistrationBean
<
DispatcherServlet
>
registration
=
new
ServletRegistrationBean
<>(
dispatcherServlet
,
"/foo"
);
registration
.
setName
(
"customDispatcher"
);
return
registration
;
}
}
private
static
class
MockMultipartResolver
implements
MultipartResolver
{
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment