當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring SetFactoryBean实例
生活随笔
收集整理的這篇文章主要介紹了
Spring SetFactoryBean实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
SetFactoryBean 類為開發(fā)者提供了一種可在 Spring?bean 配置文件創(chuàng)建一個(gè)具體的Set集合(HashSet 和 TreeSet)。 這里有一個(gè) ListFactoryBean。例如,在運(yùn)行時(shí)它將實(shí)例化 HashSet,并注入到一個(gè) bean 屬性中。 package com.yiibai.common;import java.util.Set;public class Customer
{private Set sets;//...
}
Spring的bean配置文件。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="sets"><bean class="org.springframework.beans.factory.config.SetFactoryBean"><property name="targetSetClass"><value>java.util.HashSet</value></property><property name="sourceSet"><list><value>one</value><value>2</value><value>three</value></list></property></bean></property></bean></beans>
另外,還可以使用?util的模式?和<util:set>?來做到同樣的事情。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="sets"><util:set set-class="java.util.HashSet"><value>one</value><value>2</value><value>three</value></util:set></property></bean></beans>
請記住必須包函?util?模式,否則會出現(xiàn)下面的錯(cuò)誤: Caused by: org.xml.sax.SAXParseException: The prefix "util" for element "util:set" is not bound.
執(zhí)行結(jié)果
package com.yiibai.common;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Customer cust = (Customer)context.getBean("CustomerBean");System.out.println(cust);} }輸出結(jié)果:
Customer [sets=[2, one, three]] Type=[class java.util.HashSet] 實(shí)例化 HashSet,在運(yùn)行時(shí)注入到客戶的set集合屬性。 下載代碼 –?http://pan.baidu.com/s/1o7woXQU http://www.yiibai.com/spring/spring-setfactorybean-example.html總結(jié)
以上是生活随笔為你收集整理的Spring SetFactoryBean实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 住哪儿酒店预订电话(住哪儿网酒店预订)
- 下一篇: 电脑周边线材图片(电脑周边)