为什么Spring没有将一个映射注入到子项Struts 2 Action类中,但父项的对象是否被注入?

时间:2011-02-18 22:49:21

标签: java spring struts2 ioc-container

我在Spring applicationContext.xml中有以下配置,以便将对象注入我的Struts 2 Java项目中:

<util:map id="typeToURLMap">
    <entry key="TYPEA" value="./ur1.x" />
    <entry key="TYPEB" value="./url2.x" />
    <entry key="TYPEC" value="./url3.x" />
    <entry key="OTHER" value="./url4.x" />
</util:map>

<bean id="parentAction" class="my.package.ParentAction" scope="prototype">
    <property name="businessDelegate" ref="businessDelegateNotRelevantToThisExample" />
</bean>

<bean id="childAction" class="my.package.ChildAction" scope="prototype" parent="parentAction">
    <property name="typeToURLMap" ref="typeToURLMap"/>
</bean>

出于某种原因,在父Action上调用setter,而在子Action中调用setter。 此配置有什么问题吗?

注意:据我所知,util:map将默认为Java类型的HashMap。

我的ParentAction如下所示:

 public class ParentAction extends MyAppBaseAction {

        private BusinessDelegate businessDelegate;

        //other action code using business delegate

        /**
         *  This IS called.
        */
        public void setBusinessDelegate(BusinessDelegate delegate){
            this.businessDelegate = delegate;
        }
    }

我的ChildAction如下所示:

public class ChildAction extends ParentAction{

    private Map<String,String> typeToURLMap;

    //other action code using map

    /**
     *  Never Called! (Why?)
    */
    public void setTypeToURLMap(Map<String,String map){
        this.typeToURLMap = map;
    }
}

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

尝试为applicationContext.xml文件编写单元测试

public class ApplicationContextTest extends TestCase {

  protected ApplicationContext      ctx;

  protected static final String[]   CONTEXT_LOCATIONS = new String[] {
      "classpath:resources/applicationContext.xml"};

  public void setUp() throws Exception {
    super.setUp();
    ctx = new ClassPathXmlApplicationContext(CONTEXT_LOCATIONS);
  }

  public void test() {
    ChildAction ca = ctx.getBean("childAction", ChildAction.class);
    assertNotNull(ca.getTypeToURLMap());
  }
}

如果测试通过,那么看看struts如何与spring集成