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)框架的优势 提高开发效 ...
随机推荐
- proxy.conf编写
#这里的test.com要与proxy_pass http://test.com 一至!upstream test.com { ip_hash; server 172.16.0.20:80; serv ...
- 【redis】redis常用命令及操作记录
redis-cli是Redis命令行界面,可以向Redis发送命令,并直接从终端读取服务器发送的回复. 它有两种主要模式:一种交互模式,其中有一个REPL(read eval print loop), ...
- 2018.02.12 noip模拟赛T2
二兵的赌注 Description游戏中,二兵要进入了一家奇怪的赌场.赌场中有n个庄家,每个庄家都可以猜大猜小,猜一次一元钱.每一次开彩前,你都可以到任意个庄家那里下赌注.如果开彩结果是大,你就可以得 ...
- 网页HTML代码在线运行器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iis8使用url2.0模块实现http跳转到https
第一步安装,url 2.0重写模块 点击右键选择>获取新的web平台组件 找到url 重写工具2.0并安装 找到相应网站,选择 >url重写 设定名称后 匹配URL用于路径匹配 通用 (. ...
- 阿里面试:MYSQL的引擎区别
MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...
- redis主从复制配置(1)
我们来配置一个一主两从的服务,根据前面写的已经配置好的redis基础上进行主从配置 一:进入redis的配置目录 cd /usr/local/redis 创建下面3个目录,命令为:make -p /u ...
- Altium Designer 17 ------ 多层板设计
Pullback:在内电层边缘设置一个去铜边界,以保证内电层边界距离PCB边缘有一个安全间距.
- Python之包
包是一种通过使用'.模块名'来组织python模块名称空间的方式.创建包是为了用文件夹将文件/模块组织起来,创建包的目的不是为了运行,而是为了被导入使用.包的本质就是一个文件,其功能是将文件组织起来, ...
- lucene的CRUD操作Document(四)
IndexWriter writer = new IndexWriter(Directory, IndexWriterConfig); 增加文档:writer.addDocument(); 读取文档: ...