Commit 291522a2 authored by Andy Wilkinson's avatar Andy Wilkinson

Avoid duplicate scope registration

Closes gh-14990
parent 85cb6bf0
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2018 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);
registerWebApplicationScopes(servletContext); registerApplicationScope(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();
} }
......
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2018 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;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment