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. Dev属性设置

    DisplayFormat 设置显示格式如:{0:P}表示显示为百分号模式.如数据源中为0.5.表示出来为50% 2.GridContro总合计及分组合计: 常规总合计直接RunDesigner-Gr ...

  2. python 日志的配置,python对日志封装成类,日志的调用

    # python 日志的配置,python对日志封装成类,日志的调用 import logging # 使用logging模块: class CLog: # --------------------- ...

  3. AutoFac文档5(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 扫描 autofac可 ...

  4. ubuntu下查找jdk安装位置

    which javac 返回/usr/bin/javac file /usr/bin/javac 返回/usr/bin/javac: symbolic link to `/etc/alternativ ...

  5. IBM websphere MQ 消息发送与获取

    一. 所需依赖包,安装 IBM websphere MQ 后,在安装目录下的 java 目录内 import java.io.IOException; import java.util.Propert ...

  6. WPF集合

    Dependency Property 依赖属性 http://www.cnblogs.com/HelloMyWorld/archive/2013/02/21/2920149.html Attache ...

  7. oracle 显示格式化

    sqlplus中:set wrap off; set pagesize 1000; set linesize 1000; col id format A20; //该字段最长显示20个字符 col n ...

  8. git学习(一):git的版本库在哪儿

    查看版本 git --version # 查看git的版本 设置或者查看用户名和邮箱 git config --global user.name "tuhooo" // 如果后面没 ...

  9. Linux系统编程--read/write

    . read/write read函数从打开的设备或文件中读取数据. #include <unistd.h> ssize_t read(int fd, void *buf, size_t ...

  10. Tomcat性能优化(二) 启动参数设置

    一.tomcat绿色版设置方法 进入tomcat/bin目录下,找到catalina.bat文件在文件首行中插入下面这段配置即可. set JAVA_OPTS=-server -Djava.awt.h ...