Skip to content

Commit 291522a

Browse files
committed
Avoid duplicate scope registration
Closes gh-14990
1 parent 85cb6bf commit 291522a

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
5151
import org.springframework.web.context.support.GenericWebApplicationContext;
5252
import org.springframework.web.context.support.ServletContextAwareProcessor;
5353
import org.springframework.web.context.support.ServletContextResource;
54+
import org.springframework.web.context.support.ServletContextScope;
5455
import org.springframework.web.context.support.WebApplicationContextUtils;
5556

5657
/**
@@ -114,7 +115,7 @@ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactor
114115
beanFactory.addBeanPostProcessor(
115116
new WebApplicationContextServletContextAwareProcessor(this));
116117
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
117-
registerWebApplicationScopes(null);
118+
registerWebApplicationScopes();
118119
}
119120

120121
@Override
@@ -218,19 +219,25 @@ public void onStartup(ServletContext servletContext) throws ServletException {
218219

219220
private void selfInitialize(ServletContext servletContext) throws ServletException {
220221
prepareEmbeddedWebApplicationContext(servletContext);
221-
registerWebApplicationScopes(servletContext);
222+
registerApplicationScope(servletContext);
222223
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),
223224
servletContext);
224225
for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
225226
beans.onStartup(servletContext);
226227
}
227228
}
228229

229-
private void registerWebApplicationScopes(ServletContext servletContext) {
230+
private void registerApplicationScope(ServletContext servletContext) {
231+
ServletContextScope appScope = new ServletContextScope(servletContext);
232+
getBeanFactory().registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
233+
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
234+
servletContext.setAttribute(ServletContextScope.class.getName(), appScope);
235+
}
236+
237+
private void registerWebApplicationScopes() {
230238
ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(
231239
getBeanFactory());
232-
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
233-
servletContext);
240+
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());
234241
existingScopes.restore();
235242
}
236243

spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
5151
import org.springframework.beans.factory.config.Scope;
5252
import org.springframework.beans.factory.support.AbstractBeanDefinition;
5353
import org.springframework.beans.factory.support.RootBeanDefinition;
54+
import org.springframework.boot.testutil.InternalOutputCapture;
5455
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
5556
import org.springframework.boot.web.servlet.FilterRegistrationBean;
5657
import org.springframework.boot.web.servlet.ServletContextInitializer;
@@ -96,6 +97,9 @@ public class EmbeddedWebApplicationContextTests {
9697
@Rule
9798
public ExpectedException thrown = ExpectedException.none();
9899

100+
@Rule
101+
public InternalOutputCapture output = new InternalOutputCapture();
102+
99103
private EmbeddedWebApplicationContext context;
100104

101105
@Captor
@@ -489,6 +493,7 @@ public void doesNotReplaceExistingScopes() throws Exception {
489493
@Test
490494
public void servletRequestCanBeInjectedEarly() throws Exception {
491495
// gh-14990
496+
int initialOutputLength = this.output.toString().length();
492497
addEmbeddedServletContainerFactoryBean();
493498
RootBeanDefinition beanDefinition = new RootBeanDefinition(
494499
WithAutowiredServletRequest.class);
@@ -507,6 +512,16 @@ public void postProcessBeanFactory(
507512

508513
});
509514
this.context.refresh();
515+
String output = this.output.toString().substring(initialOutputLength);
516+
assertThat(output).doesNotContain("Replacing scope");
517+
}
518+
519+
@Test
520+
public void webApplicationScopeIsRegistered() throws Exception {
521+
addEmbeddedServletContainerFactoryBean();
522+
this.context.refresh();
523+
assertThat(this.context.getBeanFactory()
524+
.getRegisteredScope(WebApplicationContext.SCOPE_APPLICATION)).isNotNull();
510525
}
511526

512527
private void addEmbeddedServletContainerFactoryBean() {
@@ -575,4 +590,18 @@ public ServletRequest getRequest() {
575590

576591
}
577592

593+
protected static class WithAutowiredServletContext {
594+
595+
private final ServletContext context;
596+
597+
public WithAutowiredServletContext(ServletContext context) {
598+
this.context = context;
599+
}
600+
601+
public ServletContext getContext() {
602+
return this.context;
603+
}
604+
605+
}
606+
578607
}

0 commit comments

Comments
 (0)