Skip to content

Commit

Permalink
feat(session sharing): improve contexts hierarchy
Browse files Browse the repository at this point in the history
* improve Spring contexts hierarchy and in order to have a true hierachy between web context an engine context and not just shared beans
* add web package to componentscan for spring session in web context only (not engine)

Relates to [DEV-324](https://bonitasoft.atlassian.net/browse/DEV-324)
  • Loading branch information
abirembaut committed Sep 5, 2023
1 parent 9beaa40 commit 2cc5233
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import org.bonitasoft.platform.setup.PlatformSetupAccessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class EngineInitializerListener implements ServletContextListener {

Expand All @@ -40,20 +39,25 @@ public void contextInitialized(final ServletContextEvent event) {
.createServiceAccessor()
.getContext();

GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext(
new DefaultListableBeanFactory(engineContext), event.getServletContext());

AnnotationConfigWebApplicationContext webApplicationContext = initializeWebContext(event, engineContext);
webApplicationContext.refresh();

//A web application context needs to be referenced in the Servlet context so that servlet and filters beans handled by Spring can use it
event.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
webApplicationContext);
} catch (final Throwable e) {
throw new RuntimeException("Error while initializing the Engine", e);
}

}

protected AnnotationConfigWebApplicationContext initializeWebContext(ServletContextEvent event,
ApplicationContext engineContext) {
AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
webApplicationContext.setParent(engineContext);
webApplicationContext.setServletContext(event.getServletContext());
//A web application context needs to be referenced in the Servlet context so that servlet and filters beans handled by Spring web can use it
event.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
webApplicationContext);
return webApplicationContext;
}

protected EngineInitializer getEngineInitializer() {
return new EngineInitializer();
}
Expand Down

0 comments on commit 2cc5233

Please sign in to comment.