集合注入
前面我们已经能完成引入数据类型和简单数据类型的注入,但是还有一种数据类型集合,集合中既可
以装简单数据类型也可以装引用数据类型,对于集合,在Spring中该如何注入呢?
先来回顾下,常见的集合类型有哪些?
- 数组
- List
- Set
- Map
- Properties
针对不同的集合类型,该如何实现注入呢?
环境准备
- 创建一个Maven项目
- pom.xml添加依赖
- resources下添加spring的配置文件applicationContext.xml
这些步骤和前面的都一致,大家可以快速的拷贝即可,最终项目的结构如下:
(1)项目中添加添加BookDao、BookDaoImpl类
1 public interface BookDao {2 public void save();3 }4 5 public class BookDaoImpl implements BookDao {6 7 public class BookDaoImpl implements BookDao {8 9 private int[] array;10 11 private List<String> list;12 13 private Set<String> set;14 15 private Map<String,String> map;16 17 private Properties properties;18 19 public void save() {20 System.out.println("book dao save ...");21 22 System.out.println("遍历数组:" + Arrays.toString(array));23 24 System.out.println("遍历List" + list);25 26 System.out.println("遍历Set" + set);27 28 System.out.println("遍历Map" + map);29 30 System.out.println("遍历Properties" + properties);31 }32 //setter....方法省略,自己使用工具生成
33 }
(2)resources下提供spring的配置文件,applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">5 6 <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>7 </beans>
(3)编写AppForDICollection运行类,加载Spring的IOC容器,并从中获取对应的bean对象
1 public class AppForDICollection {2 public static void main( String[] args ) {3 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");4 BookDao bookDao = (BookDao) ctx.getBean("bookDao");5 bookDao.save();6 }7 }
接下来,在上面这个环境中来完成 集合注入 的学习:
下面的所以配置方式,都是在bookDao的bean标签中使用进行注入
1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">5 6 <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">7 8 </bean>9 </beans>
注入数组类型数据
1 <property name="array">2 <array>3 <value>100</value>4 <value>200</value>5 <value>300</value>6 </array>7 </property>
注入List类型数据
1 <property name="list">2 <list>3 <value>itcast</value>4 <value>itheima</value>5 <value>boxuegu</value>6 <value>chuanzhihui</value>7 </list>8 </property>
注入Set类型数据
1 <property name="set">2 <set>3 <value>itcast</value>4 <value>itheima</value>5 <value>boxuegu</value>6 <value>boxuegu</value>7 </set>8 </property>
注入Map类型数据
1 <property name="map">2 <map>3 <entry key="country" value="china"/>4 <entry key="province" value="henan"/>5 <entry key="city" value="kaifeng"/>6 </map>7 </property>
map需要entry的写法。因为map是一个键值对的形式
注入Properties类型数据
1 <property name="properties">2 <props>3 <prop key="country">china</prop>4 <prop key="province">henan</prop>5 <prop key="city">kaifeng</prop>6 </props>7 </property>
配置完成后,运行下看结果:
说明:
- property标签表示setter方式注入,构造方式注入constructor-arg标签内部也可以写<array>、<list>、<set>、<map>、<props>标签
- List的底层也是通过数组实现的,所以<list>和<array>标签是可以混用
- 集合中要添加引用类型,只需要把<value>标签改成<ref>标签,这种方式用的比较少