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. redis配置master-slave模式

    由于云服务器存在闪断现象,项目线上会存在基于redis的功能在闪断时段内出现异常,所以redis需要做master-slave模式.直接上代码: 原单机redis,RedisConnectionFac ...

  2. asp.net mvc部署iis常见问题

    1.Q:iis比网上的少很多选项 A:iis没装全,去控制面板里把没勾选的选项勾选 2.Q:发布mvc遇到的HTTP错误 403.14-Forbidden解决办法 A:需要在web.config里添加 ...

  3. JMeter学习笔记(三)

    D. User Defined Variables:用户自定义的变量,在此我们可以定义后面原件需要引用的变量并对其进行赋值.jsessionid一般是服务器返回的,每个用户返回的都不一样,所以在此不应 ...

  4. HBase源代码分析之HRegionServer上MemStore的flush处理流程(二)

    继上篇文章<HBase源代码分析之HRegionServer上MemStore的flush处理流程(一)>遗留的问题之后,本文我们接着研究HRegionServer上MemStore的fl ...

  5. Nginx 使用中文URL,中文目录路径

    Nginx 使用中文URL,中文目录路径 分类: linux2012-05-03 11:04 2672人阅读 评论(0) 收藏 举报 nginxurl服务器translationcentosserve ...

  6. NGINX + LUA实现复杂的控制

    安装lua_nginx_module 模块 lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty Centos和debian的安装就简单了.. 这里说下freebs ...

  7. dp之01背包hdu2639(第k优解)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值.......(注意,不要 和它的第一题混 ...

  8. 一款纯css3实现的发光屏幕旋转特效

    今天给大家带来一款纯css3实现的发光屏幕旋转特效.该屏幕由纯css3实现带发光旋转特效,效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="s ...

  9. GuozhongCrawler看准网爬虫动态切换IP漫爬虫

    有些关于URL去重的方面代码没有提供,须要自己去实现.主要这里提供思路 项目地址:http://git.oschina.net/woshidaniu/GuozhongCrawler/tree/mast ...

  10. Latex之希腊字母表 花体字母 实数集

    花体字母 \mathcal{x} 实数集字母 \mathbb{R} 转自:http://blog.csdn.net/lanchunhui/article/details/49819445 拉丁字母是2 ...