spring xml <property> 中的多个 <ref>
multiple <ref> in <property> in spring xml
我是 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>
如评论中所述,如果 answerList
的类型为 java.util.List
,则应将 bean 引用放在 <list>
标记内。其他包装器元素分别为 <set>
、<map>
和 <props>
,用于 java.util.Set
、java.util.Map
和 java.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>
如果您的 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>
我是 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>
如评论中所述,如果 answerList
的类型为 java.util.List
,则应将 bean 引用放在 <list>
标记内。其他包装器元素分别为 <set>
、<map>
和 <props>
,用于 java.util.Set
、java.util.Map
和 java.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>
如果您的 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>