diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java index 23c714deb1..0ee888b4dd 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java @@ -405,16 +405,16 @@ public class TomcatEmbeddedServletContainerFactory private void configureSession(Context context) { long sessionTimeout = getSessionTimeoutInMinutes(); context.setSessionTimeout((int) sessionTimeout); - Manager manager = context.getManager(); - if (manager == null) { - manager = new StandardManager(); - context.setManager(manager); - } if (isPersistSession()) { + Manager manager = context.getManager(); + if (manager == null) { + manager = new StandardManager(); + context.setManager(manager); + } configurePersistSession(manager); } else { - disablePersistSession(manager); + context.addLifecycleListener(new DisablePersistSessionListener()); } } @@ -427,12 +427,6 @@ public class TomcatEmbeddedServletContainerFactory ((StandardManager) manager).setPathname(file.getAbsolutePath()); } - private void disablePersistSession(Manager manager) { - if (manager instanceof StandardManager) { - ((StandardManager) manager).setPathname(null); - } - } - private long getSessionTimeoutInMinutes() { long sessionTimeout = getSessionTimeout(); if (sessionTimeout > 0) { @@ -771,4 +765,24 @@ public class TomcatEmbeddedServletContainerFactory } + /** + * {@link LifecycleListener} to disable persistence in the {@link StandardManager}. A + * {@link LifecycleListener} is used so not to interfere with Tomcat's default manager + * creation logic. + */ + private static class DisablePersistSessionListener implements LifecycleListener { + + @Override + public void lifecycleEvent(LifecycleEvent event) { + if (event.getType().equals(Lifecycle.START_EVENT)) { + Context context = (Context) event.getLifecycle(); + Manager manager = context.getManager(); + if (manager != null && manager instanceof StandardManager) { + ((StandardManager) manager).setPathname(null); + } + } + } + + } + }