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. C#验证码

    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...

  2. POI做题记录:第二届POI

    Trees Memory limit: 32 MB Trees occur very often in computer science. As opposed to trees in nature, ...

  3. C# Dictionary的xml序列化

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  4. LCD显示的一些基本概念以及DSI的一些clock解释

     数字视频的基本概念源自于模拟视频.对于模拟视频我们可以这样理解:视频可以分解为若干个基本视点(像素),每个像素都有独立的色彩信息,在屏幕上依次将 这些点用电子枪按照行和列打出来,就形成了一幅完整画面 ...

  5. spring 学习的开源项目

    http://jinnianshilongnian.iteye.com/blog/1508016 http://jinnianshilongnian.iteye.com/blog/2049092 sp ...

  6. Cocos2d-x CCActionInterval

    第一部分:CCActionInterval家族(持续动作) 持续动作,顾名思义,就是该动作的执行将持续一段时间.因此持续动作的静态生成函数,往往附带一个时间值Duration.例如: CCAction ...

  7. dp poj 1080 Human Gene Functions

    题目链接: http://poj.org/problem?id=1080 题目大意: 给两个由A.C.T.G四个字符组成的字符串,可以在两串中加入-,使得两串长度相等. 每两个字符匹配时都有个值,求怎 ...

  8. dede 如何去除[field:title/]里面出现的b标签

    调用[field:title/]标签,两边加<b>怎么去掉<b> 怎么回事??? 最近更新dede的版本后,调用[field:title/]标签,生成的标题两边会自动在标题两边 ...

  9. 栈的链式存储 - API实现

    基本概念 其它概念详情參看前一篇博文:栈的顺序存储 - 设计与实现 - API实现 这里也是运用了链表的链式存储API高速实现了栈的API. 代码: // linkstack.h // 链式存储栈的A ...

  10. 【2014】【辛星】【php】【秋季】【2】第一个php程序

    <span style="font-family:KaiTi_GB2312;font-size:18px;">*******************设置server** ...