Skip to content

Commit 30d6ea9

Browse files
committed
fix: avoid ClassCastException for non-web application contexts (e.g. webflux, see aws/serverless-java-container#904)
1 parent 90f7565 commit 30d6ea9

File tree

1 file changed

+10
-8
lines changed
  • spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web

1 file changed

+10
-8
lines changed

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.beans.BeansException;
2323
import org.springframework.beans.factory.InitializingBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25-
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
2625
import org.springframework.boot.web.server.WebServer;
2726
import org.springframework.boot.web.server.WebServerException;
2827
import org.springframework.boot.web.servlet.ServletContextInitializer;
@@ -53,7 +52,7 @@ public ServletWebServerFactory servletWebServerFactory() {
5352
public static class ServerlessServletWebServerFactory
5453
implements ServletWebServerFactory, ApplicationContextAware, InitializingBean {
5554

56-
private ConfigurableWebServerApplicationContext applicationContext;
55+
private ApplicationContext applicationContext;
5756

5857
@Override
5958
public WebServer getWebServer(ServletContextInitializer... initializers) {
@@ -77,28 +76,31 @@ public int getPort() {
7776

7877
@Override
7978
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
80-
this.applicationContext = (ConfigurableWebServerApplicationContext) applicationContext;
79+
this.applicationContext = applicationContext;
8180
}
8281

8382
@Override
8483
public void afterPropertiesSet() throws Exception {
85-
if (applicationContext instanceof ServletWebServerApplicationContext servletApplicationContet) {
84+
if (applicationContext instanceof ServletWebServerApplicationContext servletApplicationContext) {
8685
logger.info("Configuring Serverless Web Container");
8786
ServerlessServletContext servletContext = new ServerlessServletContext();
88-
servletApplicationContet.setServletContext(servletContext);
87+
servletApplicationContext.setServletContext(servletContext);
8988
DispatcherServlet dispatcher = applicationContext.getBean(DispatcherServlet.class);
9089
try {
9190
logger.info("Initializing DispatcherServlet");
92-
dispatcher.init(new ProxyServletConfig(servletApplicationContet.getServletContext()));
93-
logger.info("Initalized DispatcherServlet");
91+
dispatcher.init(new ProxyServletConfig(servletApplicationContext.getServletContext()));
92+
logger.info("Initialized DispatcherServlet");
9493
}
9594
catch (Exception e) {
96-
throw new IllegalStateException("Faild to create Spring MVC DispatcherServlet proxy", e);
95+
throw new IllegalStateException("Failed to create Spring MVC DispatcherServlet proxy", e);
9796
}
9897
for (ServletContextInitializer initializer : new ServletContextInitializerBeans(this.applicationContext)) {
9998
initializer.onStartup(servletContext);
10099
}
101100
}
101+
else {
102+
logger.debug("Skipping Serverless configuration for " + this.applicationContext);
103+
}
102104
}
103105
}
104106
}

0 commit comments

Comments
 (0)