Spring-注入引數詳解-[集合型別屬性]
阿新 • • 發佈:2019-02-03
概述
java.util包中的集合型別是最常用的結構資料型別,主要包括List、Set、Map、Properties。
Spring為這些集合型別屬性提供了專屬的配置標籤
常用集合
Set
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.set;
import java.util.Iterator;
import java.util.Set;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取注入的set,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
Set<Object> set = pets.getSet();
Iterator<Object> it = set.iterator();
while (it.hasNext()) {
System.out.println("PetShop has " + it.next());
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.set;
import java.util.HashSet;
import java.util.Set;
public class Pets {
private Set set = new HashSet();
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.set.Pets">
<property name="set">
<set>
<value>bear</value>
<value>dog</value>
<value>cat</value>
<value>snake</value>
<value>pig</value>
</set>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.set.PetShop">
<property name="pets" ref="pets"/>
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectSetTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/set/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果
List
List屬性既可以通過注入字串,也可以通過注入容器中其他的Bean
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.list;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取容器注入的List,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
for (int i = 0; i < pets.getPetsList().size(); i++) {
System.out.println("PetShop has " + pets.getPetsList().get(i));
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.list;
import java.util.ArrayList;
import java.util.List;
public class Pets {
private List petsList = new ArrayList();
public List getPetsList() {
return petsList;
}
public void setPetsList(List petsList) {
this.petsList = petsList;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.list.Pets">
<property name="petsList">
<list>
<value>dog</value>
<value>cat</value>
<value>bear</value>
<value>rabbit</value>
<value>bird</value>
</list>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.list.PetShop">
<property name="pets">
<ref bean="pets"/>
</property>
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.list;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectListTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/list/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果
假設一個屬性型別可以通過字串字面值進行配置,那麼該型別對應的陣列型別的屬性比入String[],int[]也可以採用<list>
方式進行配置.
Map
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.map;
import java.util.Map;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取容器注入的Map,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
Map<Object, Object> map = pets.getMap();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
System.out.println("編號Key = " + entry.getKey() + ",品種Value = "
+ entry.getValue());
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.map;
import java.util.HashMap;
import java.util.Map;
public class Pets {
private Map map = new HashMap();
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.map.Pets">
<property name="map">
<map>
<entry>
<key>
<value>135</value>
</key>
<value>cat</value>
</entry>
<entry>
<key>
<value>137</value>
</key>
<value>bird</value>
</entry>
<entry>
<key>
<value>139</value>
</key>
<value>dog</value>
</entry>
</map>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.map.PetShop">
<property name="pets" ref="pets" />
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectMapTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/map/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果:
如果Map元素的key和value都是物件,這可以採用以下配置方式
<entry>
<key>
<ref bean="keyBbean"/>
</key>
<ref bean="valueBean">
</entry>
Properties
Propertites鍵值對可以看做Map型別的特例, Map的鍵值對可以是任何型別,Properties的鍵值對只能是字串,因此配置簡單一些,注意Value的配置沒有<value>
子元素標籤
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.properties;
import java.util.Map;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取容器注入的Map,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
Map<Object, Object> map = pets.getProperties();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
System.out.println("編號Key = " + entry.getKey() + ",品種Value = "
+ entry.getValue());
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.properties;
import java.util.Properties;
public class Pets {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.properties.Pets">
<property name="properties">
<props>
<prop key="101">cat</prop>
<prop key="103">dog</prop>
<prop key="105">bird</prop>
</props>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.properties.PetShop">
<property name="pets" ref="pets" />
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectPropertiesTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/properties/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果
強型別集合
Java5.0提供了強型別集合的新功能,允許為集合元素指定特定型別。
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.strongType;
import java.util.Map;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取容器注入的Map,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
Map<Integer, String> map = pets.getMap();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("編號Key = " + entry.getKey() + ",品種Value = "
+ entry.getValue());
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.strongType;
import java.util.HashMap;
import java.util.Map;
public class Pets {
private Map<Integer, String> map = new HashMap<Integer, String>();
public Map<Integer, String> getMap() {
return map;
}
public void setMap(Map<Integer, String> map) {
this.map = map;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.strongType.Pets">
<property name="map">
<map>
<entry>
<key>
<!-- 為Integer提供值,spring在設定值時,會轉換為定義的Integer型別 -->
<value>111</value>
</key>
<value>cat</value>
</entry>
<entry>
<key>
<value>113</value>
</key>
<value>bird</value>
</entry>
<entry>
<key>
<value>115</value>
</key>
<value>dog</value>
</entry>
</map>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.strongType.PetShop">
<property name="pets" ref="pets" />
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.strongType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectStrongTypeTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/strongType/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果
集合合併
Spring支援集合合併的功能,允許子bean繼承父bean的同名屬性集合元素,並將子bean和父bean中配置的集合屬性組合並起來作為最終的bean的屬性值。
例項
POJO類
package com.xgj.ioc.inject.construct.jihe.merge;
import java.util.Map;
public class PetShop {
private Pets pets;
public void setPets(Pets pets) {
this.pets = pets;
}
/**
*
*
* @Title: petsInfo
*
* @Description: 獲取容器注入的Map,遍歷輸出
*
*
* @return: void
*/
public void petsInfo() {
Map<Integer, String> map = pets.getMap();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("編號Key = " + entry.getKey() + ",品種Value = "
+ entry.getValue());
}
}
}
POJO類
package com.xgj.ioc.inject.construct.jihe.merge;
import java.util.HashMap;
import java.util.Map;
public class Pets {
private Map<Integer, String> map = new HashMap<Integer, String>();
public Map<Integer, String> getMap() {
return map;
}
public void setMap(Map<Integer, String> map) {
this.map = map;
}
}
配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<!-- 父Bean abstract="true" -->
<bean id="parentPets" abstract="true" class="com.xgj.ioc.inject.construct.jihe.merge.Pets">
<property name="map">
<map>
<entry>
<key>
<!-- 為Integer提供值,spring在設定值時,會轉換為定義的Integer型別 -->
<value>111</value>
</key>
<value>cat</value>
</entry>
<entry>
<key>
<value>113</value>
</key>
<value>bird</value>
</entry>
<entry>
<key>
<value>115</value>
</key>
<value>dog</value>
</entry>
</map>
</property>
</bean>
<bean id="pets" parent="parentPets"> <!-- 指定父Bean -->
<property name="map">
<!-- 設定merge="true" 和父bean的同名集合屬性合併 -->
<map merge="true">
<entry>
<key>
<value>117</value>
</key>
<value>monkey</value>
</entry>
</map>
</property>
</bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.merge.PetShop">
<property name="pets" ref="pets" />
</bean>
</beans>
測試類
package com.xgj.ioc.inject.construct.jihe.merge;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectStrongTypeTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/ioc/inject/construct/jihe/merge/beans.xml");
PetShop shop = ctx.getBean("petShop", PetShop.class);
shop.petsInfo();
}
}
執行結果