Spring 集合注入
Spring注入是spring框架的核心思想之一。在实际的开发中,我们经常会遇见这样一些类的注入,这些类中包含一些集合作为类的属性,那么要怎样想类中的集合注入数据呢?本文通过一个简单的示例向大家介绍一下如何在Spring中完成集合信息的注入。
首先建立一个最基本的web项目:springSetInjection。
干脆利落直接点击Finish,生成springSetInjection项目,框架如下图:
首先向项目中引入Spring开发必要的jar包,将相关包放在lib目录下:
然后在src目录下新建两个package:com.unionpay.beans 和 com.unionpay.controller。从包名就可以看出,这两个package的作用:一个用来装Bean类,一个用来装Controller类。
下面在beans包里面新建两个类:Person.java 和 Injection.java
Person.java
1 package com.unionpay.beans; 2 3 public class Person { 4 5 private String username; 6 private int age; 7 private String address; 8 9 10 public Person(String username, int age, String address) { 11 super(); 12 this.username = username; 13 this.age = age; 14 this.address = address; 15 } 16 17 public Person() { 18 super(); 19 // TODO Auto-generated constructor stub 20 } 21 22 public String getUsername() { 23 return username; 24 } 25 26 public void setUsername(String username) { 27 this.username = username; 28 } 29 30 public int getAge() { 31 return age; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 38 public String getAddress() { 39 return address; 40 } 41 42 public void setAddress(String address) { 43 this.address = address; 44 } 45 46 @Override 47 public String toString() { 48 return "Person [username=" + username + ", age=" + age + ", address=" + address + "]"; 49 } 50 }
Injection.java
1 package com.unionpay.beans; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 public class Injection { 9 10 private List<Object> lists; 11 private Set<Object> sets; 12 private Map<Object,Object> maps; 13 private Properties properties; 14 15 public Injection(List<Object> lists, Set<Object> sets, Map<Object, Object> maps, Properties properties) { 16 super(); 17 this.lists = lists; 18 this.sets = sets; 19 this.maps = maps; 20 this.properties = properties; 21 } 22 23 public Injection() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 public List<Object> getLists() { 29 return lists; 30 } 31 32 public void setLists(List<Object> lists) { 33 this.lists = lists; 34 } 35 36 public Set<Object> getSets() { 37 return sets; 38 } 39 40 public void setSets(Set<Object> sets) { 41 this.sets = sets; 42 } 43 44 public Map<Object, Object> getMaps() { 45 return maps; 46 } 47 48 public void setMaps(Map<Object, Object> maps) { 49 this.maps = maps; 50 } 51 52 public Properties getProperties() { 53 return properties; 54 } 55 56 public void setProperties(Properties properties) { 57 this.properties = properties; 58 } 59 60 public String listInfo(){ 61 return lists.toString(); 62 } 63 64 public String setInfo(){ 65 return sets.toString(); 66 } 67 68 public String mapInfo(){ 69 return maps.toString(); 70 } 71 72 public String propertiesInfo(){ 73 return properties.toString(); 74 } 75 }
然后在src目录下新建spring配置文件:config-beans.xml
config-beans.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" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="injectionBean" class="com.unionpay.beans.Injection"> 7 <property name="lists"> 8 <list> 9 <value>data1</value> 10 <ref bean="person" /> 11 <bean class="com.unionpay.beans.Person"> 12 <property name="username" value="jianxianwch"></property> 13 <property name="age" value="27"></property> 14 <property name="address" value="Shanghai"></property> 15 </bean> 16 </list> 17 </property> 18 19 <property name="sets"> 20 <set> 21 <value>data1</value> 22 <ref bean="person" /> 23 <bean class="com.unionpay.beans.Person"> 24 <property name="username" value="jianxianwch"></property> 25 <property name="age" value="27"></property> 26 <property name="address" value="Shanghai"></property> 27 </bean> 28 </set> 29 </property> 30 31 <property name="maps"> 32 <map> 33 <entry key="key 1" value="data1"></entry> 34 <entry key="key 2" value-ref="person"></entry> 35 <entry key="key 3"> 36 <bean class="com.unionpay.beans.Person"> 37 <property name="username" value="jianxianwch"></property> 38 <property name="age" value="27"></property> 39 <property name="address" value="Shanghai"></property> 40 </bean> 41 </entry> 42 </map> 43 </property> 44 45 <property name="properties"> 46 <props> 47 <prop key="username">admin</prop> 48 <prop key="password">admin</prop> 49 </props> 50 </property> 51 </bean> 52 53 <bean id="person" class="com.unionpay.beans.Person"> 54 <constructor-arg name="username" value="jxwch"></constructor-arg> 55 <constructor-arg name="age" value="27"></constructor-arg> 56 <constructor-arg name="address" value="Anhui"></constructor-arg> 57 </bean> 58 </beans>
从配置文件中,我们可以看出将lists,sets,maps 和properties注入到了injectionBean中。Spring支持这四种集合的注入。
然后在controller包中建立InjectionController.java
1 package com.unionpay.controller; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.unionpay.beans.Injection; 7 8 public class InjectionController { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 13 ApplicationContext context = new ClassPathXmlApplicationContext("config-beans.xml"); 14 15 Injection injection = (Injection) context.getBean("injectionBean"); 16 17 System.out.println(injection.getLists()); 18 System.out.println(injection.getSets()); 19 System.out.println(injection.getMaps()); 20 System.out.println(injection.getProperties()); 21 } 22 23 }
到目前为止,项目建立完成。右键InjectionController.java文件,选择Run As -->Java Application。终端打印出如下信息:
从终端打印信息中可以看见刚才注入的四种集合的数据,示例成功。
源码下载:test.zip