您的位置:首页 > 编程语言 > Java开发

Spring入门学习——使用工厂Bean和Utility Schema定义集合

2017-04-21 10:49 381 查看
使用基本集合标记定义集合时,不能指定集合的实体类,例如LinkedList、TreeSet或TreeMap,而且不能通过将(集合定义为可供其他Bean引用)的(单独Bean)在不同的Bean中(共享集合)。
解决方法:
(1)使用对应的集合工厂Bean,如ListFactoryBean,SetFactoryBean,MapFactoryBean
(2)使用在Spring2.x引入的util schema使用集合标记,<util:list>、<util:set>和<util:map>
仍然使用之前博客中一直使用的生成序列号代码作为理解实践
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="prefixGenerator" ref="datePrefixGenerator"/>
<property name="initial" value="10000"/>
<property name="suffixes">
<bean class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="targetSetClass[/b]">
<value>java.util.TreeSet</value>
</property>
<property name="sourceSet[/b]">
<set>
<value>5</value>
<value>10</value>
<value>20</value>
</set>
</property>
</bean>
</property>
</bean>

<bean id="datePrefixGenerator" class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: