国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

11、spring的bean基礎(3)

2019-11-08 02:27:03
字體:
來源:轉載
供稿:網友

11、sPRing的bean基礎(3)

在本文中,主要介紹的知識點有以下三個

ListFactoryBeanSetFactoryBeanMapFactoryBean

現項目中存在一個實體類,將用于以下三個例子的演示 HelloWorld.java

public class HelloWorld{ private List list; private Set set; private Map map; //.....}

ListFactoryBean示例 在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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main.HelloWorld"> <property name="list"> <bean class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="sourceList"> <list> <value>hello</value> <value>1</value> <value>three</value> </list> </property> </bean> </property> </bean></beans>

還可以使用下方式:

<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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.yiibai.common.Customer"> <property name="list"> <util:list list-class="java.util.ArrayList"> <value>Hello</value> <value>1</value> <value>three</value> </util:list> </property> </bean></beans>

如果你不把util:list模式包含進配置文件中:或許會出現以下錯誤:

Caused by: org.xml.sax.SAXParseException: The prefix "util" for element "util:list" is not bound.

SetFactoryBean實例

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main.HelloWorld"> <property name="set"> <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>

也可以使用以下模式:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main.HelloWorld"> <property name="set"> <util:set set-class="java.util.HashSet"> <value>one</value> <value>2</value> <value>three</value> </util:set> </property> </bean></beans>

MapFactoryBean實例

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main.HelloWorld"> <property name="map"> <bean class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="targetMapClass"> <value>java.util.HashMap</value> </property> <property name="sourceMap"> <map> <entry key="Key1" value="one" /> <entry key="Key2" value="two" /> <entry key="Key3" value="three" /> </map> </property> </bean> </property> </bean></beans>

也可以使用以下方式:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main.HelloWorld"> <property name="map"> <util:map map-class="java.util.HashMap"> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </util:map> </property> </bean></beans>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤壁市| 抚宁县| 准格尔旗| 红原县| 大兴区| 巨野县| 任丘市| 惠安县| 堆龙德庆县| 囊谦县| 儋州市| 荔波县| 同江市| 公安县| 全南县| 孟村| 安仁县| 青神县| 长阳| 牙克石市| 色达县| 海宁市| 伊川县| 广南县| 聂拉木县| 宜章县| 宣城市| 嘉兴市| 嘉义市| 大邑县| 清新县| 宜阳县| 洱源县| 仁化县| 霍林郭勒市| 晋城| 裕民县| 棋牌| 北安市| 肥东县| 顺义区|