Fixing setter order problem by using constructor injection

WW-1753


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@532755 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2007-04-26 14:23:02 +00:00
parent 8974b8142a
commit ea890e6700
2 changed files with 10 additions and 24 deletions
@@ -43,22 +43,14 @@ import com.opensymphony.xwork2.spring.SpringObjectFactory;
*/
public class StrutsSpringObjectFactory extends SpringObjectFactory {
private static final Log log = LogFactory.getLog(StrutsSpringObjectFactory.class);
private String autoWire;
private boolean useClassCache = true;
@Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false)
public void setAutoWire(String val) {
autoWire = val;
}
@Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false)
public void setUseClassCache(String val) {
useClassCache = "true".equals(val);
}
@Inject
public void setServletContext(ServletContext servletContext) {
public StrutsSpringObjectFactory(
@Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
@Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
@Inject ServletContext servletContext) {
boolean useClassCache = "true".equals(useClassCacheStr);
log.info("Initializing Struts-Spring integration...");
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
@@ -38,18 +38,13 @@ public class StrutsSpringObjectFactoryTest extends TestCase {
public void testNoSpringContext() throws Exception {
// to cover situations where there will be logged an error
StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory();
ServletContext msc = (ServletContext) new MockServletContext();
fac.setServletContext(msc);
StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory(null, null, new MockServletContext());
assertEquals(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, fac.getAutowireStrategy());
}
public void testWithSpringContext() throws Exception {
StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory();
// autowire by constructure, we try a non default setting in this unit test
fac.setAutoWire("constructor");
ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
ServletContext msc = (ServletContext) new MockServletContext();
@@ -57,8 +52,7 @@ public class StrutsSpringObjectFactoryTest extends TestCase {
ac.setServletContext(msc);
ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
ac.refresh();
fac.setServletContext(msc);
StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, msc);
assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
}