diff --git a/core/src/main/java/org/apache/struts2/factory/PrefixBasedActionProxyFactory.java b/core/src/main/java/org/apache/struts2/factory/PrefixBasedActionProxyFactory.java index cf196a9b2..03052ec6a 100644 --- a/core/src/main/java/org/apache/struts2/factory/PrefixBasedActionProxyFactory.java +++ b/core/src/main/java/org/apache/struts2/factory/PrefixBasedActionProxyFactory.java @@ -59,11 +59,12 @@ import java.util.Set; * * */ -public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory { +public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory implements Initializable { private static final Logger LOG = LogManager.getLogger(PrefixBasedActionProxyFactory.class); private Map actionProxyFactories = new HashMap<>(); + private Set prefixes = new HashSet<>(); @Inject public void setContainer(Container container) { @@ -73,18 +74,22 @@ public class PrefixBasedActionProxyFactory extends StrutsActionProxyFactory { @Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION) public void setPrefixBasedActionProxyFactories(String list) { if (list != null) { - Set prefixes = new HashSet<>(Arrays.asList(list.split(","))); - for (String factory : prefixes) { - String[] thisFactory = factory.split(":"); - if (thisFactory.length == 2) { - String factoryPrefix = thisFactory[0].trim(); - String factoryName = thisFactory[1].trim(); - ActionProxyFactory obj = container.getInstance(ActionProxyFactory.class, factoryName); - if (obj != null) { - actionProxyFactories.put(factoryPrefix, obj); - } else { - LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]", factory); - } + prefixes = new HashSet<>(Arrays.asList(list.split(","))); + } + } + + @Override + public void init() { + for (String factory : prefixes) { + String[] thisFactory = factory.split(":"); + if (thisFactory.length == 2) { + String factoryPrefix = thisFactory[0].trim(); + String factoryName = thisFactory[1].trim(); + ActionProxyFactory obj = container.getInstance(ActionProxyFactory.class, factoryName); + if (obj != null) { + actionProxyFactories.put(factoryPrefix, obj); + } else { + LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]", factory); } } } diff --git a/core/src/test/java/org/apache/struts2/factory/PrefixBasedActionProxyFactoryTest.java b/core/src/test/java/org/apache/struts2/factory/PrefixBasedActionProxyFactoryTest.java index 27e9d019c..109a61fec 100644 --- a/core/src/test/java/org/apache/struts2/factory/PrefixBasedActionProxyFactoryTest.java +++ b/core/src/test/java/org/apache/struts2/factory/PrefixBasedActionProxyFactoryTest.java @@ -41,7 +41,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { private PrefixBasedActionProxyFactory factory; public void testDifferentPrefixes() throws Exception { - factory.setPrefixBasedActionProxyFactories("/ns1:prefix1,/ns2:prefix2"); + initFactory("/ns1:prefix1,/ns2:prefix2"); ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.emptyMap(), false, true); assertTrue(proxy1 instanceof Prefix1ActionProxy); @@ -51,7 +51,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { } public void testFallbackToDefault() throws Exception { - factory.setPrefixBasedActionProxyFactories("/ns1:prefix1"); + initFactory("/ns1:prefix1"); ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.emptyMap(), false, true); assertTrue(proxy1 instanceof Prefix1ActionProxy); @@ -61,7 +61,7 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { } public void testEmptyPrefix() throws Exception { - factory.setPrefixBasedActionProxyFactories(":prefix1"); + initFactory(":prefix1"); ActionProxy proxy1 = factory.createActionProxy("/ns1", "", "", Collections.emptyMap(), false, true); assertTrue(proxy1 instanceof Prefix1ActionProxy); @@ -81,7 +81,9 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { public Object create(Context context) throws Exception { return new Prefix1Factory(); } - + public Class type() { + return Prefix1Factory.class; + } }, Scope.SINGLETON); } }, @@ -92,7 +94,9 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { public Object create(Context context) throws Exception { return new Prefix2Factory(); } - + public Class type() { + return Prefix2Factory.class; + } }, Scope.SINGLETON); } } @@ -104,6 +108,11 @@ public class PrefixBasedActionProxyFactoryTest extends StrutsInternalTestCase { factory.setContainer(container); } + void initFactory(String list) { + factory.setPrefixBasedActionProxyFactories(list); + factory.init(); + } + @Override public void tearDown() throws Exception { super.tearDown();