一、集合注入
<bean id="mc" class="com.gql.di.MyCollection">
<property name="list">
<list>
<value>13</value>
<value>14</value>
</list>
</property>
<property name="set">
<set>
<value>21</value>
<value>22</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="gql"></entry>
<entry key="2" value="zdy"></entry>
</map>
</property>
<property name="prop">
<props>
<prop key="name" >Tom</prop>
<prop key="name" >Jon</prop>
</props>
</property>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
成功通过集合注入对象。
(1)集合标签
- 用于给
List结构
集合注入的标签:list
,array
,set
。 - 用于给
Map结构
集合注入的标签:map
,props
。 - 结构相同,标签可以互换。
(2)测试集合注入
package com.gql.di;
import javax.swing.plaf.synth.SynthSpinnerUI;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gql.entity.Person;
/**
* 类说明:
* 测试集合注入
* @guoqianliang1998.
*/
public class Demo {
@Test
public void testCollection(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
MyCollection mc = (MyCollection) ac.getBean("mc");
//打印List集合
System.out.println("List集合:"+mc.getList());
//打印Set集合
System.out.println("Set集合:"+mc.getSet());
//打印Map集合
System.out.println("Map集合:"+mc.getMap());
//打印Prop集合
System.out.println("Prop集合:"+mc.getProp());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
(3)MyCollection实体类
package com.gql.di;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* 类说明:
* 集合实体类
* @guoqianliang1998.
*/
public class MyCollection {
private List list;
private Set set;
private Map map;
private Properties prop;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
(4)XML配置
applicationContext_Di.xml(局部配置)
<?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="mc" class="com.gql.di.MyCollection">
<property name="list">
<list>
<value>13</value>
<value>14</value>
</list>
</property>
<property name="set">
<set>
<value>21</value>
<value>22</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="gql"></entry>
<entry key="2" value="zdy"></entry>
</map>
</property>
<property name="prop">
<props>
<prop key="name" >Tom</prop>
<prop key="name" >Jon</prop>
</props>
</property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
applicationContext.xml(全局配置)
<?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">
<import resource="com/gql/di/applicationContext_Di.xml"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
转载:https://blog.csdn.net/weixin_43691058/article/details/104096832
查看评论