From aefec4c16a9bb3753352f527489578667eaed218 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 27 Nov 2013 14:04:09 +0000 Subject: [PATCH] Check that SessionScope is available early --- .../EmbeddedWebApplicationContext.java | 17 +++++-- ...figEmbeddedWebApplicationContextTests.java | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java index 374081b1cef..b0492b2b99b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java @@ -21,8 +21,10 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.EventListener; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -167,10 +169,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext "Cannot initialize servlet context", ex); } } - WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(), - getServletContext()); - WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(), - getServletContext()); initPropertySources(); } @@ -209,6 +207,10 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext @Override public void onStartup(ServletContext servletContext) throws ServletException { prepareEmbeddedWebApplicationContext(servletContext); + WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(), + getServletContext()); + WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(), + getServletContext()); for (ServletContextInitializer beans : getServletContextInitializerBeans()) { beans.onStartup(servletContext); } @@ -357,7 +359,12 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext o2.getValue()); } }; - beans.addAll(getBeanFactory().getBeansOfType(type, true, true).entrySet()); + String[] names = getBeanFactory().getBeanNamesForType(type, true, false); + Map map = new LinkedHashMap(); + for (String name : names) { + map.put(name, getBeanFactory().getBean(name, type)); + } + beans.addAll(map.entrySet()); Collections.sort(beans, comparator); return beans; } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java index d01097092c8..155ee9510d0 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java @@ -16,13 +16,23 @@ package org.springframework.boot.context.embedded; +import java.io.IOException; + +import javax.servlet.GenericServlet; import javax.servlet.Servlet; import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.config.ExampleEmbeddedWebApplicationConfiguration; import org.springframework.context.annotation.Bean; 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.servlet.config.annotation.EnableWebMvc; @@ -46,6 +56,23 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests { 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 public void createFromConfigClass() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( @@ -102,6 +129,26 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests { 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 @EnableWebMvc public static class ServletContextAwareEmbeddedConfiguration implements