Spring Bean的ref属性和IoC注入集合
这是一个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注入集合的更多相关文章
- Spring学习总结(5)——IOC注入方式总结
一.构造注入 在类被实例化的时候,它的构造方法被调用并且只能调用一次.所以它被用于类的初始化操作.<constructor-arg>是<bean>标签的子标签.通过其<v ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- Spring - <bean parent="xxx" 属性>
总结 必要条件: 1.子bean必须与父bean保持兼容,也就是说子bean中必须有父bean定义的所有属性. 2.父bean必须是抽象bean或者定义lazy-init=true也就是不让bean工 ...
- Spring的DI(Ioc) - 注入集合类型
1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...
- Spring - bean的lazy-init属性(懒加载)
默认情况下,容器初始化的时候便会把bean实例化,通常这样做可以让一些配置或者bean实例化的异常在容器启动的时候就发现,而不是在N久之后.但有时候,我们希望某个可能不会用到但又不是100%不用的be ...
- spring bean标签常用属性
一.id属性 其名称,可以是任意名称,但不能包含特殊符号. 根据id得到配置对象. 二.class属性 创建对象所在的类名称 三.name属性 功能和id属性一样,但name属性值可以包含特殊属性 四 ...
- 【转载】Spring bean 中 constructor-arg属性
转载地址:https://blog.csdn.net/qq_27292113/article/details/78063696 方便以后查阅
- Spring学习3—控制反转(IOC)Spring依赖注入(DI)和控制反转(IOC)
一.思想理解 Spring 能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的Service对象,还是持久层的DAO对象,都可在Spring的 管理下有机地协调.运行.S ...
- spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现
知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...
随机推荐
- Hdoj 2454.Degree Sequence of Graph G 题解
Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...
- 【BZOJ5335】[TJOI2018]智力竞赛(二分图匹配)
[BZOJ5335][TJOI2018]智力竞赛(二分图匹配) 题面 BZOJ 洛谷 题解 假装图不是一个DAG想了半天,.发现并不会做. 于是假装图是一个DAG. 那么显然就是二分答案,然后求一个最 ...
- [luogu3810][bzoj3262]陌下花开【cdq分治】
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示.现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅Sa&g ...
- CSharp for Jupyter Notebook
之前说有机会就说下Linux下如何搭建C#版的交互编程,今天写篇文章还债^_^ Win下比较简单,可以自己看官方文档,下面逆天带大家搭建下Linux下的环境(官方方法有问题) 在线预览:https:/ ...
- jsp (1)
一.jsp介绍: jsp是servlet的一种包装.是html+js+css+servlet. jsp文件无需配置,如果修改了jsp文件不需要reload应用. jsp访问方法:直接访问文件名.jsp ...
- django orm跨表查询废话最少最精简版
在model.py中: class B1(models.Model): u1= models.CharField(max_length=32) #多 class B2(models.Model): f ...
- 【译】10. Java反射——数组
原文地址:http://tutorials.jenkov.com/java-reflection/arrays.html ======================================= ...
- java的抽象方法
抽象类所起的功能就像定义模板的功能,子类必须继承抽象类,因此不能用final修饰 http://blog.csdn.net/wei_zhi/article/details/52736350 抽象类的函 ...
- Codeforce 886 Технокубок 2018 - Отборочный Раунд 3 C. Petya and Catacombs(结论题)
A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really exper ...
- please select android sdk