spring:为JavaBean的集合对象注入属性值

在 spring 中可以对List、Set、Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合。

1.创建 TsetUtil 类,在该类中定义List、Set、Map 类型的属性,并设置getter 和 setter 方法。代码如下

package com.importnew;
import java.util.List;
import java.util.Map;
import java.util.Set; public class TestUtil {
private List list;
private Map map;
private Set set; public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
} public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
} public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
}

2.在 spring 配置文件中对 TsetUtil 进行配置,并通过<list>,<set>,<map>为 TsetUtil 的List、Set、Map集合属性赋值。applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="testUtil" class="com.importnew.TestUtil" >
<property name="list">
<list>
<value>list 集合的第一个元素</value>
<value>list 集合的第二个元素</value>
<value>list 集合的第三个元素</value>
</list>
</property>
<property name="set">
<set>
<value>张三</value>
<value>李四</value>
</set>
</property>
<property name="map">
<map>
<entry key="key1" value="java从基础到项目死战" />
<entry key="key2" value="java开发" />
</map>
</property>
</bean>
<bean id="user" class="com.importnew.User"></bean>
</beans>

3.编写测试类 TestSpring ,代码如下:

package test;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.importnew.TestUtil; public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestUtil testUtil = (TestUtil) context.getBean("testUtil"); List lists = testUtil.getList();
for(Object ss:lists){
System.out.println(ss.toString());
} Set sets = testUtil.getSet();
for(Object ss:sets){
System.out.println(ss.toString());
} Map<String,String> maps = testUtil.getMap();for (Map.Entry<String, String> entry : maps.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
}

———————————————————————————————————————————————————

备注:向集合中添加对象类型的元素时,<list>,<set>,<map>不仅可以添加 Stirng 类型的元素,而且可以添加对象类型的元素。如下代码实现了向集合中添加了对象user:

<bean id="testUtil" class="com.importnew.TestUtil"  >
<property name="list">
<list>
<value>list 集合的第一个元素</value>
<value>list 集合的第二个元素</value>
<value>list 集合的第三个元素</value>
<ref bean="user"/>
</list>
</property>
<property name="set">
<set>
<value>张三</value>
<value>李四</value>
<ref bean="user"/>
</set>
</property>
<property name="map">
<map>
<entry key="key1" value="java从基础到项目死战" />
<entry key="key2" value="java开发" />
<entry key="user">
<ref bean="user"/>
</entry>
</map>
</property>
</bean>
<bean id="user" class="com.importnew.User"></bean>

////end

spring:为javabean的集合对象注入属性值的更多相关文章

  1. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

  2. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  3. (转)Spring如何装配各种集合类型的属性

    http://blog.csdn.net/yerenyuan_pku/article/details/52858499 在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合 ...

  4. spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

    spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...

  5. 【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更

    java的反射实现: 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更 今日份代码: package com.sxd.streamTest; imp ...

  6. JQuery 操作对象的属性值

    通过JQuery去操作前台对象(div,span...)的属性是很常见的事情,本文就简单的介绍几种操作情形. 1):通过属性值去获取对象 2):用JQuery去修改对象的属性值 3):获取并修改对象的 ...

  7. jquery attr()方法 添加,修改,获取对象的属性值。

    jquery attr()方法 添加,修改,获取对象的属性值. jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到 ...

  8. 读匿名object对象的属性值

    读匿名object对象的属性值 1.定义读object对象值的功能方法 public static class StaticClass { public static string ValueByKe ...

  9. WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)

    WPFS数据绑定(要是后台类对象的属性值发生改变,通知在"client界面与之绑定的控件值"也发生改变须要实现INotitypropertyChanged接口) MainWindo ...

随机推荐

  1. Android开发笔记(6)——类的设定与继承

    转载请注明http://www.cnblogs.com/igoslly/p/6838991.html [类]的设定与继承 当设置相同格式的TextView时,已提出在styles.xml自定义格式统一 ...

  2. Microsoft SQL Server学习(二)--数据库的语法

    关于数据库的语法 创建数据库 样例 名词概念 编写数据库代码的注意事项 关于文件语法 实例代码 关于数据库的语法: 1.创建数据库 create database 数据库名 on primary (主 ...

  3. 关闭掉eclipse启动的自动更新功能(提高打开eclipse的速度)

  4. js 学习笔记---BOM

    window对象 1. window 对象是Global对象,在全局作用域中声明的变量和函数都可以通过window.来访问.跟直接在window上添加属性效果一样.唯一的区别就是delete时,如果是 ...

  5. Nginx 监控

    编译安装加上http_stub_status_module 模块 location /status { stub_status on; # access_log /usr/local/nginx/lo ...

  6. Linux Shell ssh登录脚本

    Linux 登陆服务器敲命令太多,某时候确实不便,所以就用shell写了一个  我的blog地址: http://www.cnblogs.com/caoguo 一.说明 支持秘密和密钥两种格式 用户名 ...

  7. jquery jstree 插件的使用

    最近一个项目 需要用到jstree 这个jQuery插件,就研究了下,做目录树 菜单还是很强大的,下面对经常会用到几个用法做下说明. 1. 首先页面 引用 jquery.jstree 2. html ...

  8. (转)OGNL与值栈

    http://blog.csdn.net/yerenyuan_pku/article/details/67709693 OGNL的概述 什么是OGNL 据度娘所说: OGNL是Object-Graph ...

  9. swift class protocol-限定协议只能由类实现

    protocol GameMode:class “You can limit protocol adoption to class types (and not structures or enume ...

  10. js 获取 checkbox[] 值

    $("#btnAdd1").click(function () { console.log($("form").serialize()); var checkb ...