Spring_DI利用set方法赋值Demo
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的更多相关文章
- js混淆代码还原-js反混淆:利用js进行赋值实现
js混淆代码还原-js反混淆:利用js进行赋值实现 [不想用工具的直接看方法二] 本文地址:http://www.cnblogs.com/vnii/archive/2011/12/14/22875 ...
- iOS开发UI篇-懒加载、重写setter方法赋值
一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...
- 利用scrollintoview方法模拟聊天室收到新消息
这段时间再写一个聊天的功能,基本的原理已经通了,剩下的就是细化功能和实现了.原理通了不代表就能解决了这个问题,今天就遇到了一个小问题,就是在接收到新的消息以后,最新的消息不能显示在消息区域,而是跑到了 ...
- 利用扩展方法重写JSON序列化和反序列化
利用.NET 3.5以后的扩展方法重写JSON序列化和反序列化,在代码可读性和可维护性上更加加强了. 首先是不使用扩展方法的写法 定义部分: /// <summary> /// JSON ...
- 数据库字段值为null利用setInc方法无法直接写入
1.数据库字段值为null利用setInc方法无法直接写入,先判断是否为空,再写入. if($points->add($dataList)){ $user=M('cuser'); $null=$ ...
- YUV420数据和字符信息如何利用滤镜方法进行编码?
YUV420数据和字符信息如何利用滤镜方法进行编码?我希望用ffmpeg中的filter方法,把YUV420数据和字符信息一起编码,该怎么办呢? 本人目前只实现了把yuv420的数据进行h.264的编 ...
- 通过反射对任意class类中方法赋值的方式
import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;i ...
- 在Python的列表中利用remove()方法删除元素的教程
在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...
- 数据可视化之powerBI技巧(二十二)利用这个方法,帮你搞定Power BI"增量刷新"
Power BI的增量刷新功能现在已经对Pro用户开通,但由于种种限制,很多人依然无法使用无这个功能,所以,每一次刷新,都要彻底更新数据集.这对于量级比较大的数据集来说,着实是一件耗费时间的事情. 拿 ...
随机推荐
- Delphi使用NativeXml访问XML文件
Delphi使用NativeXml访问XML文件 1.创建XML文件var Doc: TNativeXml;//声明上下文对象var filepath:string;//文件路径DOC:=TNativ ...
- Linux学习笔记30——套接字
一 什么是套接字 套接字是一种通信机制,凭借这种机制,客户/服务器系统的开发既可以在本地单机上进行,也可以跨网络进行. 二 套接字属性 套接字的特性由3个属性确定,它们是:域,类型和协议 1 套接 ...
- Tree2cycle
Problem Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, w ...
- HDOU/HDU 2548 两军交锋(看你的思维~)
Problem Description 话说辽军与MCA相峙多年,终于在一个秋日的早晨爆发了一次大规模的冲突.情况是这样子的,当天上午,由耶律-Pacision领军的辽军忽然带领数万人马浩浩荡荡向MC ...
- OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(二)——网络节点的安装
序:OpenStack Havana 部署在Ubuntu 12.04 Server [OVS+GRE] 网络节点: 1.安装前更新系统 安装好ubuntu 12.04 Server 64bits后,进 ...
- Google表格
本博文的主要内容有 .Google表格的介绍 https://www.google.com/intl/zh-CN/sheets/about/ https://accounts.google.com/ ...
- TOYS - POJ 2318(计算几何,叉积判断)
题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方 ...
- app启动其他应用
因开发需要内包一个app,所以要启动一个app,这种操作 如果知道包名和类名 其实很简单 只需要将包名内嵌即可(一般情况 我们都可以解压或者反接拿到) 代码如下: Intent intent = ne ...
- java.lang包
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.特性——不用import 2.String String x = "abc"; < ...
- Eclipse(Myeclipse)安装GoogleGWT
1,下载gpe http://code.google.com/p/googleappengine/并安装. 2,下载gwt http://code.google.com/intl/zh-CN/webt ...