spring xml </property> </ref>中<property>中的多个<ref>

时间:2015-01-06 13:49:07

标签: java xml spring spring-mvc java-ee

我是Spring的新手,我有三个bean id,例如answerBean1,answerBean2,answerBean3,并且应该使用<property>将它们包含在bean questionBean中。我可以将一个bean指定为ref,但是当分配多个bean时,我收到错误。我不需要使用<constuctor-arg>来完成这项工作。

<bean id="answerBean1" class="com.spring.java.CICollection.Answer">
<property name="id" value="101"></property>
<property name="answerText" value="Collection of constants and method declarations"></property>
</bean>

<bean id="answerBean2" class="com.spring.java.CICollection.Answer">
<property name="id" value="102"></property>
<property name="answerText" value="Collection of abstract method and constants"></property>
</bean>

<bean id="answerBean3" class="com.spring.java.CICollection.Answer">
<property name="id" value="103"></property>
<property name="answerText" value="Constants and abstract methods"></property>
</bean>

<bean id="questionBean" class="com.spring.java.CICollection.Question">
<property name="id" value="101"></property>
<property name="questionText" value="What is interface?"></property>
<property name="answerList" >
<ref bean="answerBean1"/>
 <ref bean="answerBean2"/>
<ref bean="answerBean3"/>
</property>
</bean>

2 个答案:

答案 0 :(得分:2)

正如您在评论中已经说过的那样,如果<list>的类型为answerList,则应将bean引用放在java.util.List标记内。其他包装元素分别为<set><map><props> java.util.Setjava.util.Mapjava.util.Properties。请查看these examples以获取更多信息。

<bean id="questionBean" class="com.spring.java.CICollection.Question">
    <property name="id" value="101"></property>
    <property name="questionText" value="What is interface?"></property>
    <property name="answerList" >
        <list>
            <ref bean="answerBean1"/>
            <ref bean="answerBean2"/>
            <ref bean="answerBean3"/>
        <list>
    </property>
</bean>

答案 1 :(得分:0)

如果您的answerList属于类型列表,那么您应该只在列表标记中添加多个ref。如下所示代码 -

<bean id="questionBean" class="com.spring.java.CICollection.Question">
<property name="id" value="101"></property>
<property name="questionText" value="What is interface?"></property>
<property name="answerList">
    <list>
        <ref bean="answerBean1"/>
        <ref bean="answerBean2"/>
        <ref bean="answerBean3"/>
    </list>
</property>

...

</bean>