mirror of
https://github.com/apache/struts.git
synced 2026-08-11 09:36:57 +00:00
[WW-4938] Uses container to create an instance of the class (#295)
* WW-4938 Uses container to create an instance of the class * WW-4938 Adds a test to proof that @Inject works on constructor * WW-4938 Adds missing header * Update core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java Co-Authored-By: lukaszlenart <lukasz.lenart@gmail.com> * Update core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java Co-Authored-By: lukaszlenart <lukasz.lenart@gmail.com>
This commit is contained in:
committed by
Yasser Zamani
parent
357b9c70b0
commit
699f5d6c79
@@ -151,7 +151,7 @@ public class ObjectFactory implements Serializable {
|
||||
* @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 class ObjectFactory implements Serializable {
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -168,9 +168,7 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon
|
||||
Class beanClazz = getClassInstance(beanName);
|
||||
o = buildBean(beanClazz, extraContext);
|
||||
}
|
||||
if (injectInternal) {
|
||||
injectInternalBeans(o);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
@@ -224,9 +222,7 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon
|
||||
}
|
||||
injectApplicationContext(bean);
|
||||
|
||||
injectInternalBeans(bean);
|
||||
|
||||
return bean;
|
||||
return injectInternalBeans(bean);
|
||||
}
|
||||
|
||||
private void injectApplicationContext(Object bean) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -921,7 +921,11 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
|
||||
}
|
||||
|
||||
public <T> T inject(Class<T> implementation) {
|
||||
return null;
|
||||
try {
|
||||
return implementation.newInstance();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void removeScopeStrategy() {
|
||||
|
||||
Reference in New Issue
Block a user