Skip to content

Commit

Permalink
fix: put and use app context as a servlet context attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Anisimov authored and mshabarov committed Mar 23, 2021
1 parent c1d083d commit bd2e12c
Showing 1 changed file with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -157,9 +163,7 @@ public void initialize(VaadinContext context,
@Override
protected Lookup createLookup(VaadinContext context,
Map<Class<?>, Collection<Class<?>>> services) {
WebApplicationContext appContext = WebApplicationContextUtils
.getWebApplicationContext(
((VaadinServletContext) context).getContext());
WebApplicationContext appContext = getApplicationContext(context);
return new SpringLookup(appContext,
(spi, impl) -> instantiate(appContext, spi, impl), services);
}
Expand All @@ -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> T instantiate(WebApplicationContext context,
Class<T> serviceClass, Class<?> impl) {
Collection<T> beans = context.getBeansOfType(serviceClass).values();
Expand Down

0 comments on commit bd2e12c

Please sign in to comment.