这是一个Demo

1、Phone.java

package com.cn.pojo;

public class Phone {

    private String name;
private double price; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Phone(String name, double price) {
super();
this.name = name;
this.price = price;
}
public Phone() {
super();
// TODO Auto-generated constructor stub
}
}

2、Student.java

package com.cn.pojo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class Student { private int stuId;
private String name;
private Phone phone;
private List<String> favorties = new ArrayList<String>();;
private Map<String,String> jobTime = new HashMap<String,String>(); public List<String> getFavorties() {
return favorties;
}
public void setFavorties(List<String> favorties) {
this.favorties = favorties;
}
public Map<String, String> getJobTime() {
return jobTime;
}
public void setJobTime(Map<String, String> jobTime) {
this.jobTime = jobTime;
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student() {
super();
System.out.println("come in...");
}
public Student(int stuId, String name) {
super();
this.stuId = stuId;
this.name = name;
} public void init(){
System.out.println("初始化。。。");
} public void destroy(){
System.out.println("销毁。。。");
}
}

3、Test.java

package com.cn.test;

import java.util.Iterator;
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.cn.pojo.Student; public class Test { @org.junit.Test
public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); System.out.println(stu.getName()); } @org.junit.Test
public void test2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); System.out.println(stu.getPhone().getName()); } @org.junit.Test
public void test3(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); List<String> favs = stu.getFavorties(); for(String str : favs){
System.out.println(str);
} } @org.junit.Test
public void test4(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student)applicationContext.getBean("stu"); Map<String,String> maps = stu.getJobTime(); Set<String> keys = maps.keySet(); Iterator<String> iters = keys.iterator(); while(iters.hasNext()){
String key = iters.next();
System.out.println("key:"+key+",value:"+maps.get(key));
} } }

4、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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="stu" class="com.cn.pojo.Student" init-method="init" destroy-method="destroy">
<property name="name" value="XXX"></property>
<property name="phone" ref="iphone"></property>
<property name="favorties" >
<list>
<value>吃饭</value>
<value>睡觉</value>
<value>看电影</value>
</list>
</property>
<property name="jobTime">
<map>
<entry>
<key><value>AM</value></key>
<value>lol</value>
</entry>
<entry>
<key><value>PM</value></key>
<value>吃鸡</value>
</entry>
</map>
</property>
</bean> <bean id="iphone" class="com.cn.pojo.Phone">
<property name="name" value="iphongX"></property>
</bean> </beans>

Spring Bean的ref属性和IoC注入集合的更多相关文章

  1. Spring学习总结(5)——IOC注入方式总结

    一.构造注入 在类被实例化的时候,它的构造方法被调用并且只能调用一次.所以它被用于类的初始化操作.<constructor-arg>是<bean>标签的子标签.通过其<v ...

  2. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  3. Spring - <bean parent="xxx" 属性>

    总结 必要条件: 1.子bean必须与父bean保持兼容,也就是说子bean中必须有父bean定义的所有属性. 2.父bean必须是抽象bean或者定义lazy-init=true也就是不让bean工 ...

  4. Spring的DI(Ioc) - 注入集合类型

    1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...

  5. Spring - bean的lazy-init属性(懒加载)

    默认情况下,容器初始化的时候便会把bean实例化,通常这样做可以让一些配置或者bean实例化的异常在容器启动的时候就发现,而不是在N久之后.但有时候,我们希望某个可能不会用到但又不是100%不用的be ...

  6. spring bean标签常用属性

    一.id属性 其名称,可以是任意名称,但不能包含特殊符号. 根据id得到配置对象. 二.class属性 创建对象所在的类名称 三.name属性 功能和id属性一样,但name属性值可以包含特殊属性 四 ...

  7. 【转载】Spring bean 中 constructor-arg属性

    转载地址:https://blog.csdn.net/qq_27292113/article/details/78063696 方便以后查阅

  8. Spring学习3—控制反转(IOC)Spring依赖注入(DI)和控制反转(IOC)

    一.思想理解 Spring 能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的Service对象,还是持久层的DAO对象,都可在Spring的 管理下有机地协调.运行.S ...

  9. spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现

    知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...

随机推荐

  1. Windows server install mrtg

    由于MRTG使用Perl语言编写 , 安装ActivePerl http://downloads.activestate.com/ActivePerl/releases/5.20.1.2000/Act ...

  2. 多IP加强SSH的安全性

    本文针对一台服务器有多个网卡及IP地址的情况,可以限制某些IP不监听SSH,只允许通过某些IP来登陆 以下配置项在/etc/ssh/sshd_config文件中修改 比如你有4个网卡: eth0 – ...

  3. [HAOI2012]道路(最短路DAG上计数)

    C国有n座城市,城市之间通过m条[b]单向[/b]道路连接.一条路径被称为最短路,当且仅当不存在从它的起点到终点的另外一条路径总长度比它小.两条最短路不同,当且仅当它们包含的道路序列不同.我们需要对每 ...

  4. smtp

    新闻系统的定时通知初步有三种实用方式,1.短信 2.邮箱 3.微信 短信就不得不使用第三方平台,虽说5分一条,但耐不住量大,一天1000条的话,50元也是一笔不小的支出. 这时,邮箱和微信的优势就体现 ...

  5. 【STM32】PWM DAC基本原理(实验:PWM实现DAC)

    虽然STM32F103ZET6具有内部DAC,但是也仅仅只有两条DAC通道,并且STM32还有其他的很多型号是没有DAC的.通常情况下,采用专用的D/A芯片来实现,但是这样就会带来成本的增加. 不过S ...

  6. Python3 与 C# 并发编程之~ 线程篇

      2.线程篇¶ 在线预览:https://github.lesschina.com/python/base/concurrency/3.并发编程-线程篇.html 示例代码:https://gith ...

  7. Python基础之文件和目录操作

    1 .文件操作 1.1 文件打开和关闭 在python, 使用 open 函数, 可以打开一个已经存在的文件, 或者创建一个新文件. # 打开文件 f = open('test.txt', 'w') ...

  8. 使用Spring Cloud连接不同服务

    http://www.infoq.com/cn/articles/spring-cloud-service-wiring 主要结论 Spring Cloud为微服务系统中相互依赖的服务提供了丰富的连接 ...

  9. macs 学习

    点击 首先andriomianfest主配文件(主要配置文件),来启动主要的activity对象,然后通过该对象调用create方法来加载布局文件xml active通过布局文件的控件生成相应的对象. ...

  10. 我们数学中常用的自然常数e代表什么?看完长知识了!

    我们在学习期间都接触过自然常数e,也知道e ≍ 2.718,学过极限的同学应该也知道 那么大家知道e的含义是什么吗?为啥叫“自然常数”? e的含义可以用一个计算利息的例子来解释. 假如你有1块钱,银行 ...