struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4938) ObjectFactory should use Container to instantiate actions and inject dependencies
Date Tue, 18 Dec 2018 12:26:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4938?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16724000#comment-16724000
] 

ASF GitHub Bot commented on WW-4938:
------------------------------------

yasserzamani closed pull request #295: [WW-4938] Uses container to create an instance of the
class
URL: https://github.com/apache/struts/pull/295
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java
index a5d91c68a..6dac12f5d 100644
--- a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java
+++ b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java
@@ -151,7 +151,7 @@ public Object buildAction(String actionName, String namespace, ActionConfig
conf
      * @throws Exception in case of any error
      */
     public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception
{
-        return clazz.newInstance();
+        return container.inject(clazz);
     }
 
     /**
@@ -188,11 +188,7 @@ public Object buildBean(String className, Map<String, Object> extraContext)
thro
      */
     public Object buildBean(String className, Map<String, Object> extraContext, boolean
injectInternal) throws Exception {
         Class clazz = getClassInstance(className);
-        Object obj = buildBean(clazz, extraContext);
-        if (injectInternal) {
-            injectInternalBeans(obj);
-        }
-        return obj;
+        return buildBean(clazz, extraContext);
     }
 
     /**
diff --git a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java
index 9dead67ee..0d3df9f22 100644
--- a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java
+++ b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java
@@ -168,9 +168,7 @@ public Object buildBean(String beanName, Map<String, Object> extraContext,
boole
             Class beanClazz = getClassInstance(beanName);
             o = buildBean(beanClazz, extraContext);
         }
-        if (injectInternal) {
-            injectInternalBeans(o);
-        }
+
         return o;
     }
 
@@ -224,9 +222,7 @@ public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFac
         }
         injectApplicationContext(bean);
 
-        injectInternalBeans(bean);
-
-        return bean;
+        return injectInternalBeans(bean);
     }
 
     private void injectApplicationContext(Object bean) {
diff --git a/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java b/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java
new file mode 100644
index 000000000..036926e2f
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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 com.opensymphony.xwork2;
+
+import com.opensymphony.xwork2.mock.DummyTextProvider;
+import com.opensymphony.xwork2.mock.InjectableAction;
+import org.apache.struts2.StrutsInternalTestCase;
+
+import java.util.HashMap;
+
+public class ObjectFactoryTest extends StrutsInternalTestCase {
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        this.loadButAdd(TextProvider.class, new DummyTextProvider());
+    }
+
+    public void testCreatingActionsWithInjectableParametersInConstructor() throws Exception
{
+        // given
+        ObjectFactory of = container.getInstance(ObjectFactory.class);
+
+        // when
+        InjectableAction action = (InjectableAction) of.buildBean(InjectableAction.class,
new HashMap<String, Object>());
+
+        // then
+        assertNotNull(action.getTextProvider());
+        assertTrue(action.getTextProvider() instanceof DummyTextProvider);
+    }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java b/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java
new file mode 100644
index 000000000..01bc270a0
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java
@@ -0,0 +1,87 @@
+/*
+ * 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 com.opensymphony.xwork2.mock;
+
+import com.opensymphony.xwork2.TextProvider;
+import com.opensymphony.xwork2.util.ValueStack;
+
+import java.util.List;
+import java.util.ResourceBundle;
+
+public class DummyTextProvider implements TextProvider {
+    @Override
+    public boolean hasKey(String key) {
+        return false;
+    }
+
+    @Override
+    public String getText(String key) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String obj) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, List<?> args) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String[] args) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, List<?> args) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String[] args) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, List<?> args, ValueStack
stack) {
+        return null;
+    }
+
+    @Override
+    public String getText(String key, String defaultValue, String[] args, ValueStack stack)
{
+        return null;
+    }
+
+    @Override
+    public ResourceBundle getTexts(String bundleName) {
+        return null;
+    }
+
+    @Override
+    public ResourceBundle getTexts() {
+        return null;
+    }
+}
diff --git a/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java b/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java
new file mode 100644
index 000000000..9ef1d35ba
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java
@@ -0,0 +1,36 @@
+/*
+ * 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 com.opensymphony.xwork2.mock;
+
+import com.opensymphony.xwork2.TextProvider;
+import com.opensymphony.xwork2.inject.Inject;
+
+public class InjectableAction {
+
+    private TextProvider textProvider;
+
+    @Inject
+    public InjectableAction(TextProvider textProvider) {
+        this.textProvider = textProvider;
+    }
+
+    public TextProvider getTextProvider() {
+        return textProvider;
+    }
+}
diff --git a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
index a19be9bed..02a487815 100644
--- a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
+++ b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java
@@ -921,7 +921,11 @@ public void inject(Object o) {
         }
 
         public <T> T inject(Class<T> implementation) {
-            return null;
+            try {
+                return implementation.newInstance();
+            } catch (Exception e) {
+                return null;
+            }
         }
 
         public void removeScopeStrategy() {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> ObjectFactory should use Container to instantiate actions and inject dependencies 
> ----------------------------------------------------------------------------------
>
>                 Key: WW-4938
>                 URL: https://issues.apache.org/jira/browse/WW-4938
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Core
>            Reporter: Lukasz Lenart
>            Assignee: Lukasz Lenart
>            Priority: Minor
>             Fix For: 2.6
>
>
> It isn't possible to define an action with a constructor and annotated with {{@Inject}}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message