mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-4827 Injects Container in constructor
This commit is contained in:
@@ -49,7 +49,7 @@ public class ObjectFactory implements Serializable {
|
||||
private static final Logger LOG = LogManager.getLogger(ObjectFactory.class);
|
||||
|
||||
private transient ClassLoader ccl;
|
||||
private Container container;
|
||||
private final Container container;
|
||||
|
||||
private ActionFactory actionFactory;
|
||||
private ResultFactory resultFactory;
|
||||
@@ -58,15 +58,15 @@ public class ObjectFactory implements Serializable {
|
||||
private ConverterFactory converterFactory;
|
||||
private UnknownHandlerFactory unknownHandlerFactory;
|
||||
|
||||
@Inject
|
||||
public ObjectFactory(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Inject(value="objectFactory.classloader", required=false)
|
||||
public void setClassLoader(ClassLoader cl) {
|
||||
this.ccl = cl;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setActionFactory(ActionFactory actionFactory) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.opensymphony.xwork2.spring;
|
||||
|
||||
import com.opensymphony.xwork2.ObjectFactory;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -72,6 +73,11 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon
|
||||
this.enableAopSupport = BooleanUtils.toBoolean(enableAopSupport);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public SpringObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Spring ApplicationContext that should be used to look beans up with.
|
||||
*
|
||||
|
||||
@@ -23,6 +23,9 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -38,6 +41,11 @@ public class SpringProxyableObjectFactory extends SpringObjectFactory {
|
||||
|
||||
private List<String> skipBeanNames = new ArrayList<>();
|
||||
|
||||
@Inject
|
||||
public SpringProxyableObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object buildBean(String beanName, Map<String, Object> extraContext) throws Exception {
|
||||
LOG.debug("Building bean for name {}", beanName);
|
||||
|
||||
+1
-1
@@ -106,7 +106,7 @@ public class ActionAutowiringInterceptor extends AbstractInterceptor implements
|
||||
LOG.warn("ApplicationContext could not be found. Action classes will not be autowired.");
|
||||
} else {
|
||||
setApplicationContext(applicationContext);
|
||||
factory = new SpringObjectFactory();
|
||||
factory = new SpringObjectFactory(ActionContext.getContext().getContainer());
|
||||
factory.setApplicationContext(getApplicationContext());
|
||||
if (autowireStrategy != null) {
|
||||
factory.setAutowireStrategy(autowireStrategy);
|
||||
|
||||
@@ -5,11 +5,19 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Map;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
|
||||
/**
|
||||
* ObjectFactory that returns a FooProxy in the buildBean if the clazz is FooAction
|
||||
*/
|
||||
public class ProxyObjectFactory extends ObjectFactory {
|
||||
|
||||
@Inject
|
||||
public ProxyObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* It returns an instance of the bean except if the class is FooAction.
|
||||
* In this case, it returns a FooProxy of it.
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ public class ScopedModelDrivenInterceptorTest extends XWorkTestCase {
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
inter = new ScopedModelDrivenInterceptor();
|
||||
inter.setObjectFactory(new ProxyObjectFactory());
|
||||
inter.setObjectFactory(new ProxyObjectFactory(container));
|
||||
}
|
||||
|
||||
public void testResolveModel() throws Exception {
|
||||
|
||||
@@ -221,7 +221,6 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
|
||||
public void testObjectFactoryDestroy() throws Exception {
|
||||
|
||||
final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
|
||||
ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
|
||||
Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), cm);
|
||||
Mock mockConfiguration = new Mock(Configuration.class);
|
||||
@@ -231,6 +230,7 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
String reloadConfigs = container.getInstance(String.class, XWorkConstants.RELOAD_XML_CONFIGURATION);
|
||||
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(XWorkConstants.RELOAD_XML_CONFIGURATION)),
|
||||
reloadConfigs);
|
||||
final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory((Container) mockContainer.proxy());
|
||||
mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);
|
||||
|
||||
mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
|
||||
@@ -261,7 +261,7 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
packageConfigs.put("test", packageConfig);
|
||||
|
||||
Mock mockContainer = new Mock(Container.class);
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory((Container) mockContainer.proxy()));
|
||||
String reloadConfigs = container.getInstance(String.class, XWorkConstants.RELOAD_XML_CONFIGURATION);
|
||||
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(XWorkConstants.RELOAD_XML_CONFIGURATION)),
|
||||
reloadConfigs);
|
||||
@@ -316,6 +316,10 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
public static class InnerDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
|
||||
public boolean destroyed = false;
|
||||
|
||||
public InnerDestroyableObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
destroyed = true;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package org.apache.struts2.cdi;
|
||||
|
||||
import com.opensymphony.xwork2.ObjectFactory;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -73,8 +74,9 @@ public class CdiObjectFactory extends ObjectFactory {
|
||||
|
||||
Map<Class<?>, InjectionTarget<?>> injectionTargetCache = new ConcurrentHashMap<Class<?>, InjectionTarget<?>>();
|
||||
|
||||
public CdiObjectFactory() {
|
||||
super();
|
||||
@Inject
|
||||
public CdiObjectFactory(Container container) {
|
||||
super(container);
|
||||
LOG.info("Initializing Struts2 CDI integration...");
|
||||
this.beanManager = findBeanManager();
|
||||
if (beanManager != null) {
|
||||
|
||||
@@ -27,19 +27,19 @@ public class CdiObjectFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testFindBeanManager() throws Exception {
|
||||
assertNotNull(new CdiObjectFactory().findBeanManager());
|
||||
assertNotNull(new CdiObjectFactory(null).findBeanManager());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBean() throws Exception {
|
||||
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
|
||||
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
|
||||
FooConsumer fooConsumer = (FooConsumer) cdiObjectFactory.buildBean(FooConsumer.class.getCanonicalName(), null, false);
|
||||
assertNotNull(fooConsumer);
|
||||
assertNotNull(fooConsumer.fooService);
|
||||
}
|
||||
|
||||
@Test public void testGetInjectionTarget() throws Exception {
|
||||
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
|
||||
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
|
||||
final InjectionTarget<?> injectionTarget = cdiObjectFactory.getInjectionTarget(FooConsumer.class);
|
||||
assertNotNull(injectionTarget);
|
||||
assertTrue(cdiObjectFactory.injectionTargetCache.containsKey(FooConsumer.class));
|
||||
|
||||
+6
-3
@@ -338,7 +338,7 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
|
||||
configuration.addPackageConfig("class-level", classLevelParentPkg);
|
||||
|
||||
ActionNameBuilder actionNameBuilder = new SEOActionNameBuilder("true", "-");
|
||||
ObjectFactory of = new ObjectFactory();
|
||||
ObjectFactory of = new ObjectFactory(mockContainer);
|
||||
DefaultInterceptorMapBuilder interceptorBuilder = new DefaultInterceptorMapBuilder();
|
||||
interceptorBuilder.setConfiguration(configuration);
|
||||
|
||||
@@ -778,8 +778,9 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
|
||||
if (type == FileManagerFactory.class) {
|
||||
return (T) fileManagerFactory;
|
||||
}
|
||||
T obj = type.newInstance();
|
||||
if (obj instanceof ObjectFactory) {
|
||||
T obj;
|
||||
if (type == ObjectFactory.class) {
|
||||
obj = type.getConstructor(Container.class).newInstance(this);
|
||||
OgnlReflectionProvider rp = new OgnlReflectionProvider() {
|
||||
|
||||
@Override
|
||||
@@ -809,6 +810,8 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
|
||||
|
||||
((ObjectFactory) obj).setInterceptorFactory(dif);
|
||||
((ObjectFactory) obj).setResultFactory(drf);
|
||||
} else {
|
||||
obj = type.newInstance();
|
||||
}
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -35,6 +35,11 @@ public class DelegatingObjectFactory extends ObjectFactory implements ObjectFact
|
||||
private BundleAccessor bundleResourceLoader;
|
||||
private OsgiConfigurationProvider osgiConfigurationProvider;
|
||||
|
||||
@Inject
|
||||
public DelegatingObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setDelegateObjectFactory(@Inject Container container,
|
||||
@Inject("struts.objectFactory.delegate") String delegate) {
|
||||
|
||||
@@ -23,6 +23,7 @@ package org.apache.struts2.osgi;
|
||||
|
||||
import com.opensymphony.xwork2.ObjectFactory;
|
||||
import com.opensymphony.xwork2.util.ClassLoaderUtil;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
@@ -38,6 +39,11 @@ public class SpringOsgiObjectFactory extends ObjectFactory {
|
||||
|
||||
private BundleAccessor bundleAccessor;
|
||||
|
||||
@Inject
|
||||
public SpringOsgiObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
public Object buildBean(String className, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
|
||||
if (containsBean(className))
|
||||
return getBean(className);
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
|
||||
import com.opensymphony.xwork2.config.entities.ResultConfig;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.interceptor.Interceptor;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -80,6 +81,11 @@ public class PlexusObjectFactory extends ObjectFactory {
|
||||
private PlexusContainer base;
|
||||
private ReflectionProvider reflectionProvider;
|
||||
|
||||
@Inject
|
||||
public PlexusObjectFactory(Container container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setReflectionProvider(ReflectionProvider reflectionProvider) {
|
||||
this.reflectionProvider = reflectionProvider;
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ public class StrutsSpringObjectFactory extends SpringObjectFactory {
|
||||
@Inject(StrutsConstants.STRUTS_DEVMODE) String devMode,
|
||||
@Inject Container container) {
|
||||
|
||||
super();
|
||||
super(container);
|
||||
boolean useClassCache = BooleanUtils.toBoolean(useClassCacheStr);
|
||||
LOG.info("Initializing Struts-Spring integration...");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user