使用工厂bean和Utility Schema定义集合

时间:2015-01-31 20:36:07   收藏:0   阅读:234

工厂bean是实现了beanFactory接口的bean,也可以继承AbstractFactoryBean,主要是用于在给定属性参数之后自动创建一个bean对象。

我们在使用基本集合标记定义集合时,不能够指定集合的实体类,例如LinkedList、TreeSet或TreeMap等,而且也不能通过将集合定义为可供其他bean引用的单独bean在不同放入bean中共享集合。

Spring提供了两种选项来提供基本集合标记的不足。

  选项之一使用对应的集合工厂bean:ListFactoryBean、SetFactoryBean、MapFactoryBean。

<bean id="set" class="org.springframework.beans.factory.config.SetFactoryBean">
      <property name="targetSetClass">
              <value>java.util.TreeSet</value>
      </property>
      <property  name="sourceSet">
              <set>
                   <value></value>
              </set>
      </property>
</bean>        

  选项二在Spring 2.x之后引入了util schema中使用了集合标记:<util:list>、<util:set>、<util:map>

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemalocation="…………
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"

<util:set  id="" set-class="java.util.TreeSet">
         <value></value>
</util:set>

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!