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

 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。终端打印出如下信息:

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

  源码下载:test.zip

  

Spring 集合注入的更多相关文章

  1. Spring集合注入

    1.集合注入 上一篇博客讲了spring得属性注入,通过value属性来配置基本数据类型,通过<property>标签的 ref 属性来配置对象的引用.如果想注入多个数据,那我们就要用到集 ...

  2. spring集合类型注入

    spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...

  3. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  4. 没想到吧,Spring中还有一招集合注入的写法

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. Spring作为项目中不可缺少的底层框架,提供的最基础的功能就是bean的管理了.bean的注入相信大家都比较熟 ...

  5. Spring(二)scope、集合注入、自动装配、生命周期

    原文链接:http://www.orlion.ga/189/ 一.scope bean的scope属性中常用的有两种:singleton(单例,默认)和prototype(原型,每次创建新对象) 例: ...

  6. SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DI和IOC相比,DI更偏向于实现 DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说 ...

  7. Spring中集合注入方法

    集合注入重要是对数组.List.Set.map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式): 1.在工程中新建一个Departmen ...

  8. 在Spring中注入Java集合

    集合注入重要是对数组.List.Set.map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式): 1.在工程中新建一个Departmen ...

  9. Spring之注入的几种方式

    普通注入 在配置文件里 <!-- 构造注入 --> <bean id="user1" class="entity.User"> < ...

随机推荐

  1. ExecuteScalar,ExecuteReader,ExecuteNonQuery区别

    ExecuteScalar is typically used when your query returns a single value. If it returns more, then the ...

  2. ICSharpCode.SharpZipLib工具压缩与解压缩zip文件

    using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...

  3. AutoFac文档8(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 泛型 给定一个开放的泛 ...

  4. 为什么会找不到D层文件?

    近期两天在重装系统,今天好不easy把各种东西都装齐全了,再打开我的机房收费系统,就提演示样例如以下错误: 看到这个问题.我感觉非常熟,由于曾经也遇到过两次这个问题,都是改了下D层的编译路径.改到了U ...

  5. PKI/CA

    PKI( Public Key Infrastructure )指的是公钥基础设施. CA ( Certificate Authority )指的是认证中心. PKI从技术上解决了网络通信安全的种种障 ...

  6. 如何使用微信JS-SDK实际分享功能

    http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=7

  7. 导出word功能,用html代码在word中插入分页符

    <span lang=EN-US style="font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:" mce_st ...

  8. 设置open_cursors参数

    1.进入终端,输入命令:sqlplus /nolog 2.输入命令:conn /as sysdba 3.输入命令:alter system set open_cursors=1000 scope=me ...

  9. am335x 一个按键实现重置 ip 和 root passwd

    * 其实做法很简单,我连按键驱动都没有去写,读取 gpio 的值然后 拷贝了一份 /etc/network/interfaces_bak 为 interfaces ,用脚本重新设置了一次root 密码 ...

  10. Android基础总结(五)网络通信2

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...