Lukasz Lenart created WW-5851:
---------------------------------
Summary: Spring plugin closes the class-reloading context before
using it, so the documented hot-reload setup fails at startup
Key: WW-5851
URL: https://issues.apache.org/jira/browse/WW-5851
Project: Struts 2
Issue Type: Bug
Components: Plugin - Spring
Affects Versions: 7.4.0, 6.12.0
Reporter: Lukasz Lenart
Fix For: 7.6.0
The class-reloading feature of the Spring plugin cannot start. With
{{struts.devMode=true}}, a non-empty
{{struts.objectFactory.spring.class.reloading.watchList}} and
{{org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext}} configured
as the root web application context (the setup described on the plugin page),
{{StrutsSpringObjectFactory}} throws during construction and Struts-Spring
integration never initializes.
h2. What happens
{{StrutsSpringObjectFactory}} (constructor, lines 107-120 on {{main}}) wraps
the reloading context in a try-with-resources block:
{code:java}
try (ClassReloadingXMLWebApplicationContext reloadingContext =
(ClassReloadingXMLWebApplicationContext) appContext) {
reloadingContext.setupReloading(...);
setClassLoader(reloadingContext.getReloadingClassLoader());
reloadingContext.refresh();
}
this.setApplicationContext(appContext);
{code}
Leaving the block calls {{ClassReloadingXMLWebApplicationContext.close()}},
which closes the Spring context and stops the JCI
{{FilesystemAlterationMonitor}} that {{setupReloading}} has just started. The
next line hands the now-closed context to
{{SpringObjectFactory.setApplicationContext}}, whose
{{findAutoWiringBeanFactory}} calls {{getBeanFactory()}} on it. Spring rejects
that:
{code}
java.lang.IllegalStateException: BeanFactory not initialized or already closed
- call 'refresh' before accessing beans via the ApplicationContext
{code}
Catching the exception would not be enough: the monitor thread is already
stopped, so no file change would ever trigger a reload.
The try-with-resources block was introduced in commit {{176a0d775}}
(2018-11-19, "Fix: Resources should be closed") and first shipped in 6.0.0. The
code is identical on {{main}} and {{support/struts-6-x-x}}. Spring 6.2.x and
7.0.x behave the same.
h2. Reproduction
A unit test in the Spring plugin module that:
# creates a {{ClassReloadingXMLWebApplicationContext}}, registers it as the
root context attribute on a {{MockServletContext}}, points it at
{{emptyContext-spring.xml}} and refreshes it;
# binds an {{ActionContext}} whose container returns a temp directory for the
watch-list key and a {{FileManagerFactory}} (the reloading class loader needs
one);
# constructs {{StrutsSpringObjectFactory}} with {{devMode}} set to {{"true"}}.
Step 3 throws the {{IllegalStateException}} above and the context's
{{isActive()}} is false afterwards. Two controls pass: the same setup with
{{devMode}} set to {{"false"}}, and {{devMode}} set to {{"true"}} with an empty
watch list. Both start and leave the context active.
h2. Fix
Do not close the reloading context in the constructor. The context is owned by
Spring's {{ContextLoaderListener}}, which closes it on servlet context
destruction. Replace the try-with-resources block with a plain block (or a
plain cast) and keep the {{refresh()}} call. Add the reproduction above as a
regression test so the reloading branch is covered; today no test exercises it.
h2. Related documentation issue
The plugin page on the site still documents the keys as
{{struts.class.reloading.watchList}}, {{struts.class.reloading.acceptClasses}}
and {{struts.class.reloading.reloadConfig}}. The plugin has read
{{struts.objectFactory.spring.class.reloading.*}} since commit {{f85149791}}
(2018-05-28). A user following the page literally sets keys the plugin never
reads, so the reloading branch is skipped entirely. The page needs the current
key names; that is a struts-site change and can be tracked separately or
alongside this ticket.
h2. Credit
Reported publicly on the Full Disclosure list, 2026-10-31:
https://seclists.org/fulldisclosure/2026/Nov/5
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)