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
aefec4c1
Commit
aefec4c1
authored
Nov 27, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check that SessionScope is available early
parent
2f6f88e3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
5 deletions
+59
-5
EmbeddedWebApplicationContext.java
.../boot/context/embedded/EmbeddedWebApplicationContext.java
+12
-5
AnnotationConfigEmbeddedWebApplicationContextTests.java
...d/AnnotationConfigEmbeddedWebApplicationContextTests.java
+47
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java
View file @
aefec4c1
...
...
@@ -21,8 +21,10 @@ import java.util.Collection;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.EventListener
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Set
;
...
...
@@ -167,10 +169,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
"Cannot initialize servlet context"
,
ex
);
}
}
WebApplicationContextUtils
.
registerWebApplicationScopes
(
getBeanFactory
(),
getServletContext
());
WebApplicationContextUtils
.
registerEnvironmentBeans
(
getBeanFactory
(),
getServletContext
());
initPropertySources
();
}
...
...
@@ -209,6 +207,10 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
@Override
public
void
onStartup
(
ServletContext
servletContext
)
throws
ServletException
{
prepareEmbeddedWebApplicationContext
(
servletContext
);
WebApplicationContextUtils
.
registerWebApplicationScopes
(
getBeanFactory
(),
getServletContext
());
WebApplicationContextUtils
.
registerEnvironmentBeans
(
getBeanFactory
(),
getServletContext
());
for
(
ServletContextInitializer
beans
:
getServletContextInitializerBeans
())
{
beans
.
onStartup
(
servletContext
);
}
...
...
@@ -357,7 +359,12 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
o2
.
getValue
());
}
};
beans
.
addAll
(
getBeanFactory
().
getBeansOfType
(
type
,
true
,
true
).
entrySet
());
String
[]
names
=
getBeanFactory
().
getBeanNamesForType
(
type
,
true
,
false
);
Map
<
String
,
T
>
map
=
new
LinkedHashMap
<
String
,
T
>();
for
(
String
name
:
names
)
{
map
.
put
(
name
,
getBeanFactory
().
getBean
(
name
,
type
));
}
beans
.
addAll
(
map
.
entrySet
());
Collections
.
sort
(
beans
,
comparator
);
return
beans
;
}
...
...
spring-boot/src/test/java/org/springframework/boot/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java
View file @
aefec4c1
...
...
@@ -16,13 +16,23 @@
package
org
.
springframework
.
boot
.
context
.
embedded
;
import
java.io.IOException
;
import
javax.servlet.GenericServlet
;
import
javax.servlet.Servlet
;
import
javax.servlet.ServletContext
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.config.ExampleEmbeddedWebApplicationConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.ScopedProxyMode
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.ServletContextAware
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
...
...
@@ -46,6 +56,23 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests {
verifyContext
();
}
@Test
public
void
sessionScopeAvailable
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ExampleEmbeddedWebApplicationConfiguration
.
class
,
SessionScopedComponent
.
class
);
verifyContext
();
}
@Test
public
void
sessionScopeAvailableToServlet
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ExampleEmbeddedWebApplicationConfiguration
.
class
,
ExampleServletWithAutowired
.
class
,
SessionScopedComponent
.
class
);
Servlet
servlet
=
this
.
context
.
getBean
(
ExampleServletWithAutowired
.
class
);
assertNotNull
(
servlet
);
}
@Test
public
void
createFromConfigClass
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
...
...
@@ -102,6 +129,26 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests {
verify
(
containerFactory
.
getServletContext
()).
addServlet
(
"servlet"
,
servlet
);
}
@Component
protected
static
class
ExampleServletWithAutowired
extends
GenericServlet
{
@Autowired
private
SessionScopedComponent
component
;
@Override
public
void
service
(
ServletRequest
req
,
ServletResponse
res
)
throws
ServletException
,
IOException
{
assertNotNull
(
this
.
component
);
}
}
@Component
@Scope
(
value
=
"session"
,
proxyMode
=
ScopedProxyMode
.
TARGET_CLASS
)
protected
static
class
SessionScopedComponent
{
}
@Configuration
@EnableWebMvc
public
static
class
ServletContextAwareEmbeddedConfiguration
implements
...
...
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