这是一个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. BZOJ4671 异或图(容斥+线性基)

    题意 定义两个结点数相同的图 \(G_1\) 与图 \(G_2\) 的异或为一个新的图 \(G\) ,其中如果 \((u, v)\) 在 \(G_1\) 与 \(G_2\) 中的出现次数之和为 \(1 ...

  2. Leetcode 75.颜色分类 By Python

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  3. Java 类设计技巧

    摘自<Java核心技术>卷I:基础知识 p140 第4章对象与类 - 类设计技巧 1)一定将数据设计为私有. 最重要的是:绝对不要破坏封装性.有时候,需要编写一个访问器方法或更改器方法,但 ...

  4. python列表转字符串

    temp = "".join(sorted(arr[i])) arr[i] = temp

  5. C++类相关

    本文打算通过一些小例子来说明几个关键的知识点. 一:成员函数相关 #include <iostream> using namespace std; class D { public: vo ...

  6. Python中的实例方法、classmethod和staticmethod的区别

    class NewsPaper(object): # 类属性 __print_times = 0 # 下划线表示私有属性 # 实例方法 def __init__(self, title, conten ...

  7. Spring的核心

    技术书籍这么多,每次好不容易读完一本,但总过不了多久就会遗忘.为了对抗,整理记录和回看,也是实属必要.由此,从这<Spring 实战(第四版)>开始,记录一下知识点,下次再要复习时,能免去 ...

  8. Building real-time dashboard applications with Apache Flink, Elasticsearch, and Kibana

    https://www.elastic.co/cn/blog/building-real-time-dashboard-applications-with-apache-flink-elasticse ...

  9. 在MacOS上使用gdb(cgdb)调试Golang程序

    如果你在MacOS上使用GDB工具载入Golang程序时无法载入,这篇文章可以解决.本文不具体介绍调试的方法,网上的文章太多了就不赘述了. cgdb使用的是gdb的内核,方法和原理试用本文. 问题分析 ...

  10. axios请求、返回拦截器

    1.http 请求拦截器 axios.interceptors.request.use(function(config){ //在发送请求之前做些什么 return config }), functi ...