博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Bean的ref属性和IoC注入集合
阅读量:5162 次
发布时间:2019-06-13

本文共 4426 字,大约阅读时间需要 14 分钟。

这是一个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
favorties = new ArrayList
();; private Map
jobTime = new HashMap
(); public List
getFavorties() { return favorties; } public void setFavorties(List
favorties) { this.favorties = favorties; } public Map
getJobTime() { return jobTime; } public void setJobTime(Map
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
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
maps = stu.getJobTime(); Set
keys = maps.keySet(); Iterator
iters = keys.iterator(); while(iters.hasNext()){ String key = iters.next(); System.out.println("key:"+key+",value:"+maps.get(key)); } } }

4、applicationContext.xml

吃饭
睡觉
看电影
AM
lol
PM
吃鸡

 

转载于:https://www.cnblogs.com/Vito-Yan/p/7759839.html

你可能感兴趣的文章
博客盈利请先考虑这七点
查看>>
使用 XMLBeans 进行编程
查看>>
写接口请求类型为get或post的时,参数定义的几种方式,如何用注解(原创)--雷锋...
查看>>
【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
查看>>
Java网络编程--socket服务器端与客户端讲解
查看>>
List_统计输入数值的各种值
查看>>
学习笔记-KMP算法
查看>>
Timer-triggered memory-to-memory DMA transfer demonstrator
查看>>
跨域问题整理
查看>>
[Linux]文件浏览
查看>>
64位主机64位oracle下装32位客户端ODAC(NFPACS版)
查看>>
获取国内随机IP的函数
查看>>
今天第一次写博客
查看>>
江城子·己亥年戊辰月丁丑日话凄凉
查看>>
IP V4 和 IP V6 初识
查看>>
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
C语言基础小结(一)
查看>>