diff --git a/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringLookupInitializer.java b/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringLookupInitializer.java index 07493aa4b..627303460 100644 --- a/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringLookupInitializer.java +++ b/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringLookupInitializer.java @@ -53,6 +53,10 @@ private static interface BootstrapCallable { void execute() throws ServletException; } + private static class ApplicationContextWrapper { + private WebApplicationContext appContext; + } + private static class SpringLookup extends LookupImpl { private final WebApplicationContext context; @@ -119,6 +123,9 @@ public void setApplicationContext(ApplicationContext applicationContext) .getAttribute(BootstrapCallable.class); vaadinServletContext .removeAttribute(BootstrapCallable.class); + ApplicationContextWrapper wrapper = new ApplicationContextWrapper(); + wrapper.appContext = (WebApplicationContext) applicationContext; + vaadinServletContext.setAttribute(wrapper); } } if (callable != null) { @@ -140,8 +147,7 @@ public void initialize(VaadinContext context, VaadinServletContext servletContext = (VaadinServletContext) context; boolean isContextAvailable = false; synchronized (LOCK) { - ApplicationContext appContext = WebApplicationContextUtils - .getWebApplicationContext(servletContext.getContext()); + ApplicationContext appContext = getApplicationContext(context); isContextAvailable = appContext != null; if (!isContextAvailable) { context.setAttribute(BootstrapCallable.class, @@ -157,9 +163,7 @@ public void initialize(VaadinContext context, @Override protected Lookup createLookup(VaadinContext context, Map, Collection>> services) { - WebApplicationContext appContext = WebApplicationContextUtils - .getWebApplicationContext( - ((VaadinServletContext) context).getContext()); + WebApplicationContext appContext = getApplicationContext(context); return new SpringLookup(appContext, (spi, impl) -> instantiate(appContext, spi, impl), services); } @@ -171,6 +175,21 @@ private void doInitialize(VaadinContext context, super.initialize(context, services, bootstrap); } + private WebApplicationContext getApplicationContext(VaadinContext context) { + VaadinServletContext servletContext = (VaadinServletContext) context; + WebApplicationContext appContext = WebApplicationContextUtils + .getWebApplicationContext(servletContext.getContext()); + if (appContext == null) { + // Spring behavior is always unbelievably surprising: under some + // circumstances {@code appContext} may be null even though the app + // context has been set via ApplicationContextAware: no idea WHY + ApplicationContextWrapper wrapper = context + .getAttribute(ApplicationContextWrapper.class); + appContext = wrapper == null ? null : wrapper.appContext; + } + return appContext; + } + private T instantiate(WebApplicationContext context, Class serviceClass, Class impl) { Collection beans = context.getBeansOfType(serviceClass).values();