Spring的三种依赖注入方式

  ↵三种依赖注入方式

  1. 通过set方法注入

  2. 通过构造方法注入

  3. 通过标签注入

准备工作

jar包  将以下这些包导入到项目中(包的作用及环境搭建参考Spring的简单环境配置

commons-logging-1.2.jar
spring-aop-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar

1.配置applicationContext.xml

三种依赖注入方式,将注释部分切换即可

使用p标签方式依赖注入时需要在<beans>头文件中加入:xmlns:p="http://www.springframework.org/schema/p"

<?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.xsd">

	<!-- 通过bean来给类中的属性赋值 id:唯一标识符 class:实体类的位置 包名+类名 property:该class类代表的属性值 
		name:属性名 value:值 当该属性值时普通类型时用value,对象类型用ref引用id -->
	<bean id="studentAddress" class="com.zy.pojo.StudentAddress">
		<property name="homeAddress" value="广州"></property>
		<property name="schoolAddress" value="广州"></property>
	</bean>
	
	<!-- 注入依赖有三种方式
		 第一种:property是通过set方法赋值
		<property name="stuNo" value="8"></property> == student.setStuNo("8");
	<bean id="student" class="com.zy.pojo.Student">
		<property name="stuNo" value="8"></property>
		<property name="stuName" value="uu"></property>
		<property name="stuClass" value="2"></property>
		<property name="stuAddress" ref="studentAddress"></property>
	</bean>
	-->

	<!-- 第二种注入依赖的方式
		 constructor-arg是通过构造方法赋值,相当调用类中的构造方法
		   当该属性值时普通类型时用value,对象类型用ref引用id
		   赋值的顺序为构造方法的顺序,当赋值顺序和构造方法中顺序不同时,可以使用name(属性名)、index(下标位置)、type(类型)来区分,建议用name来区分
		 注意:name:区分大小写  index:下标从0开始  type:int和Integer不同
	<bean id="student" class="com.zy.pojo.Student">
		<constructor-arg value="5" name="stuNo" index="0" type="Integer"></constructor-arg>
		<constructor-arg value="kk" name="stuName"></constructor-arg>
		<constructor-arg value="3" name="stuClass"></constructor-arg>
		<constructor-arg ref="studentAddress" name="stuAddress"></constructor-arg>
	</bean>
	-->

	<!-- 第三种注入依赖的方式 
		 使用p标签	需要加入:xmlns:p="http://www.springframework.org/schema/p"
		 普通类型用p:属性名="值"   对象类型用属性名-red="引用的bean的id"
	-->
	<bean id="student" class="com.zy.pojo.Student" p:stuNo="4" p:stuName="ww" p:stuClass="2" p:stuAddress-ref="studentAddress">
	</bean>
	
</beans>


2.创建Student.java类

因为StudentAddress是一个对象属性,还需要创建一个StudentAddress.java类

package com.zy.pojo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zy.interfaces.Course;

public class Student {
	private Integer stuNo;
	private String stuName;
	private String stuClass;
	private StudentAddress stuAddress;
	
	public Student(Integer stuNo, String stuName, String stuClass) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuClass = stuClass;
	}

	public Student() {
		super();
	}
	
	public Student(Integer stuNo, String stuName, String stuClass, StudentAddress stuAddress) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuClass = stuClass;
		this.stuAddress = stuAddress;
	}
	
	public StudentAddress getStuAddress() {
		return stuAddress;
	}

	public void setStuAddress(StudentAddress stuAddress) {
		this.stuAddress = stuAddress;
	}

	public Integer getStuNo() {
		return stuNo;
	}

	public void setStuNo(Integer stuNo) {
		this.stuNo = stuNo;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getStuClass() {
		return stuClass;
	}

	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}

	@Override
	public String toString() {
		return "stuNo=" + stuNo + ", stuName=" + stuName + ", stuClass=" + stuClass+","
				+ stuAddress + "]";
	}
	
}

StudentAddress.java类

package com.zy.pojo;

public class StudentAddress {

	private String homeAddress;
	private String schoolAddress;
	public String getHomeAddress() {
		return homeAddress;
	}
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}
	public String getSchoolAddress() {
		return schoolAddress;
	}
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}
	
	public StudentAddress(String homeAddress, String schoolAddress) {
		super();
		this.homeAddress = homeAddress;
		this.schoolAddress = schoolAddress;
	}
	public StudentAddress() {
		super();
	}
	
	@Override
	public String toString() {
		return "StudentAddress [homeAddress=" + homeAddress + ", schoolAddress=" + schoolAddress + "]";
	}
	
}

 

3.创建StudentTest.java测试类

package com.zy.test;

import java.applet.AppletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zy.pojo.Student;
import com.zy.pojo.StudentAddress;
import com.zy.pojo.TypeOfSetValue;

public class StudentTest {

	public static void main(String[] args) {
        	showStudent();
	}
	

	private static void showStudent() {
		/**
		 * 使用Spring IOC容器外部注入,控制反转(依赖注入)
		 */
//		ApplicationContext:获取Spring上下文
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		通过id来找到bean
		Student student = (Student) ac.getBean("student");
		System.out.println(student);
	}

}

4.项目结构

红色方框为所用到的类,其他不用管

Spring的三种依赖注入方式

5.运行结果

Spring的三种依赖注入方式