这是一个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. C# 新语法收集

    内联变量 使用int.tryparst时,先要申明变量,用于out参数 int d; int.tryparse(s,out d); 使用内联变量写法可以如下.功能一样简化了写化 int.trypars ...

  2. 「SCOI2014」方伯伯运椰子 解题报告

    「SCOI2014」方伯伯运椰子 可以看出是分数规划 然后我们可以看出其实只需要改变1的流量就可以了,因为每次改变要保证流量守恒,必须流成一个环,在正负性确定的情况下,变几次是无所谓的. 然后按照套路 ...

  3. Typescript学习笔记(四)class 类

    typescript的类,与c#,java等语言的类类似.也是包含了一大部分的es6的实现.我会用最通俗的语言讲一下对coding有用的地方. class Greeter { greeting: st ...

  4. 深入学习semaphore

    深入学习semaphore 控制同时访问资源线程数 访问特定资源前,先使用acquire(1)获得许可,如果许可数量为0,该线程则一直阻塞,直到有可用许可. 访问资源后,使用release()释放许可 ...

  5. Java 判断相等

    1.基本数据类型用 == long a = (long) 1234567890; long b = (long) 1234567890; if (a == b) { System.out.printl ...

  6. Ecplise 快捷键笔记

    1.显示出这个方法被哪些方法调用(Ctrl+Alt+H) 选中方法名,点右键,选“open call hierarchy”,其快捷键“Ctrl+Alt+H”,Eclipse就会显示出这个方法被哪些方法 ...

  7. 洛谷P1712 区间

    题意:给你n个区间,从中选择m个,使得它们有交,且最长与最短区间的差值最小. 解:这道题我想了好多的,nlog²n错的,nlogn错的,最后终于想出nlogn的了...... 把区间按照长度排序,然后 ...

  8. [NOI2018]归程

    今年D1T1,平心而论,如果能想到kruskal重构树还是很简单的. ......苟屁啊!虽然跟其他的比是简单些,但是思维难度中上,代码难度中上,怎么看都很符合NOI T1啊. 本题还有可持久化并查集 ...

  9. 跟着 underscore 学节流

    更多内容请参考:我的新博客 在上一篇文章中,我们了解了为什么要限制事件的频繁触发,以及如何做限制: debounce 防抖 throttle 节流 上次已经说过防抖的实现了,今天主要来说一下节流的实现 ...

  10. ImageMagick: 6.8.3 升级到 6.8.9 遇到的问题

    最终还是决定升级到目前最新版:6.8.9,不知何时才真正明白为什么现在都是java8,但还是有很多软件系统使用在java5上. 虽然新版本能带来各种好处,但现实中不能忽略一个问题:原来的代码很可能无法 ...