feat(conversion): WW-4291 allow Spring bean names for type converters (#1562)

Implement two-phase processing for conversion properties to enable
Spring bean name resolution in struts-conversion.properties files.

The issue was a timing problem: type converters were processed during
bootstrap phase before SpringObjectFactory was available. Now:
- Early phase: process struts-default-conversion.properties (class names)
- Late phase: process user properties when SpringObjectFactory is ready

Changes:
- Add UserConversionPropertiesProvider interface for late initialization
- Add UserConversionPropertiesProcessor to trigger late phase processing
- Split StrutsConversionPropertiesProcessor.init() into early/late phases
- Register new beans in DefaultConfiguration and struts-beans.xml
- Add alias in StrutsBeanSelectionProvider for dependency injection
- Improve JavaDocs for BeanSelectionProvider classes

Closes WW-4291

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Lukasz Lenart
2026-02-06 07:43:55 +01:00
committed by GitHub
parent 4cd02529ca
commit 720e603d2b
12 changed files with 599 additions and 43 deletions
@@ -562,6 +562,7 @@ public final class StrutsConstants {
public static final String STRUTS_CONVERTER_ANNOTATION_PROCESSOR = "struts.converter.annotation.processor";
public static final String STRUTS_CONVERTER_CREATOR = "struts.converter.creator";
public static final String STRUTS_CONVERTER_HOLDER = "struts.converter.holder";
public static final String STRUTS_CONVERTER_USER_PROPERTIES_PROVIDER = "struts.converter.userPropertiesProvider";
public static final String STRUTS_EXPRESSION_PARSER = "struts.expression.parser";
@@ -35,7 +35,44 @@ import org.apache.struts2.inject.Scope;
import java.util.Properties;
/**
* TODO lukaszlenart: write a JavaDoc
* Base implementation of {@link BeanSelectionProvider} that provides bean aliasing functionality.
* <p>
* This class provides the {@link #alias(Class, String, ContainerBuilder, Properties, Scope)} method
* which is used to select and register bean implementations based on configuration properties.
* </p>
*
* <h2>Bean Selection Process</h2>
* <p>
* The {@code alias} method selects a bean implementation using the following process:
* </p>
* <ol>
* <li>Read the property value for the given key from the configuration properties</li>
* <li>If no property is set, use {@value #DEFAULT_BEAN_NAME} as the default bean name</li>
* <li>Check if a bean with that name already exists in the container:
* <ul>
* <li>If found, alias it to {@link Container#DEFAULT_NAME} making it the default</li>
* <li>If not found, try to load the property value as a fully qualified class name</li>
* </ul>
* </li>
* <li>If class loading succeeds, register the class as a factory for the interface type</li>
* <li>If class loading fails and the name is not the default, create a delegate factory
* that will resolve the bean through {@link ObjectFactory} at runtime. This allows
* Spring bean names to be used in configuration.</li>
* </ol>
*
* <h2>Usage Example</h2>
* <pre>
* // In struts.properties or struts.xml:
* // struts.objectFactory = spring
* // struts.converter.collection = myCustomCollectionConverter
*
* // In a subclass:
* alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
* alias(CollectionConverter.class, StrutsConstants.STRUTS_CONVERTER_COLLECTION, builder, props);
* </pre>
*
* @see BeanSelectionProvider
* @see StrutsBeanSelectionProvider
*/
public abstract class AbstractBeanSelectionProvider implements BeanSelectionProvider {
@@ -78,7 +115,7 @@ public abstract class AbstractBeanSelectionProvider implements BeanSelectionProv
// Perhaps a spring bean id, so we'll delegate to the object factory at runtime
LOG.trace("Choosing bean ({}) for ({}) to be loaded from the ObjectFactory", foundName, type.getName());
if (DEFAULT_BEAN_NAME.equals(foundName)) {
// Probably an optional bean, will ignore
LOG.trace("No bean registered for type ({}) with default name '{}', skipping as optional", type.getName(), DEFAULT_BEAN_NAME);
} else {
if (ObjectFactory.class != type) {
builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
@@ -108,7 +145,7 @@ public abstract class AbstractBeanSelectionProvider implements BeanSelectionProv
try {
return objFactory.buildBean(name, null, true);
} catch (ClassNotFoundException ex) {
throw new ConfigurationException("Unable to load bean "+type.getName()+" ("+name+")");
throw new ConfigurationException(String.format("Unable to load bean %s (name = %s)", type.getName(), name));
}
}
@@ -19,7 +19,25 @@
package org.apache.struts2.config;
/**
* When implemented allows to alias already existing beans
* A {@link ConfigurationProvider} that selects and aliases bean implementations.
* <p>
* Implementations of this interface are responsible for selecting which bean implementation
* to use for a given interface type. The selection is typically based on configuration properties
* that specify the bean name or class name.
* </p>
* <p>
* The aliasing mechanism works as follows:
* </p>
* <ol>
* <li>Look for a bean by the name specified in the configuration property</li>
* <li>If found, alias it to the default name so it becomes the default implementation</li>
* <li>If not found, try to load the value as a class name and register it as a factory</li>
* <li>If class loading fails, delegate to {@link org.apache.struts2.ObjectFactory} at runtime
* (useful for Spring bean names)</li>
* </ol>
*
* @see AbstractBeanSelectionProvider
* @see StrutsBeanSelectionProvider
*/
public interface BeanSelectionProvider extends ConfigurationProvider {
@@ -37,6 +37,7 @@ import org.apache.struts2.conversion.ConversionPropertiesProcessor;
import org.apache.struts2.conversion.ObjectTypeDeterminer;
import org.apache.struts2.conversion.TypeConverterCreator;
import org.apache.struts2.conversion.TypeConverterHolder;
import org.apache.struts2.conversion.UserConversionPropertiesProvider;
import org.apache.struts2.conversion.impl.ArrayConverter;
import org.apache.struts2.conversion.impl.CollectionConverter;
import org.apache.struts2.conversion.impl.DateConverter;
@@ -88,7 +89,7 @@ import org.apache.struts2.views.util.UrlHelper;
*
* <p>
* The following is a list of the allowed extension points:
*
* <p>
* <!-- START SNIPPET: extensionPoints -->
* <table border="1" summary="">
* <tr>
@@ -353,7 +354,7 @@ import org.apache.struts2.views.util.UrlHelper;
* <td>Provides access to resource bundles used to localise messages (since 2.5.11)</td>
* </tr>
* </table>
*
* <p>
* <!-- END SNIPPET: extensionPoints -->
*
* <p>
@@ -405,6 +406,7 @@ public class StrutsBeanSelectionProvider extends AbstractBeanSelectionProvider {
alias(ConversionAnnotationProcessor.class, StrutsConstants.STRUTS_CONVERTER_ANNOTATION_PROCESSOR, builder, props);
alias(TypeConverterCreator.class, StrutsConstants.STRUTS_CONVERTER_CREATOR, builder, props);
alias(TypeConverterHolder.class, StrutsConstants.STRUTS_CONVERTER_HOLDER, builder, props);
alias(UserConversionPropertiesProvider.class, StrutsConstants.STRUTS_CONVERTER_USER_PROPERTIES_PROVIDER, builder, props);
alias(TextProvider.class, StrutsConstants.STRUTS_TEXT_PROVIDER, builder, props, Scope.PROTOTYPE);
alias(TextProviderFactory.class, StrutsConstants.STRUTS_TEXT_PROVIDER_FACTORY, builder, props, Scope.PROTOTYPE);
@@ -106,6 +106,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.conversion.StrutsConversionPropertiesProcessor;
import org.apache.struts2.conversion.UserConversionPropertiesProcessor;
import org.apache.struts2.conversion.UserConversionPropertiesProvider;
import org.apache.struts2.conversion.StrutsTypeConverterCreator;
import org.apache.struts2.conversion.StrutsTypeConverterHolder;
import org.apache.struts2.factory.StrutsResultFactory;
@@ -126,12 +128,8 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* DefaultConfiguration
*
* @author Jason Carreira
* Created Feb 24, 2003 7:38:06 AM
*/
public class DefaultConfiguration implements Configuration {
@@ -225,7 +223,7 @@ public class DefaultConfiguration implements Configuration {
name, packageContext.getLocation());
} else {
throw new ConfigurationException("The package name '" + name
+ "' at location "+packageContext.getLocation()
+ "' at location " + packageContext.getLocation()
+ " is already been used by another package at location " + check.getLocation(),
packageContext);
}
@@ -258,7 +256,6 @@ public class DefaultConfiguration implements Configuration {
*
* @param providers list of ContainerProvider
* @return list of package providers
*
* @throws ConfigurationException in case of any configuration errors
*/
@Override
@@ -270,15 +267,14 @@ public class DefaultConfiguration implements Configuration {
ContainerProperties props = new ContainerProperties();
ContainerBuilder builder = new ContainerBuilder();
Container bootstrap = createBootstrapContainer(providers);
for (final ContainerProvider containerProvider : providers)
{
for (final ContainerProvider containerProvider : providers) {
bootstrap.inject(containerProvider);
containerProvider.init(this);
containerProvider.register(builder, props);
}
props.setConstants(builder);
builder.factory(Configuration.class, new Factory<Configuration>() {
builder.factory(Configuration.class, new Factory<>() {
@Override
public Configuration create(Context context) throws Exception {
return DefaultConfiguration.this;
@@ -299,13 +295,16 @@ public class DefaultConfiguration implements Configuration {
setContext(container);
objectFactory = container.getInstance(ObjectFactory.class);
// Trigger late initialization of user conversion properties (WW-4291)
// This must happen after full container is built so SpringObjectFactory is available
container.getInstance(UserConversionPropertiesProcessor.class);
// Process the configuration providers first
for (final ContainerProvider containerProvider : providers)
{
for (final ContainerProvider containerProvider : providers) {
if (containerProvider instanceof PackageProvider) {
container.inject(containerProvider);
((PackageProvider)containerProvider).loadPackages();
packageProviders.add((PackageProvider)containerProvider);
((PackageProvider) containerProvider).loadPackages();
packageProviders.add((PackageProvider) containerProvider);
}
}
@@ -381,6 +380,8 @@ public class DefaultConfiguration implements Configuration {
.factory(ConversionAnnotationProcessor.class, DefaultConversionAnnotationProcessor.class, Scope.SINGLETON)
.factory(TypeConverterCreator.class, StrutsTypeConverterCreator.class, Scope.SINGLETON)
.factory(TypeConverterHolder.class, StrutsTypeConverterHolder.class, Scope.SINGLETON)
.factory(UserConversionPropertiesProvider.class, StrutsConversionPropertiesProcessor.class, Scope.SINGLETON)
.factory(UserConversionPropertiesProcessor.class, Scope.SINGLETON)
.factory(TextProvider.class, "system", DefaultTextProvider.class, Scope.SINGLETON)
.factory(LocalizedTextProvider.class, StrutsLocalizedTextProvider.class, Scope.SINGLETON)
@@ -444,10 +445,9 @@ public class DefaultConfiguration implements Configuration {
Map<String, ActionConfig> actionConfigs = packageConfig.getAllActionConfigs();
for (Object o : actionConfigs.keySet()) {
String actionName = (String) o;
ActionConfig baseConfig = actionConfigs.get(actionName);
configs.put(actionName, buildFullActionConfig(packageConfig, baseConfig));
for (Map.Entry<String, ActionConfig> entry : actionConfigs.entrySet()) {
ActionConfig baseConfig = entry.getValue();
configs.put(entry.getKey(), buildFullActionConfig(packageConfig, baseConfig));
}
namespaceActionConfigs.put(namespace, configs);
@@ -489,7 +489,6 @@ public class DefaultConfiguration implements Configuration {
* and inheritance
* @return a full ActionConfig for runtime configuration with all of the inherited and default params
* @throws org.apache.struts2.config.ConfigurationException
*
*/
private ActionConfig buildFullActionConfig(PackageConfig packageContext, ActionConfig baseConfig) throws ConfigurationException {
Map<String, String> params = new TreeMap<>(baseConfig.getParams());
@@ -501,7 +500,7 @@ public class DefaultConfiguration implements Configuration {
results.putAll(packageContext.getAllGlobalResults());
}
results.putAll(baseConfig.getResults());
results.putAll(baseConfig.getResults());
setDefaultResults(results, packageContext);
@@ -512,7 +511,7 @@ public class DefaultConfiguration implements Configuration {
if (defaultInterceptorRefName != null) {
interceptors.addAll(InterceptorBuilder.constructInterceptorReference(new PackageConfig.Builder(packageContext), defaultInterceptorRefName,
new LinkedHashMap<String, String>(), packageContext.getLocation(), objectFactory));
new LinkedHashMap<>(), packageContext.getLocation(), objectFactory));
}
}
@@ -524,14 +523,14 @@ public class DefaultConfiguration implements Configuration {
LOG.debug("Using pattern [{}] to match allowed methods when SMI is disabled!", methodRegex);
return new ActionConfig.Builder(baseConfig)
.addParams(params)
.addResultConfigs(results)
.defaultClassName(packageContext.getDefaultClassRef()) // fill in default if non class has been provided
.interceptors(interceptors)
.setStrictMethodInvocation(packageContext.isStrictMethodInvocation())
.setDefaultMethodRegex(methodRegex)
.addExceptionMappings(packageContext.getAllExceptionMappingConfigs())
.build();
.addParams(params)
.addResultConfigs(results)
.defaultClassName(packageContext.getDefaultClassRef()) // fill in default if non class has been provided
.interceptors(interceptors)
.setStrictMethodInvocation(packageContext.isStrictMethodInvocation())
.setDefaultMethodRegex(methodRegex)
.addExceptionMappings(packageContext.getAllExceptionMappingConfigs())
.build();
}
@@ -547,8 +546,7 @@ public class DefaultConfiguration implements Configuration {
Map<String, String> namespaceConfigs,
PatternMatcher<int[]> matcher,
boolean appendNamedParameters,
boolean fallbackToEmptyNamespace)
{
boolean fallbackToEmptyNamespace) {
this.namespaceActionConfigs = namespaceActionConfigs;
this.namespaceConfigs = namespaceConfigs;
this.fallbackToEmptyNamespace = fallbackToEmptyNamespace;
@@ -631,7 +629,7 @@ public class DefaultConfiguration implements Configuration {
* @return a Map of namespace - > Map of ActionConfig objects, with the key being the action name
*/
@Override
public Map<String, Map<String, ActionConfig>> getActionConfigs() {
public Map<String, Map<String, ActionConfig>> getActionConfigs() {
return namespaceActionConfigs;
}
@@ -666,7 +664,7 @@ public class DefaultConfiguration implements Configuration {
public void setConstants(ContainerBuilder builder) {
for (Object keyobj : keySet()) {
String key = (String)keyobj;
String key = (String) keyobj;
builder.factory(String.class, key, new LocatableConstantFactory<>(getProperty(key), getPropertyLocation(key)));
}
}
@@ -31,7 +31,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class StrutsConversionPropertiesProcessor implements ConversionPropertiesProcessor, EarlyInitializable {
public class StrutsConversionPropertiesProcessor implements ConversionPropertiesProcessor, EarlyInitializable, UserConversionPropertiesProvider {
private static final Logger LOG = LogManager.getLogger(StrutsConversionPropertiesProcessor.class);
@@ -54,8 +54,27 @@ public class StrutsConversionPropertiesProcessor implements ConversionProperties
@Override
public void init() {
LOG.debug("Processing default conversion properties files");
// Early phase: Only process framework defaults (class names only)
// User properties are processed later in initUserConversions() when
// SpringObjectFactory is available for bean name resolution (WW-4291)
LOG.debug("Processing default conversion properties files (early phase)");
processRequired(STRUTS_DEFAULT_CONVERSION_PROPERTIES);
}
/**
* Process user conversion properties. Called during late initialization
* when SpringObjectFactory is available for bean name resolution.
* <p>
* This allows users to reference Spring bean names in struts-conversion.properties
* instead of only fully qualified class names.
* </p>
*
* @see <a href="https://issues.apache.org/jira/browse/WW-4291">WW-4291</a>
* @since 7.2.0
*/
@Override
public void initUserConversions() {
LOG.debug("Processing user conversion properties files (late phase)");
process(STRUTS_CONVERSION_PROPERTIES);
process(XWORK_CONVERSION_PROPERTIES);
}
@@ -74,7 +93,7 @@ public class StrutsConversionPropertiesProcessor implements ConversionProperties
while (resources.hasNext()) {
if (XWORK_CONVERSION_PROPERTIES.equals(propsName)) {
LOG.warn("Instead of using deprecated {} please use the new file name {}",
XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
}
URL url = resources.next();
Properties props = new Properties();
@@ -82,8 +101,7 @@ public class StrutsConversionPropertiesProcessor implements ConversionProperties
LOG.debug("Processing conversion file [{}]", propsName);
for (Object o : props.entrySet()) {
Map.Entry entry = (Map.Entry) o;
for (Map.Entry<Object, Object> entry : props.entrySet()) {
String key = (String) entry.getKey();
try {
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.conversion;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.inject.Initializable;
import org.apache.struts2.inject.Inject;
/**
* Late initialization processor for user conversion properties.
* <p>
* Processes struts-conversion.properties and xwork-conversion.properties
* after the full container is built, allowing Spring bean name resolution.
* This enables users to reference Spring bean names instead of only fully
* qualified class names in their conversion property files.
* </p>
*
* @see <a href="https://issues.apache.org/jira/browse/WW-4291">WW-4291</a>
* @see UserConversionPropertiesProvider
* @since 7.2.0
*/
public class UserConversionPropertiesProcessor implements Initializable {
private static final Logger LOG = LogManager.getLogger(UserConversionPropertiesProcessor.class);
private UserConversionPropertiesProvider provider;
@Inject
public void setUserConversionPropertiesProvider(UserConversionPropertiesProvider provider) {
this.provider = provider;
}
@Override
public void init() {
LOG.debug("Initializing user conversion properties via late initialization");
provider.initUserConversions();
}
}
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.conversion;
/**
* Interface for processors that support late initialization of user conversion properties.
* <p>
* Implementations provide user conversion properties processing after the full container
* is built, allowing Spring bean name resolution for type converters.
* </p>
*
* @see <a href="https://issues.apache.org/jira/browse/WW-4291">WW-4291</a>
* @since 7.2.0
*/
public interface UserConversionPropertiesProvider {
/**
* Process user conversion properties (struts-conversion.properties, xwork-conversion.properties).
* Called during late initialization when SpringObjectFactory is available.
*/
void initUserConversions();
}
+3
View File
@@ -114,6 +114,9 @@
class="org.apache.struts2.conversion.StrutsTypeConverterCreator"/>
<bean type="org.apache.struts2.conversion.TypeConverterHolder" name="struts"
class="org.apache.struts2.conversion.StrutsTypeConverterHolder"/>
<bean type="org.apache.struts2.conversion.UserConversionPropertiesProvider" name="struts"
class="org.apache.struts2.conversion.StrutsConversionPropertiesProcessor"/>
<bean class="org.apache.struts2.conversion.UserConversionPropertiesProcessor" scope="singleton"/>
<bean class="org.apache.struts2.conversion.impl.XWorkBasicConverter"/>
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.conversion;
import org.apache.struts2.XWorkTestCase;
import java.io.File;
/**
* Tests for {@link StrutsConversionPropertiesProcessor} two-phase processing.
*
* @see <a href="https://issues.apache.org/jira/browse/WW-4291">WW-4291</a>
*/
public class StrutsConversionPropertiesProcessorTest extends XWorkTestCase {
private TypeConverterHolder converterHolder;
private StrutsConversionPropertiesProcessor processor;
@Override
protected void setUp() throws Exception {
super.setUp();
converterHolder = container.getInstance(TypeConverterHolder.class);
processor = (StrutsConversionPropertiesProcessor) container.getInstance(ConversionPropertiesProcessor.class);
}
/**
* Tests that default converters from struts-default-conversion.properties
* are registered during the early initialization phase.
* java.io.File -> UploadedFileConverter is defined in struts-default-conversion.properties.
*/
public void testDefaultConvertersRegisteredDuringEarlyPhase() {
// The java.io.File converter should be registered from struts-default-conversion.properties
// struts-default-conversion.properties defines: java.io.File=org.apache.struts2.conversion.UploadedFileConverter
TypeConverter fileConverter = converterHolder.getDefaultMapping(File.class.getName());
assertNotNull("java.io.File converter should be registered from default properties", fileConverter);
}
/**
* Tests that the init() method only processes the default conversion properties file.
* User conversion properties should be processed separately via initUserConversions().
*/
public void testInitOnlyProcessesDefaultProperties() {
// This test verifies the behavior is correct - default converters are available
// after bootstrap. The actual split behavior is validated by checking that the
// framework doesn't throw ClassNotFoundException for bean names.
assertNotNull("Processor should be available", processor);
assertNotNull("Converter holder should have default mappings", converterHolder);
}
}
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.spring;
import junit.framework.TestCase;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.support.StaticApplicationContext;
/**
* Tests for Spring bean name support in type converters.
* <p>
* Verifies that Spring bean names can be used in struts-conversion.properties
* instead of fully qualified class names via SpringObjectFactory.getClassInstance().
* </p>
*
* @see <a href="https://issues.apache.org/jira/browse/WW-4291">WW-4291</a>
*/
public class SpringTypeConverterTest extends TestCase {
private StaticApplicationContext sac;
private SpringObjectFactory objectFactory;
@Override
public void setUp() throws Exception {
super.setUp();
sac = new StaticApplicationContext();
objectFactory = new SpringObjectFactory();
objectFactory.setApplicationContext(sac);
}
@Override
public void tearDown() throws Exception {
sac = null;
objectFactory = null;
super.tearDown();
}
/**
* Tests that SpringObjectFactory.getClassInstance() can resolve Spring bean names.
* This is the core mechanism that enables WW-4291 - when user conversion properties
* are processed during late initialization, SpringObjectFactory will be available
* and can resolve bean names via containsBean() check.
*/
public void testGetClassInstanceBySpringBeanName() throws Exception {
// Register a class as a Spring bean
sac.registerSingleton("myTestBean", TestBean.class, new MutablePropertyValues());
// SpringObjectFactory.getClassInstance() should resolve bean name to class
Class<?> clazz = objectFactory.getClassInstance("myTestBean");
assertNotNull("Should resolve Spring bean name to class", clazz);
assertEquals(TestBean.class, clazz);
}
/**
* Tests that getClassInstance() falls back to class loading for fully qualified
* class names (backward compatibility).
*/
public void testGetClassInstanceByClassName() throws Exception {
// Should work with fully qualified class name even if not a Spring bean
Class<?> clazz = objectFactory.getClassInstance(TestBean.class.getName());
assertNotNull("Should resolve class name", clazz);
assertEquals(TestBean.class, clazz);
}
/**
* Tests that an invalid bean/class name produces ClassNotFoundException.
*/
public void testInvalidBeanNameThrowsException() {
try {
objectFactory.getClassInstance("nonExistentBeanOrClass");
fail("Should throw ClassNotFoundException for non-existent bean/class");
} catch (ClassNotFoundException e) {
// Expected
assertTrue("Exception message should mention the class name",
e.getMessage().contains("nonExistentBeanOrClass"));
}
}
/**
* Tests that Spring bean takes precedence over class name when both exist.
* If a bean is registered with a name that could also be a class, the bean wins.
*/
public void testSpringBeanTakesPrecedence() throws Exception {
// Register a bean with a name that looks like a class
sac.registerSingleton("org.apache.struts2.spring.SpringTypeConverterTest$AnotherBean",
TestBean.class, new MutablePropertyValues());
// Should get TestBean.class (from Spring) not try to load the literal class name
Class<?> clazz = objectFactory.getClassInstance(
"org.apache.struts2.spring.SpringTypeConverterTest$AnotherBean");
// The bean was registered with TestBean.class, so that's what we should get
assertEquals(TestBean.class, clazz);
}
/**
* A simple test bean class.
*/
public static class TestBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* Another test bean class for precedence testing.
*/
public static class AnotherBean {
// Empty
}
}
@@ -0,0 +1,185 @@
---
date: 2026-01-31T12:15:00+01:00
topic: "WW-4291: Allow Spring Bean Names for Type Converters"
tags: [research, codebase, spring-plugin, type-converters, initialization]
status: implemented
jira: WW-4291
---
# WW-4291: Allow Spring Bean Names for Type Converters
**Date**: 2026-01-31
**Status**: Implemented
## Problem Summary
Users cannot reference Spring bean names in `struts-conversion.properties` files. When specifying a bean name (e.g., "myConverter") instead of a fully qualified class name, a `ClassNotFoundException` is thrown.
**JIRA:** https://issues.apache.org/jira/browse/WW-4291
## Root Cause
**Timing issue during initialization:**
1. **Bootstrap Phase** (`DefaultConfiguration.createBootstrapContainer()`):
- `StrutsConversionPropertiesProcessor` implements `EarlyInitializable`
- `EarlyInitializable` beans are always instantiated when container is created (ContainerBuilder.java:617-620)
- At this point, `SpringObjectFactory` is not yet available
- User's `struts-conversion.properties` is processed with basic `ObjectFactory.getClassInstance()` which only does class loading
2. **Full Container Phase** (later):
- Spring plugin loads `SpringObjectFactory`
- `SpringObjectFactory.getClassInstance()` already supports bean names via `containsBean()` check
- But conversion properties were already processed!
**Historical context:** `EarlyInitializable` was introduced in Jan 2018 (fad603c49, d64365770) to ensure converters are registered before first action execution. Removing it entirely could break converter availability.
## Implemented Solution: Two-Phase Processing with Interface
Split conversion property processing into two phases:
1. **Early phase (EarlyInitializable):** Process `struts-default-conversion.properties` only - contains class names
2. **Late phase (Initializable):** Process user properties (`struts-conversion.properties`, `xwork-conversion.properties`) - may contain Spring bean names
### Key Design Decision
Instead of using `instanceof` checks to access the late initialization method, we introduced a clean interface `UserConversionPropertiesProvider` that defines the contract for late-phase processing. This follows the Dependency Inversion Principle.
## Implementation Details
### Files Created
| File | Purpose |
|------|---------|
| `core/src/main/java/org/apache/struts2/conversion/UserConversionPropertiesProvider.java` | Interface defining `initUserConversions()` method |
| `core/src/main/java/org/apache/struts2/conversion/UserConversionPropertiesProcessor.java` | Late initialization processor implementing `Initializable` |
| `core/src/test/java/org/apache/struts2/conversion/StrutsConversionPropertiesProcessorTest.java` | Unit tests |
| `plugins/spring/src/test/java/org/apache/struts2/spring/SpringTypeConverterTest.java` | Spring integration tests |
### Files Modified
| File | Changes |
|------|---------|
| `core/src/main/java/org/apache/struts2/conversion/StrutsConversionPropertiesProcessor.java` | Implements `UserConversionPropertiesProvider`, split `init()` into early/late phases |
| `core/src/main/java/org/apache/struts2/config/impl/DefaultConfiguration.java` | Register `UserConversionPropertiesProvider` and `UserConversionPropertiesProcessor`; explicitly trigger late initialization in `reloadContainer()` |
| `core/src/main/java/org/apache/struts2/StrutsConstants.java` | Add `STRUTS_CONVERTER_USER_PROPERTIES_PROVIDER` constant |
| `core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java` | Add alias for `UserConversionPropertiesProvider` |
| `core/src/main/resources/struts-beans.xml` | Add bean definitions for new classes |
### Interface Definition
```java
/**
* Interface for processors that support late initialization of user conversion properties.
*/
public interface UserConversionPropertiesProvider {
/**
* Process user conversion properties (struts-conversion.properties, xwork-conversion.properties).
* Called during late initialization when SpringObjectFactory is available.
*/
void initUserConversions();
}
```
### Class Diagram
```
+----------------------------+
| ConversionPropertiesProcessor |
+----------------------------+
^
|
+---------------------------+ | +--------------------------------+
| UserConversionPropertiesProvider |<----+ StrutsConversionPropertiesProcessor |
+---------------------------+ | (EarlyInitializable) |
^ +--------------------------------+
|
| @Inject
|
+--------------------------------+
| UserConversionPropertiesProcessor |
| (Initializable) |
+--------------------------------+
```
### Initialization Flow
```
Bootstrap Container Created
|
v
StrutsConversionPropertiesProcessor.init() [EarlyInitializable]
|
+-> processRequired("struts-default-conversion.properties")
| (Only class names, no Spring beans needed)
|
v
Full Container Created (SpringObjectFactory available)
|
v
DefaultConfiguration.reloadContainer() explicitly triggers:
container.getInstance(UserConversionPropertiesProcessor.class)
|
v
UserConversionPropertiesProcessor.init() [Initializable]
|
+-> provider.initUserConversions()
|
+-> process("struts-conversion.properties")
+-> process("xwork-conversion.properties")
(Spring bean names now resolved via SpringObjectFactory)
```
## Verification
### Unit Tests
```bash
mvn test -pl core -DskipAssembly -Dtest=StrutsConversionPropertiesProcessorTest
```
### Spring Plugin Integration Tests
```bash
mvn test -pl plugins/spring -DskipAssembly -Dtest=SpringTypeConverterTest
```
### All Conversion Tests
```bash
mvn test -pl core -DskipAssembly '-Dtest=*Conversion*'
```
## Usage After Implementation
Users can now specify Spring bean names in `struts-conversion.properties`:
```properties
# Using Spring bean name
java.time.LocalDate=localDateConverter
# Using class name (still works - backward compatible)
java.util.UUID=com.example.UUIDConverter
```
With Spring configuration:
```xml
<bean id="localDateConverter" class="com.example.LocalDateConverter"/>
```
## Code References
- `core/src/main/java/org/apache/struts2/conversion/UserConversionPropertiesProvider.java` - New interface
- `core/src/main/java/org/apache/struts2/conversion/StrutsConversionPropertiesProcessor.java:34` - Implements interface
- `core/src/main/java/org/apache/struts2/conversion/StrutsConversionPropertiesProcessor.java:75` - `initUserConversions()`
- `core/src/main/java/org/apache/struts2/conversion/UserConversionPropertiesProcessor.java` - Late processor
- `plugins/spring/src/main/java/org/apache/struts2/spring/SpringObjectFactory.java:246` - Bean name resolution
- `core/src/main/java/org/apache/struts2/config/impl/DefaultConfiguration.java:300` - Explicit late initialization trigger
- `core/src/main/java/org/apache/struts2/config/impl/DefaultConfiguration.java:384` - Factory registrations
- `core/src/main/resources/struts-beans.xml:117-119` - Bean definitions
## Alternative Approaches Considered
| Approach | Pros | Cons |
|----------|------|------|
| **Remove EarlyInitializable** | Simple, single change | Risk: converters may not be ready when first needed |
| **Spring plugin reprocessing** | Plugin-specific | Duplicates work, potential race conditions |
| **Lazy converter proxies** | Elegant | Complex implementation, overhead |
| **instanceof check** | Simple | Violates Dependency Inversion Principle |
| **Two-phase with interface** | Clean design, testable | Requires new interface (chosen approach) |