学习笔记(04-02):轻松搞定Spring全家桶(初识篇)-控制反转IOC:集合装配

立即学习:https://edu.csdn.net/course/play/27273/366736?utm_source=blogtoedu

集合装配:

采用基于xml装配Bean的方式:学习笔记(05):轻松搞定Spring全家桶(初识篇)-控制反转IOC:基于XML装配Bean:

例子所需的jar包点击下载

注:均使用属性setter装配的方式

  1. 集合元素:用途(list,set,map与Java含义一样)
    <list>:装配ist类型的值,允许重复
    <set>:装配set类型的值,不允许重复
    <map>:装配map类型的值,名称和值可以是任意类型
    <props>:装配properties类型的值,名称和值必须都是String类型
    <array>:装配数组的值
  2. 例子:
    1. IocCollections.java
      package com.ioc;
      
      import java.util.List;
      import java.util.Map;
      import java.util.Properties;
      import java.util.Set;
      
      public class IocCollections {
      	private List<String> listColl;
      	private Set<String> setColl;
      	private String[] arrayColl;
      	private Map<String, String> mapColl;
      	private Properties propColl;
      	
      	public List<String> getListColl() {
      		return listColl;
      	}
      	public void setListColl(List<String> listColl) {
      		this.listColl = listColl;
      	}
      	public Set<String> getSetColl() {
      		return setColl;
      	}
      	public void setSetColl(Set<String> setColl) {
      		this.setColl = setColl;
      	}
      	public String[] getArrayColl() {
      		return arrayColl;
      	}
      	public void setArrayColl(String[] arrayColl) {
      		this.arrayColl = arrayColl;
      	}
      	public Map<String, String> getMapColl() {
      		return mapColl;
      	}
      	public void setMapColl(Map<String, String> mapColl) {
      		this.mapColl = mapColl;
      	}
      	public Properties getPropColl() {
      		return propColl;
      	}
      	public void setPropColl(Properties propColl) {
      		this.propColl = propColl;
      	}
      	public void show() {
      		System.out.println("list is:"+listColl);
      		System.out.println("set is:"+setColl);
      		System.out.println("array is:");
      		for (int i = 0; i < arrayColl.length; i++) {
      			System.out.println(arrayColl[i]+" ");
      		}
      		System.out.println("mapColl is:"+mapColl);
      		System.out.println("propColl is:"+propColl);
      		
      	}
      	
      }
      
    2. 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-4.3.xsd">
      	 <bean id="iocCollections" class="com.ioc.IocCollections">
      	 	<property name="listColl">
      	 		<list>
      	 			<value>China</value>
      	 			<value>Japan</value>
      	 			<value>America</value>
      	 		</list>
      	 	</property>
      	 	<property name="setColl">
      	 		<set>
      	 			<value>Beijing</value>
      	 			<value>Shanghai</value>
      	 		</set>
      	 	</property>
      	 	<property name="arrayColl">
      	 		<array>
      	 			<value>Qingdao</value>
      	 			<value>Dalian</value>
      	 		</array>
      	 	</property>
      	 	<property name="mapColl">
      	 		<map>
      	 			<entry>
      	 				<key><value>BJ</value></key>
      	 				<value>Beijing</value>
      	 			</entry>
      	 			<!-- 第二种写法  -->
      	 			<entry key="DL" value="Dalian"></entry>
      	 		</map>
      	 	</property>
      	 	<property name="propColl">
      	 		<props>
      	 			<prop key="TW">Tiwan</prop>
      	 			<prop key="HN">Hainan</prop>
      	 			<prop key="DYD">Diaoyudao</prop>
      	 		</props>
      	 	</property>
      	 </bean>
      </beans>
      

       

    3. TestIocCollection.java

      package com.ioc;
      
      import static org.junit.jupiter.api.Assertions.*;
      
      import org.junit.jupiter.api.Test;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      class TestIocCollection {
      
      	@Test
      	void testShow() {
      		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      		IocCollections iocCollections = context.getBean("iocCollections", IocCollections.class);
      		iocCollections.show();
      	}
      
      }
      

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值