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
85a56a79
Commit
85a56a79
authored
Mar 13, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidy up implementation of DispatcherServletAutoConfiguration
also adds another test.
parent
659d7b6d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
51 deletions
+78
-51
DispatcherServletAutoConfiguration.java
...autoconfigure/web/DispatcherServletAutoConfiguration.java
+48
-51
DispatcherServletAutoConfigurationTests.java
...onfigure/web/DispatcherServletAutoConfigurationTests.java
+30
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java
View file @
85a56a79
...
@@ -113,69 +113,66 @@ public class DispatcherServletAutoConfiguration {
...
@@ -113,69 +113,66 @@ public class DispatcherServletAutoConfiguration {
return
checkServletRegistrations
(
beanFactory
);
return
checkServletRegistrations
(
beanFactory
);
}
}
private
ConditionOutcome
checkServlets
(
ConfigurableListableBeanFactory
beanFactory
)
{
}
List
<
String
>
servlets
=
Arrays
.
asList
(
beanFactory
.
getBeanNamesForType
(
DispatcherServlet
.
class
,
false
,
false
));
private
static
ConditionOutcome
checkServlets
(
boolean
containsDispatcherBean
=
beanFactory
ConfigurableListableBeanFactory
beanFactory
)
{
.
containsBean
(
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
List
<
String
>
servlets
=
Arrays
.
asList
(
beanFactory
.
getBeanNamesForType
(
if
(
servlets
.
isEmpty
())
{
DispatcherServlet
.
class
,
false
,
false
));
if
(
containsDispatcherBean
)
{
boolean
containsDispatcherBean
=
beanFactory
return
ConditionOutcome
.
containsBean
(
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
.
noMatch
(
"found no DispatcherServlet but a non-DispatcherServlet named "
if
(
servlets
.
isEmpty
())
{
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
return
ConditionOutcome
.
match
(
"no DispatcherServlet found"
);
}
if
(
servlets
.
contains
(
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
))
{
return
ConditionOutcome
.
noMatch
(
"found DispatcherServlet named "
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
if
(
containsDispatcherBean
)
{
if
(
containsDispatcherBean
)
{
return
ConditionOutcome
.
noMatch
(
"found non-DispatcherServlet named "
return
ConditionOutcome
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
.
noMatch
(
"found no DispatcherServlet but a non-DispatcherServlet named "
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
}
return
ConditionOutcome
.
match
(
"no DispatcherServlet found"
);
}
if
(
servlets
.
contains
(
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
))
{
return
ConditionOutcome
.
noMatch
(
"found DispatcherServlet named "
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
if
(
containsDispatcherBean
)
{
return
ConditionOutcome
.
noMatch
(
"found non-DispatcherServlet named "
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
return
ConditionOutcome
return
ConditionOutcome
.
match
(
"one or more DispatcherServlets found and none is named "
.
match
(
"one or more DispatcherServlets found and none is named "
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
+
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
);
}
}
/**
private
static
ConditionOutcome
checkServletRegistrations
(
*/
ConfigurableListableBeanFactory
beanFactory
)
{
protected
ConditionOutcome
checkServletRegistrations
(
ConfigurableListableBeanFactory
beanFactory
)
{
List
<
String
>
registrations
=
Arrays
.
asList
(
beanFactory
.
getBeanNamesForType
(
ServletRegistrationBean
.
class
,
false
,
false
));
boolean
containsDispatcherRegistrationBean
=
beanFactory
.
containsBean
(
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
if
(
registrations
.
isEmpty
())
{
if
(
containsDispatcherRegistrationBean
)
{
return
ConditionOutcome
.
noMatch
(
"found no ServletRegistrationBean but a non-ServletRegistrationBean named "
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
}
return
ConditionOutcome
.
match
(
"no ServletRegistrationBean found"
);
}
if
(
registrations
.
contains
(
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
))
{
List
<
String
>
registrations
=
Arrays
.
asList
(
beanFactory
.
getBeanNamesForType
(
return
ConditionOutcome
.
noMatch
(
"found ServletRegistrationBean named "
ServletRegistrationBean
.
class
,
false
,
false
));
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
boolean
containsDispatcherRegistrationBean
=
beanFactory
}
.
containsBean
(
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
if
(
registrations
.
isEmpty
())
{
if
(
containsDispatcherRegistrationBean
)
{
if
(
containsDispatcherRegistrationBean
)
{
return
ConditionOutcome
return
ConditionOutcome
.
noMatch
(
"found non-ServletRegistrationBean named "
.
noMatch
(
"found no
ServletRegistrationBean but a no
n-ServletRegistrationBean named "
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
}
}
return
ConditionOutcome
.
match
(
"no ServletRegistrationBean found"
);
}
return
ConditionOutcome
if
(
registrations
.
contains
(
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
))
{
.
match
(
"one or more ServletRegistrationBeans is found and none is named "
return
ConditionOutcome
.
noMatch
(
"found ServletRegistrationBean named "
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
}
if
(
containsDispatcherRegistrationBean
)
{
return
ConditionOutcome
.
noMatch
(
"found non-ServletRegistrationBean named "
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
}
}
}
return
ConditionOutcome
.
match
(
"one or more ServletRegistrationBeans is found and none is named "
+
DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME
);
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java
View file @
85a56a79
...
@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web;
...
@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web;
import
javax.servlet.MultipartConfigElement
;
import
javax.servlet.MultipartConfigElement
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.beans.factory.UnsatisfiedDependencyException
;
import
org.springframework.boot.context.embedded.MultiPartConfigFactory
;
import
org.springframework.boot.context.embedded.MultiPartConfigFactory
;
import
org.springframework.boot.context.embedded.ServletRegistrationBean
;
import
org.springframework.boot.context.embedded.ServletRegistrationBean
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
...
@@ -69,6 +70,23 @@ public class DispatcherServletAutoConfigurationTests {
...
@@ -69,6 +70,23 @@ public class DispatcherServletAutoConfigurationTests {
assertEquals
(
0
,
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
);
assertEquals
(
0
,
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
);
}
}
// If you override either the dispatcherServlet or its registration you have to
// provide both...
@Test
(
expected
=
UnsatisfiedDependencyException
.
class
)
public
void
registrationOverrideWithAutowiredServlet
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
this
.
context
.
register
(
CustomAutowiredRegistration
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
DispatcherServletAutoConfiguration
.
class
);
this
.
context
.
setServletContext
(
new
MockServletContext
());
this
.
context
.
refresh
();
ServletRegistrationBean
registration
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertEquals
(
"[/foo]"
,
registration
.
getUrlMappings
().
toString
());
assertEquals
(
"customDispatcher"
,
registration
.
getServletName
());
assertEquals
(
1
,
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
);
}
@Test
@Test
public
void
servletPath
()
throws
Exception
{
public
void
servletPath
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
this
.
context
=
new
AnnotationConfigWebApplicationContext
();
...
@@ -121,4 +139,16 @@ public class DispatcherServletAutoConfigurationTests {
...
@@ -121,4 +139,16 @@ public class DispatcherServletAutoConfigurationTests {
}
}
}
}
@Configuration
protected
static
class
CustomAutowiredRegistration
{
@Bean
public
ServletRegistrationBean
dispatcherServletRegistration
(
DispatcherServlet
dispatcherServlet
)
{
ServletRegistrationBean
registration
=
new
ServletRegistrationBean
(
dispatcherServlet
,
"/foo"
);
registration
.
setName
(
"customDispatcher"
);
return
registration
;
}
}
}
}
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