Commit aefec4c1 authored by Dave Syer's avatar Dave Syer

Check that SessionScope is available early

parent 2f6f88e3
...@@ -21,8 +21,10 @@ import java.util.Collection; ...@@ -21,8 +21,10 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.EventListener; import java.util.EventListener;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
...@@ -167,10 +169,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext ...@@ -167,10 +169,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
"Cannot initialize servlet context", ex); "Cannot initialize servlet context", ex);
} }
} }
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
getServletContext());
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),
getServletContext());
initPropertySources(); initPropertySources();
} }
...@@ -209,6 +207,10 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext ...@@ -209,6 +207,10 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { public void onStartup(ServletContext servletContext) throws ServletException {
prepareEmbeddedWebApplicationContext(servletContext); prepareEmbeddedWebApplicationContext(servletContext);
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
getServletContext());
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),
getServletContext());
for (ServletContextInitializer beans : getServletContextInitializerBeans()) { for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
beans.onStartup(servletContext); beans.onStartup(servletContext);
} }
...@@ -357,7 +359,12 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext ...@@ -357,7 +359,12 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
o2.getValue()); 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); Collections.sort(beans, comparator);
return beans; return beans;
} }
......
...@@ -16,13 +16,23 @@ ...@@ -16,13 +16,23 @@
package org.springframework.boot.context.embedded; package org.springframework.boot.context.embedded;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.config.ExampleEmbeddedWebApplicationConfiguration; import org.springframework.boot.context.embedded.config.ExampleEmbeddedWebApplicationConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.context.ServletContextAware;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
...@@ -46,6 +56,23 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests { ...@@ -46,6 +56,23 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests {
verifyContext(); 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 @Test
public void createFromConfigClass() throws Exception { public void createFromConfigClass() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
...@@ -102,6 +129,26 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests { ...@@ -102,6 +129,26 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests {
verify(containerFactory.getServletContext()).addServlet("servlet", servlet); 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 @Configuration
@EnableWebMvc @EnableWebMvc
public static class ServletContextAwareEmbeddedConfiguration implements public static class ServletContextAwareEmbeddedConfiguration implements
......
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