为什么 Spring 不将映射注入到子 Struts 2 Action 类中,而是注入父对象的对象?

发布于 2024-10-18 06:33:31 字数 1642 浏览 2 评论 0原文

我在 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;
    }
}

任何帮助将不胜感激。谢谢!

I have the following configuration in my Spring applicationContext.xml in order to inject objects into my Struts 2 Java project:

<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>

For some reason, the setter is being called on the parent Action, but not in the child Action. Is there anything wrong with this configuration?

Note: It is my understanding that util:map will default to a Java type of HashMap.

My ParentAction looks like the following:

 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;
        }
    }

My ChildAction looks like the following:

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;
    }
}

Any help would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-10-25 06:33:31

尝试为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集成的

Try writing a unit test for the applicationContext.xml file

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());
  }
}

If the test passes then look at how struts is integrated with spring

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文