005_Spring的属性注入

1. DI依赖注入, 前提必须有IOC的环境, Spring管理这个类的时候将类的依赖的属性注入(设置)进来。

2. Spring的属性注入

2.1. 构造方法的方式的属性注入

2.2. Set方法的方式的属性注入

2.3. 集合类型属性注入

3. 依赖注入实例

3.1. 新建一个名为SpringDI的Java工程, 拷入Spring相关包

3.2. 创建Student.java

package com.lywgames.bean;

public class Student {
	private int id;
	private String name;
	
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
}

3.3. 创建Teacher.java

package com.lywgames.bean;

public class Teacher {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
	
}

3.4. 创建Clazz.java

package com.lywgames.bean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Clazz {
	private Teacher teacher;
	
	private String[] clazzName;
	private List<Student> studentList;
	private Set<Student> studentSet;
	private Map<Integer, Student> studentMap;

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public String[] getClazzName() {
		return clazzName;
	}

	public void setClazzName(String[] clazzName) {
		this.clazzName = clazzName;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

	public Set<Student> getStudentSet() {
		return studentSet;
	}

	public void setStudentSet(Set<Student> studentSet) {
		this.studentSet = studentSet;
	}

	public Map<Integer, Student> getStudentMap() {
		return studentMap;
	}

	public void setStudentMap(Map<Integer, Student> studentMap) {
		this.studentMap = studentMap;
	}

	@Override
	public String toString() {
		return "Clazz [teacher=" + teacher + ", clazzName=" + Arrays.toString(clazzName) + ", studentList=" + studentList + ", studentSet=" + studentSet
				+ ", studentMap=" + studentMap + "]";
	}

}

3.5. 创建Test.java

package com.lywgames;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lywgames.bean.Clazz;
import com.lywgames.bean.Student;
import com.lywgames.bean.Teacher;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println(context.getBean(Student.class).toString());
		System.out.println(context.getBean(Teacher.class).toString());
		System.out.println(context.getBean(Clazz.class).toString());
		context.close();
	}
}

3.6. 在src目录下创建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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 构造函数注入 -->
	<bean id="student" class="com.lywgames.bean.Student"> 
		<constructor-arg name="id" value="101"></constructor-arg>
		<constructor-arg name="name" value="zhangsan"></constructor-arg>
	</bean> 
	
	<!-- set方法注入 -->
	<bean id="teacher" class="com.lywgames.bean.Teacher"> 
		<property name="id" value="901"></property>
		<property name="name" value="xiaocui"></property>
	</bean> 
	
	<!-- 注入数组, list, set, map -->
	<bean id="clazz" class="com.lywgames.bean.Clazz">  
		<property name="teacher" ref="teacher"></property>
		<!-- 注入数组-->
		<property name="clazzName">
			<list>
				<value>计算机1班</value>
				<value>三好班级</value>
			</list>
		</property>
		<!-- 注入 list集合 -->
		<property name="studentList">
			<list>
				<ref bean="student"/>
				<ref bean="student"/>
			</list>
		</property>
		<!-- 注入set集合 -->
		<property name="studentSet">
			<set>
				<ref bean="student"/>
				<ref bean="student"/>
			</set>
		</property>
		<!-- 注入map集合 -->
		<property name="studentMap">
			<map>
				<entry key="801" value-ref="student"></entry>
				<entry key="802" value-ref="student"></entry>
			</map>
		</property>
	</bean>
</beans>

3.7. 运行项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值