Person.java

public class Person {
private Long pid;
private String pname;
private Student student;
private List list;
private Set set;
private Map map;
private Properties properties;
//get和set方法
}

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="person" class="cn.test.di.Person">
<!--
property就是代表属性
在spring中基本类型(包装类型和String)都可以用value来赋值
引用类型用ref赋值
-->
<property name="pid" value="5"></property>
<property name="pname" value="张三"></property><!-- 基本数据类型 -->
<property name="student" ref="student"></property><!-- 引用类型 -->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property> <property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="map1">
<value>map1</value>
</entry>
<entry key="map2">
<value>map2</value>
</entry>
<entry key="map3">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="pro1">
pro1
</prop>
</props>
</property>
</bean>
<bean name="student" class="cn.test.di.Student"></bean>
</beans>

测试

 @Test
public void doSome(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/di/applicationContext.xml");
Person person= (Person) applicationContext.getBean("person");
System.err.println(person.getPid());
System.err.println(person.getPname());
person.getStudent().say();
List list=person.getList();
for(int i=0;i<list.size();i++){
System.err.println(list.get(i).toString());
}

处理使用上面的get方法进行赋值外,还可以通过构造函数来赋值,首先在类中添加构造函数,然后进行配置

public Person(String pname, Student student) {
this.pname = pname;
this.student = student;
}
 <!--
构造函数的参数
index 第几个参数,下标从0开始
type 参数的类型
ref 如果类型是引用类型,赋值
value 如果类型是基本类型,赋值
说明:
只能指定一个构造函数
-->
<constructor-arg index="0" type="java.lang.String" value="李四"></constructor-arg>
<constructor-arg index="1" type="cn.test.di2.Student" ref="student"></constructor-arg>
 @Test
public void doSome(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/di2/applicationContext.xml");
Person person= (Person) applicationContext.getBean("person");
System.err.println(person.getPname());
person.getStudent().say();
}

Spring_DI利用set方法赋值Demo的更多相关文章

  1. js混淆代码还原-js反混淆:利用js进行赋值实现

    js混淆代码还原-js反混淆:利用js进行赋值实现   [不想用工具的直接看方法二] 本文地址:http://www.cnblogs.com/vnii/archive/2011/12/14/22875 ...

  2. iOS开发UI篇-懒加载、重写setter方法赋值

    一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...

  3. 利用scrollintoview方法模拟聊天室收到新消息

    这段时间再写一个聊天的功能,基本的原理已经通了,剩下的就是细化功能和实现了.原理通了不代表就能解决了这个问题,今天就遇到了一个小问题,就是在接收到新的消息以后,最新的消息不能显示在消息区域,而是跑到了 ...

  4. 利用扩展方法重写JSON序列化和反序列化

    利用.NET 3.5以后的扩展方法重写JSON序列化和反序列化,在代码可读性和可维护性上更加加强了. 首先是不使用扩展方法的写法 定义部分: /// <summary>  /// JSON ...

  5. 数据库字段值为null利用setInc方法无法直接写入

    1.数据库字段值为null利用setInc方法无法直接写入,先判断是否为空,再写入. if($points->add($dataList)){ $user=M('cuser'); $null=$ ...

  6. YUV420数据和字符信息如何利用滤镜方法进行编码?

    YUV420数据和字符信息如何利用滤镜方法进行编码?我希望用ffmpeg中的filter方法,把YUV420数据和字符信息一起编码,该怎么办呢? 本人目前只实现了把yuv420的数据进行h.264的编 ...

  7. 通过反射对任意class类中方法赋值的方式

    import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;i ...

  8. 在Python的列表中利用remove()方法删除元素的教程

    在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...

  9. 数据可视化之powerBI技巧(二十二)利用这个方法,帮你搞定Power BI"增量刷新"

    Power BI的增量刷新功能现在已经对Pro用户开通,但由于种种限制,很多人依然无法使用无这个功能,所以,每一次刷新,都要彻底更新数据集.这对于量级比较大的数据集来说,着实是一件耗费时间的事情. 拿 ...

随机推荐

  1. 2016年QS亚洲大学排行榜

    2016年QS亚洲大学排行榜 人民网北京6月14日电 (记者 郝孟佳)今天,全球高等教育分析机构QS全球教育集团发布了2016年QS亚洲大学排名.清华大学在亚洲20强大学中进步最大,比去年提升6名,上 ...

  2. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  3. Linux Kernel空指针引用本地拒绝服务漏洞(CVE-2013-5634)

    漏洞版本: Linux kernel 漏洞描述: BUGTRAQ ID: 61995 CVE(CAN) ID: CVE-2013-5634 Linux Kernel是Linux操作系统的内核. 适用于 ...

  4. Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理

    题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出 ...

  5. [MCM]2014年美赛MCM题目原文及翻译

    PROBLEM A: The Keep-Right-Except-To-Pass Rule In countries where driving automobiles on the right is ...

  6. Java 集合框架 LinkedHashSet 和 LinkedHashMap 源码剖析

    总体介绍 如果你已看过前面关于HashSet和HashMap,以及TreeSet和TreeMap的讲解,一定能够想到本文将要讲解的LinkedHashSet和LinkedHashMap其实也是一回事. ...

  7. hdoj 1969 Pie【二分】

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. c++重点知识点

    - const加强 在变量前加const,说明变量是常量只读属性.假如用指针去修改const常量会用什么结果.上例子: //a 是一个只读的常量,按照理论应该不能被修改 ;//内存中为a分配地址,赋值 ...

  9. Assigning retained object to unsafe property;object will be released after assignment

    解决方法,将变量 @property (assign) UILabel *titleView; 改为 @property (retain) UILabel *titleView;

  10. Apache开启Gzip压缩设置(转)

    第一步: 查看一下/etc/httpd/conf/httpd.conf中下面两个模块是否开启: LoadModule deflate_module modules/mod_deflate.so Loa ...