From 30d6ea9c219a58a24626e5451e9afafd860b92d9 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Thu, 1 Aug 2024 12:29:19 +0200 Subject: [PATCH] fix: avoid ClassCastException for non-web application contexts (e.g. webflux, see https://github.com/aws/serverless-java-container/issues/904) --- .../web/ServerlessAutoConfiguration.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java index b9c845f0d..dcef7bb27 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java +++ b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java @@ -22,7 +22,6 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServerException; import org.springframework.boot.web.servlet.ServletContextInitializer; @@ -53,7 +52,7 @@ public ServletWebServerFactory servletWebServerFactory() { public static class ServerlessServletWebServerFactory implements ServletWebServerFactory, ApplicationContextAware, InitializingBean { - private ConfigurableWebServerApplicationContext applicationContext; + private ApplicationContext applicationContext; @Override public WebServer getWebServer(ServletContextInitializer... initializers) { @@ -77,28 +76,31 @@ public int getPort() { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = (ConfigurableWebServerApplicationContext) applicationContext; + this.applicationContext = applicationContext; } @Override public void afterPropertiesSet() throws Exception { - if (applicationContext instanceof ServletWebServerApplicationContext servletApplicationContet) { + if (applicationContext instanceof ServletWebServerApplicationContext servletApplicationContext) { logger.info("Configuring Serverless Web Container"); ServerlessServletContext servletContext = new ServerlessServletContext(); - servletApplicationContet.setServletContext(servletContext); + servletApplicationContext.setServletContext(servletContext); DispatcherServlet dispatcher = applicationContext.getBean(DispatcherServlet.class); try { logger.info("Initializing DispatcherServlet"); - dispatcher.init(new ProxyServletConfig(servletApplicationContet.getServletContext())); - logger.info("Initalized DispatcherServlet"); + dispatcher.init(new ProxyServletConfig(servletApplicationContext.getServletContext())); + logger.info("Initialized DispatcherServlet"); } catch (Exception e) { - throw new IllegalStateException("Faild to create Spring MVC DispatcherServlet proxy", e); + throw new IllegalStateException("Failed to create Spring MVC DispatcherServlet proxy", e); } for (ServletContextInitializer initializer : new ServletContextInitializerBeans(this.applicationContext)) { initializer.onStartup(servletContext); } } + else { + logger.debug("Skipping Serverless configuration for " + this.applicationContext); + } } } }