From 43eac78346d9513c9c47c379ce3f62f795f85a07 Mon Sep 17 00:00:00 2001 From: joeyvmason Date: Mon, 9 Jan 2017 00:16:49 -0800 Subject: [PATCH 1/3] Added ability to provide custom ApplicationContext. For Spring testing, made objects that can potentially be shared by multiple tests classes and methods beans. --- aws-serverless-java-container-spring/pom.xml | 7 +++ .../LambdaSpringApplicationInitializer.java | 48 +++----------- .../spring/SpringLambdacontainerHandler.java | 42 +++++-------- .../proxy/spring/SpringAwsProxyTest.java | 62 ++++++++++++++----- .../spring/echoapp/EchoSpringAppConfig.java | 28 +++++++++ 5 files changed, 105 insertions(+), 82 deletions(-) diff --git a/aws-serverless-java-container-spring/pom.xml b/aws-serverless-java-container-spring/pom.xml index 6ae41bbff..befa28384 100644 --- a/aws-serverless-java-container-spring/pom.xml +++ b/aws-serverless-java-container-spring/pom.xml @@ -45,6 +45,13 @@ ${spring.version} + + + org.springframework + spring-test + ${spring.version} + + commons-codec diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java index 0c4cfb39a..f20a615ab 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java @@ -12,10 +12,10 @@ */ package com.amazonaws.serverless.proxy.spring; -import com.amazonaws.serverless.exceptions.InvalidResponseObjectException; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.ServletRequestHandledEvent; @@ -44,11 +44,10 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial private static final String DEFAULT_SERVLET_NAME = "aws-servless-java-container"; // Configuration variables that can be passed in - private List configurationClasses; + private ConfigurableWebApplicationContext applicationContext; private List contextListeners; // Dynamically instantiated properties - private AnnotationConfigWebApplicationContext applicationContext; private ServletConfig dispatcherConfig; private DispatcherServlet dispatcherServlet; @@ -57,31 +56,11 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial /** * Creates a new instance of the WebApplicationInitializer + * @param applicationContext A custom ConfigurableWebApplicationContext to be used */ - public LambdaSpringApplicationInitializer() { - configurationClasses = new ArrayList<>(); - contextListeners = new ArrayList<>(); - } - - /** - * Adds a configuration class to the Spring startup process. This should be called at least once with an annotated - * class that contains the @Configuration annotations. The simplest possible class uses the @ComponentScan - * annocation to load all controllers. - * - *
-     * {@Code
-     * @Configuration
-     * @ComponentScan("com.amazonaws.serverless.proxy.spring.echoapp")
-     * public class EchoSpringAppConfig {
-     * }
-     * }
-     * 
- * @param configuration A set of configuration classes - */ - public void addConfigurationClasses(Class... configuration) { - for (Class config : configuration) { - configurationClasses.add(config); - } + public LambdaSpringApplicationInitializer(ConfigurableWebApplicationContext applicationContext) { + this.contextListeners = new ArrayList<>(); + this.applicationContext = applicationContext; } /** @@ -100,21 +79,8 @@ public void dispatch(HttpServletRequest request, HttpServletResponse response) dispatcherServlet.service(request, response); } - /** - * Returns the application context initialized in the library - * @return - */ - public AnnotationConfigWebApplicationContext getApplicationContext() { - return applicationContext; - } - @Override public void onStartup(ServletContext servletContext) throws ServletException { - // Create the 'root' Spring application context - applicationContext = new AnnotationConfigWebApplicationContext(); - for (Class config : configurationClasses) { - applicationContext.register(config); - } applicationContext.setServletContext(servletContext); dispatcherConfig = new DefaultDispatcherConfig(servletContext); @@ -138,7 +104,7 @@ public void onApplicationEvent(ServletRequestHandledEvent servletRequestHandledE // Register and map the dispatcher servlet dispatcherServlet = new DispatcherServlet(applicationContext); - dispatcherServlet.refresh(); +// dispatcherServlet.refresh(); dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(applicationContext)); dispatcherServlet.init(dispatcherConfig); diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java index 44dbcd278..17c97f64b 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java @@ -21,6 +21,7 @@ import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader; import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter; import com.amazonaws.services.lambda.runtime.Context; +import org.springframework.web.context.ConfigurableWebApplicationContext; import javax.servlet.ServletContext; import java.util.concurrent.CountDownLatch; @@ -38,25 +39,22 @@ public class SpringLambdaContainerHandler extends Lam // State vars private boolean initialized; + /** * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects - * @param config A set of classes annotated with the Spring @Configuration annotation + * @param applicationContext A custom ConfigurableWebApplicationContext to be used * @return An initialized instance of the `SpringLambdaContainerHandler` * @throws ContainerInitializationException */ - public static SpringLambdaContainerHandler getAwsProxyHandler(Class... config) + public static SpringLambdaContainerHandler getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext) throws ContainerInitializationException { - SpringLambdaContainerHandler handler = - new SpringLambdaContainerHandler<>( - new AwsProxyHttpServletRequestReader(), - new AwsProxyHttpServletResponseWriter(), - new AwsProxySecurityContextWriter(), - new AwsProxyExceptionHandler() - ); - - handler.addConfiguration(config); - - return handler; + return new SpringLambdaContainerHandler<>( + new AwsProxyHttpServletRequestReader(), + new AwsProxyHttpServletResponseWriter(), + new AwsProxySecurityContextWriter(), + new AwsProxyExceptionHandler(), + applicationContext + ); } /** @@ -69,20 +67,13 @@ public static SpringLambdaContainerHandler ge * @throws ContainerInitializationException */ public SpringLambdaContainerHandler(RequestReader requestReader, - ResponseWriter responseWriter, - SecurityContextWriter securityContextWriter, - ExceptionHandler exceptionHandler) + ResponseWriter responseWriter, + SecurityContextWriter securityContextWriter, + ExceptionHandler exceptionHandler, + ConfigurableWebApplicationContext applicationContext) throws ContainerInitializationException { super(requestReader, responseWriter, securityContextWriter, exceptionHandler); - initializer = new LambdaSpringApplicationInitializer(); - } - - /** - * Registers a set of classes with the underlying Spring application context - * @param config Spring annotated classes to be registered with the application context - */ - public void addConfiguration(Class... config) { - initializer.addConfigurationClasses(config); + initializer = new LambdaSpringApplicationInitializer(applicationContext); } @Override @@ -98,6 +89,7 @@ protected void handleRequest(AwsProxyHttpServletRequest containerRequest, AwsHtt initializer.onStartup(context); initialized = true; } + initializer.dispatch(containerRequest, containerResponse); } } diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java index aee11abf8..0d19e5884 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java @@ -1,6 +1,5 @@ package com.amazonaws.serverless.proxy.spring; -import com.amazonaws.serverless.exceptions.ContainerInitializationException; import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; @@ -12,36 +11,66 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.codec.binary.Base64; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.ConfigurableWebApplicationContext; import java.io.IOException; import java.util.UUID; import static org.junit.Assert.*; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; +import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; +import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; +import com.amazonaws.serverless.proxy.spring.echoapp.EchoSpringAppConfig; +import com.amazonaws.serverless.proxy.spring.echoapp.model.MapResponseModel; +import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel; +import com.amazonaws.services.lambda.runtime.Context; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.codec.binary.Base64; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.ConfigurableWebApplicationContext; -public class SpringAwsProxyTest { +import java.io.IOException; +import java.util.UUID; +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {EchoSpringAppConfig.class}) +@WebAppConfiguration +@TestExecutionListeners(inheritListeners = false, listeners = {DependencyInjectionTestExecutionListener.class}) +public class SpringAwsProxyTest { private static final String CUSTOM_HEADER_KEY = "x-custom-header"; private static final String CUSTOM_HEADER_VALUE = "my-custom-value"; private static final String AUTHORIZER_PRINCIPAL_ID = "test-principal-" + UUID.randomUUID().toString(); + @Autowired + private ObjectMapper objectMapper; - private static ObjectMapper objectMapper = new ObjectMapper(); - private static SpringLambdaContainerHandler handler = null; + @Autowired + private MockLambdaContext lambdaContext; - private static Context lambdaContext = new MockLambdaContext(); - - @BeforeClass - public static void init() { - try { - handler = SpringLambdaContainerHandler.getAwsProxyHandler(EchoSpringAppConfig.class); - } catch (ContainerInitializationException e) { - e.printStackTrace(); - fail(); - } - } + @Autowired + private SpringLambdaContainerHandler handler; @Test public void headers_getHeaders_echo() { @@ -188,3 +217,4 @@ private void validateSingleValueModel(AwsProxyResponse output, String value) { } } } + diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java index a8032a5fa..024f45ae3 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java @@ -1,9 +1,37 @@ package com.amazonaws.serverless.proxy.spring.echoapp; +import com.amazonaws.serverless.exceptions.ContainerInitializationException; +import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; +import com.amazonaws.serverless.proxy.spring.LambdaSpringApplicationInitializer; +import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler; +import com.amazonaws.services.lambda.runtime.Context; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.web.context.ConfigurableWebApplicationContext; @Configuration @ComponentScan("com.amazonaws.serverless.proxy.spring.echoapp") public class EchoSpringAppConfig { + + @Autowired + private ConfigurableWebApplicationContext applicationContext; + + @Bean + public SpringLambdaContainerHandler springLambdaContainerHandler() throws ContainerInitializationException { + return SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext); + } + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } + + @Bean + public MockLambdaContext lambdaContext() { + return new MockLambdaContext(); + } + } From 709ad2d56202c5e5882c361306a6b10f8505b67d Mon Sep 17 00:00:00 2001 From: joeyvmason Date: Mon, 16 Jan 2017 23:21:09 -0800 Subject: [PATCH 2/3] Added the ability to provide configuration classes, as well as applicationContext. Added the ability to specify whether or not applicationContext should be refreshed during initialization. This is necessary because if you are using the application context that is automatically generated when running Spring tests, calling refresh() on the application context is not allowed. --- .../LambdaSpringApplicationInitializer.java | 11 ++++- .../spring/SpringLambdacontainerHandler.java | 49 +++++++++++++++++-- .../spring/echoapp/EchoSpringAppConfig.java | 2 +- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java index f20a615ab..efb4882d3 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java @@ -45,6 +45,7 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial // Configuration variables that can be passed in private ConfigurableWebApplicationContext applicationContext; + private boolean refreshContext; private List contextListeners; // Dynamically instantiated properties @@ -57,10 +58,12 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial /** * Creates a new instance of the WebApplicationInitializer * @param applicationContext A custom ConfigurableWebApplicationContext to be used + * @param applicationContext Whether or not to refresh the applicationContext */ - public LambdaSpringApplicationInitializer(ConfigurableWebApplicationContext applicationContext) { + public LambdaSpringApplicationInitializer(ConfigurableWebApplicationContext applicationContext, boolean refreshContext) { this.contextListeners = new ArrayList<>(); this.applicationContext = applicationContext; + this.refreshContext = refreshContext; } /** @@ -104,7 +107,11 @@ public void onApplicationEvent(ServletRequestHandledEvent servletRequestHandledE // Register and map the dispatcher servlet dispatcherServlet = new DispatcherServlet(applicationContext); -// dispatcherServlet.refresh(); + + if (refreshContext) { + dispatcherServlet.refresh(); + } + dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(applicationContext)); dispatcherServlet.init(dispatcherConfig); diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java index 17c97f64b..51e6c717d 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java @@ -22,6 +22,7 @@ import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter; import com.amazonaws.services.lambda.runtime.Context; import org.springframework.web.context.ConfigurableWebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import javax.servlet.ServletContext; import java.util.concurrent.CountDownLatch; @@ -39,6 +40,25 @@ public class SpringLambdaContainerHandler extends Lam // State vars private boolean initialized; + /** + * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects + * @param config A set of classes annotated with the Spring @Configuration annotation + * @return An initialized instance of the `SpringLambdaContainerHandler` + * @throws ContainerInitializationException + */ + public static SpringLambdaContainerHandler getAwsProxyHandler(Class... config) throws ContainerInitializationException { + AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); + applicationContext.register(config); + + return new SpringLambdaContainerHandler<>( + new AwsProxyHttpServletRequestReader(), + new AwsProxyHttpServletResponseWriter(), + new AwsProxySecurityContextWriter(), + new AwsProxyExceptionHandler(), + applicationContext, + true + ); + } /** * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects @@ -46,14 +66,15 @@ public class SpringLambdaContainerHandler extends Lam * @return An initialized instance of the `SpringLambdaContainerHandler` * @throws ContainerInitializationException */ - public static SpringLambdaContainerHandler getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext) + public static SpringLambdaContainerHandler getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext, boolean refresh) throws ContainerInitializationException { return new SpringLambdaContainerHandler<>( new AwsProxyHttpServletRequestReader(), new AwsProxyHttpServletResponseWriter(), new AwsProxySecurityContextWriter(), new AwsProxyExceptionHandler(), - applicationContext + applicationContext, + refresh ); } @@ -73,7 +94,29 @@ public SpringLambdaContainerHandler(RequestReader requestReader, + ResponseWriter responseWriter, + SecurityContextWriter securityContextWriter, + ExceptionHandler exceptionHandler, + ConfigurableWebApplicationContext applicationContext, + boolean refreshContext) + throws ContainerInitializationException { + super(requestReader, responseWriter, securityContextWriter, exceptionHandler); + initializer = new LambdaSpringApplicationInitializer(applicationContext, refreshContext); } @Override diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java index 024f45ae3..2c9fa85c4 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java @@ -21,7 +21,7 @@ public class EchoSpringAppConfig { @Bean public SpringLambdaContainerHandler springLambdaContainerHandler() throws ContainerInitializationException { - return SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext); + return SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext, false); } @Bean From ef2f14560ebed7d657ce20b8c250802eb49641e1 Mon Sep 17 00:00:00 2001 From: joeyvmason Date: Tue, 17 Jan 2017 11:08:05 -0800 Subject: [PATCH 3/3] Do not require refreshContext variable in constructor of LambdaSpringApplicationInitializer and SpringLambdacontainerHandler. Added setter for refreshContext. Added Spring test dependencies to test scope --- aws-serverless-java-container-spring/pom.xml | 3 +- .../LambdaSpringApplicationInitializer.java | 10 +++--- .../spring/SpringLambdacontainerHandler.java | 32 ++++--------------- .../spring/echoapp/EchoSpringAppConfig.java | 4 ++- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/aws-serverless-java-container-spring/pom.xml b/aws-serverless-java-container-spring/pom.xml index befa28384..dbcb5b891 100644 --- a/aws-serverless-java-container-spring/pom.xml +++ b/aws-serverless-java-container-spring/pom.xml @@ -45,11 +45,12 @@ ${spring.version}
- + org.springframework spring-test ${spring.version} + test diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java index efb4882d3..3e0e6bd46 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java @@ -45,7 +45,7 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial // Configuration variables that can be passed in private ConfigurableWebApplicationContext applicationContext; - private boolean refreshContext; + private boolean refreshContext = true; private List contextListeners; // Dynamically instantiated properties @@ -58,12 +58,10 @@ public class LambdaSpringApplicationInitializer implements WebApplicationInitial /** * Creates a new instance of the WebApplicationInitializer * @param applicationContext A custom ConfigurableWebApplicationContext to be used - * @param applicationContext Whether or not to refresh the applicationContext */ - public LambdaSpringApplicationInitializer(ConfigurableWebApplicationContext applicationContext, boolean refreshContext) { + public LambdaSpringApplicationInitializer(ConfigurableWebApplicationContext applicationContext) { this.contextListeners = new ArrayList<>(); this.applicationContext = applicationContext; - this.refreshContext = refreshContext; } /** @@ -76,6 +74,10 @@ public void addListener(ServletContextListener listener) { contextListeners.add(listener); } + public void setRefreshContext(boolean refreshContext) { + this.refreshContext = refreshContext; + } + public void dispatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { currentResponse = response; diff --git a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java index 51e6c717d..29fa9a223 100644 --- a/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java +++ b/aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdacontainerHandler.java @@ -55,8 +55,7 @@ public static SpringLambdaContainerHandler ge new AwsProxyHttpServletResponseWriter(), new AwsProxySecurityContextWriter(), new AwsProxyExceptionHandler(), - applicationContext, - true + applicationContext ); } @@ -66,15 +65,14 @@ public static SpringLambdaContainerHandler ge * @return An initialized instance of the `SpringLambdaContainerHandler` * @throws ContainerInitializationException */ - public static SpringLambdaContainerHandler getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext, boolean refresh) + public static SpringLambdaContainerHandler getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext) throws ContainerInitializationException { return new SpringLambdaContainerHandler<>( new AwsProxyHttpServletRequestReader(), new AwsProxyHttpServletResponseWriter(), new AwsProxySecurityContextWriter(), new AwsProxyExceptionHandler(), - applicationContext, - refresh + applicationContext ); } @@ -94,29 +92,11 @@ public SpringLambdaContainerHandler(RequestReader requestReader, - ResponseWriter responseWriter, - SecurityContextWriter securityContextWriter, - ExceptionHandler exceptionHandler, - ConfigurableWebApplicationContext applicationContext, - boolean refreshContext) - throws ContainerInitializationException { - super(requestReader, responseWriter, securityContextWriter, exceptionHandler); - initializer = new LambdaSpringApplicationInitializer(applicationContext, refreshContext); + public void setRefreshContext(boolean refreshContext) { + this.initializer.setRefreshContext(refreshContext); } @Override diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java index 2c9fa85c4..53ace4766 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/EchoSpringAppConfig.java @@ -21,7 +21,9 @@ public class EchoSpringAppConfig { @Bean public SpringLambdaContainerHandler springLambdaContainerHandler() throws ContainerInitializationException { - return SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext, false); + SpringLambdaContainerHandler handler = SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext); + handler.setRefreshContext(false); + return handler; } @Bean