Hobby.java

package com.wh.bean;

public class Hobby {
private Integer id;
private String name; public Hobby() {
// TODO Auto-generated constructor stub
} public Hobby(Integer id, String name) {
super();
this.id = id;
this.name = name;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Hobby [id=" + id + ", name=" + name + "]";
} }

Student.java

package com.wh.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Set; public class Student {
private Integer id;
private String name;
private String[] addressArray;
private ArrayList<Hobby> hobbylist;
private Set<Hobby> hobbysSet;
private Map<String, String> map; public Student() {
System.out.println("Student被创建了!!!");
} public Student(Integer id, String name, String[] addressArray, ArrayList<Hobby> hobbylist, Set<Hobby> hobbysSet,
Map<String, String> map) {
super();
this.id = id;
this.name = name;
this.addressArray = addressArray;
this.hobbylist = hobbylist;
this.hobbysSet = hobbysSet;
this.map = map;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String[] getAddressArray() {
return addressArray;
} public void setAddressArray(String[] addressArray) {
this.addressArray = addressArray;
} public ArrayList<Hobby> getHobbylist() {
return hobbylist;
} public void setHobbylist(ArrayList<Hobby> hobbylist) {
this.hobbylist = hobbylist;
} public Set<Hobby> getHobbysSet() {
return hobbysSet;
} public void setHobbysSet(Set<Hobby> hobbysSet) {
this.hobbysSet = hobbysSet;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", addressArray=" + Arrays.toString(addressArray) + ", hobbylist=" + hobbylist
+ ", hobbysSet=" + hobbysSet + ", map=" + map + "]";
} }

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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean name="hobby01" class="com.wh.bean.Hobby">
<property name="id" value="1"/>
<property name="name" value="跑步"/>
</bean>
<bean name="hobby02" class="com.wh.bean.Hobby">
<property name="id" value="2"/>
<property name="name" value="游泳"/>
</bean> <bean id="student" class="com.wh.bean.Student">
<property name="id" value="001"/>
<property name="name" value="zhangsan"/>
<!-- 数组 Property下的子标签既可以是list,也可以是array -->
<property name="addressArray">
<list>
<value>广贤路</value>
<value>三丰大夏</value>
</list>
</property>
<!-- list集合 -->
<property name="hobbylist">
<list>
<ref bean="hobby01"/>
<ref bean="hobby01"/>
</list>
</property>
<!-- set集合 -->
<property name="hobbysSet">
<set>
<ref bean="hobby02"/>
<ref bean="hobby02"/>
</set>
</property>
<!-- map集合 -->
<property name="map">
<map>
<!-- <entry key="" key-ref="" value="" value-ref=""></entry> -->
<entry key="mapKey01" value="mapValue01"/>
<entry key="mapKey02" value="mapValue02"/>
<entry key="mapKey03" value="mapValue03"/>
</map>
</property>
</bean> </beans>

TestMVC.java

package com.wh.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wh.bean.Student; public class TestMVC { @Test
public void testStudent(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu =(Student)ac.getBean("student");
System.out.println(stu);
}
}

  

  

  

Spring IOC set注入的更多相关文章

  1. 一篇关于spring ioc 依赖注入3种方式的文章引用

    今天看到一篇spring ioc 容器依赖注入3种方式的文章,为了方便后面的复习,在此引用别人的文章,查看请戳我.

  2. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  3. java框架篇---spring IOC依赖注入

    spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class ...

  4. Spring IoC 依赖注入的方法大全 XML配置方式

    Spring 依赖注入 构造方法注入 ①   根据索引注入 <bean name="student" class="cn.bdqn.SpringDI.Student ...

  5. [Spring] IOC - study

    Spring IOC简单注入例子,本例子使用JUnit进行测试.Spring版本:3.2 项目结构: Spring所需引用的JAR包: Spring XML配置: springContext.xml ...

  6. 小马哥讲Spring栈核心编程思想 Spring IoC+Bean+Framework

    小马哥出手的Spring栈核心编程思想课程,可以说是非常专业和权威的Spring课程.课程主要的方向与核心是Spring Framework总览,带领同学们重新认识重新认识IoC,Spring IoC ...

  7. spring ioc三种注入方式

    spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...

  8. Hello Spring Framework——依赖注入(DI)与控制翻转(IoC)

    又到年关了,还有几天就是春节.趁最后还有些时间,复习一下Spring的官方文档. 写在前面的话: Spring是我首次开始尝试通过官方文档来学习的框架(以前学习Struts和Hibernate都大多是 ...

  9. Spring IOC 注入方式

    依赖注入通常有如下两种方式: ①设值注入:IOC容器使用属性的Setter方法来注入被依赖的实例. 设值注入是指IOC容器使用属性的Setter方法来注入被依赖的实例.这种注入方式简单.直观,因而在S ...

随机推荐

  1. 第十节:Web爬虫之数据存储与MySQL8.0数据库安装和数据插入

    用解析器解析出数据之后,接下来就是存储数据了,保存的形式可以多种多样,最简单的形式是直接保存为文本文件,如 TXT.JSON.csv 另外,还可以保存到数据库中,如关系型数据库MySQL ,非关系型数 ...

  2. 09.C语言:预处理(宏定义)、字节序、地址对齐

    一.预处理 预处理 gcc -E Hello.c -o hello.i 编译 gcc -S hello.i -o hello.s 汇编 gcc -c hello.s -o hello.o 链接 gcc ...

  3. C语言之自定义__DATE__与__TIME__

    /******************************************************************* * > File Name: 05-ymd.c * &g ...

  4. [spoj1182][Sorted Bit Sequence] (数位dp)

    Description Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ ...

  5. Prüfer序列和cayley定理

    参考资料: 1.matrix67 <经典证明:Prüfer编码与Cayley公式> 2.百度百科 3.Forget_forever prufer序列总结 4.维基百科 5.dirge的学习 ...

  6. #if 0的意义和好处

    在调试中经常遇到,写好的程序,需要调试相反的两方面,如:有两款单片机的程序,分别对应着不同的硬件引脚,我们把代码都写上了,但是不能同时让他们起效,通常的办法是/**/屏蔽一些段落,但是调试起来很麻烦. ...

  7. 【04】JSONP 教程

    JSONP 教程 Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的域 ...

  8. JavaScript保留关键字(全)

    JavaScript 标准 所有的现代浏览器已经完全支持 ES5(ECMAScript 5). JavaScript 保留关键字(keyword) Javascript 的保留关键字(标识符)不可以用 ...

  9. lnmp的安装--nginx

    1.nginx的安装 安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境. 一. gcc 安装安 ...

  10. noip模拟赛 传球接力

    [问题描述]n 个小朋友在玩传球. 小朋友们用 1 到 n 的正整数编号. 每个小朋友有一个固定的传球对象,第 i 个小朋友在接到球后会将球传给第 ai个小朋友, 并且第 i 个小朋友与第 ai个小朋 ...