Spring 集合注入

时间:2025-05-09 20:33:14

  Spring注入是spring框架的核心思想之一。在实际的开发中,我们经常会遇见这样一些类的注入,这些类中包含一些集合作为类的属性,那么要怎样想类中的集合注入数据呢?本文通过一个简单的示例向大家介绍一下如何在Spring中完成集合信息的注入。

  首先建立一个最基本的web项目:springSetInjection。

  Spring 集合注入

  干脆利落直接点击Finish,生成springSetInjection项目,框架如下图:

Spring 集合注入

  首先向项目中引入Spring开发必要的jar包,将相关包放在lib目录下:

Spring 集合注入

  然后在src目录下新建两个package:com.unionpay.beans 和 com.unionpay.controller。从包名就可以看出,这两个package的作用:一个用来装Bean类,一个用来装Controller类。

  下面在beans包里面新建两个类:Person.java 和 Injection.java

  Person.java

 package com.unionpay.beans;

 public class Person {

     private String username;
private int age;
private String address; public Person(String username, int age, String address) {
super();
this.username = username;
this.age = age;
this.address = address;
} public Person() {
super();
// TODO Auto-generated constructor stub
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", address=" + address + "]";
}
}

  Injection.java

 package com.unionpay.beans;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Injection { private List<Object> lists;
private Set<Object> sets;
private Map<Object,Object> maps;
private Properties properties; public Injection(List<Object> lists, Set<Object> sets, Map<Object, Object> maps, Properties properties) {
super();
this.lists = lists;
this.sets = sets;
this.maps = maps;
this.properties = properties;
} public Injection() {
super();
// TODO Auto-generated constructor stub
} public List<Object> getLists() {
return lists;
} public void setLists(List<Object> lists) {
this.lists = lists;
} public Set<Object> getSets() {
return sets;
} public void setSets(Set<Object> sets) {
this.sets = sets;
} public Map<Object, Object> getMaps() {
return maps;
} public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} public String listInfo(){
return lists.toString();
} public String setInfo(){
return sets.toString();
} public String mapInfo(){
return maps.toString();
} public String propertiesInfo(){
return properties.toString();
}
}

  然后在src目录下新建spring配置文件:config-beans.xml

  config-beans.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="injectionBean" class="com.unionpay.beans.Injection">
<property name="lists">
<list>
<value>data1</value>
<ref bean="person" />
<bean class="com.unionpay.beans.Person">
<property name="username" value="jianxianwch"></property>
<property name="age" value=""></property>
<property name="address" value="Shanghai"></property>
</bean>
</list>
</property> <property name="sets">
<set>
<value>data1</value>
<ref bean="person" />
<bean class="com.unionpay.beans.Person">
<property name="username" value="jianxianwch"></property>
<property name="age" value=""></property>
<property name="address" value="Shanghai"></property>
</bean>
</set>
</property> <property name="maps">
<map>
<entry key="key 1" value="data1"></entry>
<entry key="key 2" value-ref="person"></entry>
<entry key="key 3">
<bean class="com.unionpay.beans.Person">
<property name="username" value="jianxianwch"></property>
<property name="age" value=""></property>
<property name="address" value="Shanghai"></property>
</bean>
</entry>
</map>
</property> <property name="properties">
<props>
<prop key="username">admin</prop>
<prop key="password">admin</prop>
</props>
</property>
</bean> <bean id="person" class="com.unionpay.beans.Person">
<constructor-arg name="username" value="jxwch"></constructor-arg>
<constructor-arg name="age" value=""></constructor-arg>
<constructor-arg name="address" value="Anhui"></constructor-arg>
</bean>
</beans>

  从配置文件中,我们可以看出将lists,sets,maps 和properties注入到了injectionBean中。Spring支持这四种集合的注入。

  然后在controller包中建立InjectionController.java

 package com.unionpay.controller;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.unionpay.beans.Injection; public class InjectionController { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("config-beans.xml"); Injection injection = (Injection) context.getBean("injectionBean"); System.out.println(injection.getLists());
System.out.println(injection.getSets());
System.out.println(injection.getMaps());
System.out.println(injection.getProperties());
} }

  到目前为止,项目建立完成。右键InjectionController.java文件,选择Run As -->Java Application。终端打印出如下信息:

Spring 集合注入

  从终端打印信息中可以看见刚才注入的四种集合的数据,示例成功。

  源码下载:test.zip