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
291522a2
Commit
291522a2
authored
Nov 23, 2018
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid duplicate scope registration
Closes gh-14990
parent
85cb6bf0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
7 deletions
+43
-7
EmbeddedWebApplicationContext.java
.../boot/context/embedded/EmbeddedWebApplicationContext.java
+13
-6
EmbeddedWebApplicationContextTests.java
.../context/embedded/EmbeddedWebApplicationContextTests.java
+30
-1
No files found.
spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java
View file @
291522a2
/*
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -51,6 +51,7 @@ import org.springframework.web.context.WebApplicationContext;
...
@@ -51,6 +51,7 @@ import org.springframework.web.context.WebApplicationContext;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
import
org.springframework.web.context.support.ServletContextAwareProcessor
;
import
org.springframework.web.context.support.ServletContextAwareProcessor
;
import
org.springframework.web.context.support.ServletContextResource
;
import
org.springframework.web.context.support.ServletContextResource
;
import
org.springframework.web.context.support.ServletContextScope
;
import
org.springframework.web.context.support.WebApplicationContextUtils
;
import
org.springframework.web.context.support.WebApplicationContextUtils
;
/**
/**
...
@@ -114,7 +115,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -114,7 +115,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
beanFactory
.
addBeanPostProcessor
(
beanFactory
.
addBeanPostProcessor
(
new
WebApplicationContextServletContextAwareProcessor
(
this
));
new
WebApplicationContextServletContextAwareProcessor
(
this
));
beanFactory
.
ignoreDependencyInterface
(
ServletContextAware
.
class
);
beanFactory
.
ignoreDependencyInterface
(
ServletContextAware
.
class
);
registerWebApplicationScopes
(
null
);
registerWebApplicationScopes
();
}
}
@Override
@Override
...
@@ -218,7 +219,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -218,7 +219,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
private
void
selfInitialize
(
ServletContext
servletContext
)
throws
ServletException
{
private
void
selfInitialize
(
ServletContext
servletContext
)
throws
ServletException
{
prepareEmbeddedWebApplicationContext
(
servletContext
);
prepareEmbeddedWebApplicationContext
(
servletContext
);
register
WebApplicationScopes
(
servletContext
);
register
ApplicationScope
(
servletContext
);
WebApplicationContextUtils
.
registerEnvironmentBeans
(
getBeanFactory
(),
WebApplicationContextUtils
.
registerEnvironmentBeans
(
getBeanFactory
(),
servletContext
);
servletContext
);
for
(
ServletContextInitializer
beans
:
getServletContextInitializerBeans
())
{
for
(
ServletContextInitializer
beans
:
getServletContextInitializerBeans
())
{
...
@@ -226,11 +227,17 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -226,11 +227,17 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
}
}
}
}
private
void
registerWebApplicationScopes
(
ServletContext
servletContext
)
{
private
void
registerApplicationScope
(
ServletContext
servletContext
)
{
ServletContextScope
appScope
=
new
ServletContextScope
(
servletContext
);
getBeanFactory
().
registerScope
(
WebApplicationContext
.
SCOPE_APPLICATION
,
appScope
);
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
servletContext
.
setAttribute
(
ServletContextScope
.
class
.
getName
(),
appScope
);
}
private
void
registerWebApplicationScopes
()
{
ExistingWebApplicationScopes
existingScopes
=
new
ExistingWebApplicationScopes
(
ExistingWebApplicationScopes
existingScopes
=
new
ExistingWebApplicationScopes
(
getBeanFactory
());
getBeanFactory
());
WebApplicationContextUtils
.
registerWebApplicationScopes
(
getBeanFactory
(),
WebApplicationContextUtils
.
registerWebApplicationScopes
(
getBeanFactory
());
servletContext
);
existingScopes
.
restore
();
existingScopes
.
restore
();
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java
View file @
291522a2
/*
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -51,6 +51,7 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues;
...
@@ -51,6 +51,7 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues;
import
org.springframework.beans.factory.config.Scope
;
import
org.springframework.beans.factory.config.Scope
;
import
org.springframework.beans.factory.support.AbstractBeanDefinition
;
import
org.springframework.beans.factory.support.AbstractBeanDefinition
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.boot.testutil.InternalOutputCapture
;
import
org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean
;
import
org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean
;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.boot.web.servlet.ServletContextInitializer
;
import
org.springframework.boot.web.servlet.ServletContextInitializer
;
...
@@ -96,6 +97,9 @@ public class EmbeddedWebApplicationContextTests {
...
@@ -96,6 +97,9 @@ public class EmbeddedWebApplicationContextTests {
@Rule
@Rule
public
ExpectedException
thrown
=
ExpectedException
.
none
();
public
ExpectedException
thrown
=
ExpectedException
.
none
();
@Rule
public
InternalOutputCapture
output
=
new
InternalOutputCapture
();
private
EmbeddedWebApplicationContext
context
;
private
EmbeddedWebApplicationContext
context
;
@Captor
@Captor
...
@@ -489,6 +493,7 @@ public class EmbeddedWebApplicationContextTests {
...
@@ -489,6 +493,7 @@ public class EmbeddedWebApplicationContextTests {
@Test
@Test
public
void
servletRequestCanBeInjectedEarly
()
throws
Exception
{
public
void
servletRequestCanBeInjectedEarly
()
throws
Exception
{
// gh-14990
// gh-14990
int
initialOutputLength
=
this
.
output
.
toString
().
length
();
addEmbeddedServletContainerFactoryBean
();
addEmbeddedServletContainerFactoryBean
();
RootBeanDefinition
beanDefinition
=
new
RootBeanDefinition
(
RootBeanDefinition
beanDefinition
=
new
RootBeanDefinition
(
WithAutowiredServletRequest
.
class
);
WithAutowiredServletRequest
.
class
);
...
@@ -507,6 +512,16 @@ public class EmbeddedWebApplicationContextTests {
...
@@ -507,6 +512,16 @@ public class EmbeddedWebApplicationContextTests {
});
});
this
.
context
.
refresh
();
this
.
context
.
refresh
();
String
output
=
this
.
output
.
toString
().
substring
(
initialOutputLength
);
assertThat
(
output
).
doesNotContain
(
"Replacing scope"
);
}
@Test
public
void
webApplicationScopeIsRegistered
()
throws
Exception
{
addEmbeddedServletContainerFactoryBean
();
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeanFactory
()
.
getRegisteredScope
(
WebApplicationContext
.
SCOPE_APPLICATION
)).
isNotNull
();
}
}
private
void
addEmbeddedServletContainerFactoryBean
()
{
private
void
addEmbeddedServletContainerFactoryBean
()
{
...
@@ -575,4 +590,18 @@ public class EmbeddedWebApplicationContextTests {
...
@@ -575,4 +590,18 @@ public class EmbeddedWebApplicationContextTests {
}
}
protected
static
class
WithAutowiredServletContext
{
private
final
ServletContext
context
;
public
WithAutowiredServletContext
(
ServletContext
context
)
{
this
.
context
=
context
;
}
public
ServletContext
getContext
()
{
return
this
.
context
;
}
}
}
}
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