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
44273ee0
Commit
44273ee0
authored
Jul 11, 2018
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x' into 2.0.x
parents
48819253
57ebdab2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
5 deletions
+44
-5
SpringBootServletInitializer.java
...oot/web/servlet/support/SpringBootServletInitializer.java
+32
-4
SpringBootServletInitializerTests.java
...eb/servlet/support/SpringBootServletInitializerTests.java
+12
-1
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java
View file @
44273ee0
...
...
@@ -30,16 +30,20 @@ import org.apache.commons.logging.LogFactory;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.builder.ParentContextApplicationContextInitializer
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
;
import
org.springframework.boot.web.servlet.ServletContextInitializer
;
import
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.util.Assert
;
import
org.springframework.web.WebApplicationInitializer
;
import
org.springframework.web.context.ConfigurableWebEnvironment
;
import
org.springframework.web.context.ContextLoaderListener
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.context.support.StandardServletEnvironment
;
/**
* An opinionated {@link WebApplicationInitializer} to run a {@link SpringApplication}
...
...
@@ -104,9 +108,6 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
protected
WebApplicationContext
createRootApplicationContext
(
ServletContext
servletContext
)
{
SpringApplicationBuilder
builder
=
createSpringApplicationBuilder
();
StandardServletEnvironment
environment
=
new
StandardServletEnvironment
();
environment
.
initPropertySources
(
servletContext
,
null
);
builder
.
environment
(
environment
);
builder
.
main
(
getClass
());
ApplicationContext
parent
=
getExistingRootWebApplicationContext
(
servletContext
);
if
(
parent
!=
null
)
{
...
...
@@ -119,6 +120,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
new
ServletContextApplicationContextInitializer
(
servletContext
));
builder
.
contextClass
(
AnnotationConfigServletWebServerApplicationContext
.
class
);
builder
=
configure
(
builder
);
builder
.
listeners
(
new
WebEnvironmentPropertySourceInitializer
(
servletContext
));
SpringApplication
application
=
builder
.
build
();
if
(
application
.
getAllSources
().
isEmpty
()
&&
AnnotationUtils
.
findAnnotation
(
getClass
(),
Configuration
.
class
)
!=
null
)
{
...
...
@@ -178,4 +180,30 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
return
builder
;
}
private
static
final
class
WebEnvironmentPropertySourceInitializer
implements
ApplicationListener
<
ApplicationEnvironmentPreparedEvent
>,
Ordered
{
private
final
ServletContext
servletContext
;
private
WebEnvironmentPropertySourceInitializer
(
ServletContext
servletContext
)
{
this
.
servletContext
=
servletContext
;
}
@Override
public
void
onApplicationEvent
(
ApplicationEnvironmentPreparedEvent
event
)
{
ConfigurableEnvironment
environment
=
event
.
getEnvironment
();
if
(
environment
instanceof
ConfigurableWebEnvironment
)
{
((
ConfigurableWebEnvironment
)
environment
)
.
initPropertySources
(
this
.
servletContext
,
null
);
}
}
@Override
public
int
getOrder
()
{
return
Ordered
.
HIGHEST_PRECEDENCE
;
}
}
}
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java
View file @
44273ee0
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
...
@@ -21,6 +21,7 @@ import java.util.Collections;
import
javax.servlet.ServletContext
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
...
...
@@ -29,6 +30,7 @@ import org.springframework.beans.DirectFieldAccessor;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
;
import
org.springframework.boot.testsupport.rule.OutputCapture
;
import
org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory
;
import
org.springframework.boot.web.server.WebServer
;
import
org.springframework.boot.web.servlet.server.ServletWebServerFactory
;
...
...
@@ -57,10 +59,19 @@ public class SpringBootServletInitializerTests {
@Rule
public
ExpectedException
thrown
=
ExpectedException
.
none
();
@Rule
public
OutputCapture
output
=
new
OutputCapture
();
private
ServletContext
servletContext
=
new
MockServletContext
();
private
SpringApplication
application
;
@After
public
void
verifyLoggingOutput
()
{
assertThat
(
this
.
output
.
toString
())
.
doesNotContain
(
StandardServletEnvironment
.
class
.
getSimpleName
());
}
@Test
public
void
failsWithoutConfigure
()
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
...
...
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