Skip to content

Commit

Permalink
Merge pull request #1169 from deki/sjc-904
Browse files Browse the repository at this point in the history
avoid ClassCastException for non-web application contexts (e.g. webflux)
  • Loading branch information
olegz authored Aug 8, 2024
2 parents a731b93 + 30d6ea9 commit 1686e24
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
}

0 comments on commit 1686e24

Please sign in to comment.