Is it possible to create a collections with Spring configuration?

被刻印的时光 ゝ 提交于 2019-12-06 16:38:21

You could do:

<bean id="myClassA" class="org.foo.MyClass"> 
   <constructor-arg>
     <bean class="java.lang.String">
       <constructor-arg value="A"/>
     </bean>   
   <constructor-arg>
</bean>

<bean id="sequence" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <ref bean="myClassA" />
            ...
        </list>
    </constructor-arg>
</bean>

Note, however, that the most common approach is to inject a list directly into a bean rather than wrapping a list within a list first.

You should have a look at the Collections section in the spring IOC documentation.

<bean id="moreComplexObject" class="example.ComplexObject">
  <property name="someList">
    <list>
      <value>a list element followed by a reference</value>
      <ref bean="myDataSource" />
    </list>
  </property>    
</bean>

Yes. You can even create it as a standalone bean. See this thread for two examples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!