Skip to content

Commit 3331fa2

Browse files
committed
Merge branch '1.5.x' into 2.0.x
2 parents a54de61 + 291522a commit 3331fa2

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContext.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.web.context.support.GenericWebApplicationContext;
5656
import org.springframework.web.context.support.ServletContextAwareProcessor;
5757
import org.springframework.web.context.support.ServletContextResource;
58+
import org.springframework.web.context.support.ServletContextScope;
5859
import org.springframework.web.context.support.WebApplicationContextUtils;
5960

6061
/**
@@ -132,7 +133,7 @@ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactor
132133
beanFactory.addBeanPostProcessor(
133134
new WebApplicationContextServletContextAwareProcessor(this));
134135
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
135-
registerWebApplicationScopes(null);
136+
registerWebApplicationScopes();
136137
}
137138

138139
@Override
@@ -227,19 +228,25 @@ private org.springframework.boot.web.servlet.ServletContextInitializer getSelfIn
227228

228229
private void selfInitialize(ServletContext servletContext) throws ServletException {
229230
prepareWebApplicationContext(servletContext);
230-
registerWebApplicationScopes(servletContext);
231+
registerApplicationScope(servletContext);
231232
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),
232233
servletContext);
233234
for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
234235
beans.onStartup(servletContext);
235236
}
236237
}
237238

238-
private void registerWebApplicationScopes(ServletContext servletContext) {
239+
private void registerApplicationScope(ServletContext servletContext) {
240+
ServletContextScope appScope = new ServletContextScope(servletContext);
241+
getBeanFactory().registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
242+
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
243+
servletContext.setAttribute(ServletContextScope.class.getName(), appScope);
244+
}
245+
246+
private void registerWebApplicationScopes() {
239247
ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(
240248
getBeanFactory());
241-
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
242-
servletContext);
249+
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());
243250
existingScopes.restore();
244251
}
245252

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.beans.factory.config.Scope;
5050
import org.springframework.beans.factory.support.AbstractBeanDefinition;
5151
import org.springframework.beans.factory.support.RootBeanDefinition;
52+
import org.springframework.boot.testsupport.rule.OutputCapture;
5253
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
5354
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
5455
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@@ -94,6 +95,9 @@ public class ServletWebServerApplicationContextTests {
9495

9596
private ServletWebServerApplicationContext context;
9697

98+
@Rule
99+
public OutputCapture output = new OutputCapture();
100+
97101
@Captor
98102
private ArgumentCaptor<Filter> filterCaptor;
99103

@@ -469,6 +473,7 @@ public void doesNotReplaceExistingScopes() {
469473
@Test
470474
public void servletRequestCanBeInjectedEarly() throws Exception {
471475
// gh-14990
476+
int initialOutputLength = this.output.toString().length();
472477
addWebServerFactoryBean();
473478
RootBeanDefinition beanDefinition = new RootBeanDefinition(
474479
WithAutowiredServletRequest.class);
@@ -487,6 +492,16 @@ public void postProcessBeanFactory(
487492

488493
});
489494
this.context.refresh();
495+
String output = this.output.toString().substring(initialOutputLength);
496+
assertThat(output).doesNotContain("Replacing scope");
497+
}
498+
499+
@Test
500+
public void webApplicationScopeIsRegistered() throws Exception {
501+
addWebServerFactoryBean();
502+
this.context.refresh();
503+
assertThat(this.context.getBeanFactory()
504+
.getRegisteredScope(WebApplicationContext.SCOPE_APPLICATION)).isNotNull();
490505
}
491506

492507
private void addWebServerFactoryBean() {
@@ -555,4 +570,18 @@ public ServletRequest getRequest() {
555570

556571
}
557572

573+
protected static class WithAutowiredServletContext {
574+
575+
private final ServletContext context;
576+
577+
public WithAutowiredServletContext(ServletContext context) {
578+
this.context = context;
579+
}
580+
581+
public ServletContext getContext() {
582+
return this.context;
583+
}
584+
585+
}
586+
558587
}

0 commit comments

Comments
 (0)