- 1. 基于构造器注入
- 2. 基于 setter 的注入
- 3. 基于命名空间的注入
- 3.1 p 命名空间的注入
- 3.2 c 命名空间的注入
依赖注入(Dependency Injection,简称 DI) :依赖指的是 bean 对象的创建依赖于容器;注入指的是 bean 对象中的所有属性,由容器进行注入。
依赖注入的实现方式,常见的有以下几种:
- 基于构造器(也叫构造函数、构造方法)的注入
- 基于 setter 的注入
- 基于命名空间的注入
- 基于注解的注入
(基于注解的注入,这里暂时不做展开演示,它的本质就是使用注解实现自动装配,即用注解实现 bean 属性的自动注入)
1. 基于构造器注入新建一个类——Student.java
package com.yuhuofei.pojo;
public class Student {
private String studentName;
private Integer studentNo;
public Student(String name, Integer number) {
this.studentName = name;
this.studentNo = number;
}
public void printStudent() {
System.out.println("学生的名字:" + studentName);
System.out.println("学生的学号:" + studentNo);
}
}
新建一个 xml 文件—— beans.xml
新建一个测试类——TestStudent.java
import com.yuhuofei.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudent {
public static void main(String[] args) {
//拿到Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//从容器中获取student对象
Student student = (Student) context.getBean("student");
student.printStudent();
}
}
运行测试类,结果如下
由以上的示例,可以看出,在 beans.xml 文件中,直接通过对构造器的操作,实现了值的注入。
在前面的基础上,我们改变一下各个文件的内容,演示基于 set 方法的注入
Student.java
package com.yuhuofei.pojo;
import java.util.Arrays;
public class Student {
private String studentName;
private Integer studentNo;
private String[] course;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentNo() {
return studentNo;
}
public void setStudentNo(Integer studentNo) {
this.studentNo = studentNo;
}
public String[] getCourse() {
return course;
}
public void setCourse(String[] course) {
this.course = course;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + ''' +
", studentNo=" + studentNo +
", course=" + Arrays.toString(course) +
'}';
}
}
beans.xml
大学物理 高等数学 概率论 通信原理 数据库概论 工程制图 计算机导论 操作系统 信息论 计算机网络 C语言程序设计 JAVA程序设计
TestStudent.java
import com.yuhuofei.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudent {
public static void main(String[] args) {
//拿到Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//从容器中获取student对象
Student student = (Student) context.getBean("student");
System.out.println("学生的名字是:" + student.getStudentName());
System.out.println("学生的信息是:" + student);
}
}
运行测试类的结果
由以上示例可以看出,基于 setter 的注入,其实就是利用类里面的 set 方法为类中的成员变量进行赋值,而赋值的操作是在 beans.xml 文件中指定的。
新建 Course.java
package com.yuhuofei.pojo;
public class Course {
private String courseName;
private Integer courseNo;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Integer getCourseNo() {
return courseNo;
}
public void setCourseNo(Integer courseNo) {
this.courseNo = courseNo;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + ''' +
", courseNo=" + courseNo +
'}';
}
}
新建 courseBeans.xml
注意,一定要在 xml 文件中,加入下面这一行,才能使用 p 命名空间注入
xmlns:p="http://www.springframework.org/schema/p"
新建 TestCourse.java
import com.yuhuofei.pojo.Course;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCourse {
public static void main(String[] args) {
//拿到spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("courseBeans.xml");
//从容器中获取对象
Course course = context.getBean("course", Course.class);
//输出对象
System.out.println(course);
}
}
运行测试类,就能打印输出 course 对象了。
3.2 c 命名空间的注入改动 Course.java 类的内容
package com.yuhuofei.pojo;
public class Course {
private String courseName;
private Integer courseNo;
public Course(String name,Integer num){
this.courseName=name;
this.courseNo=num;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + ''' +
", courseNo=" + courseNo +
'}';
}
}
改动 courseBeans.xml 的内容
注意,要加入下面这一行,才能使用 c 命名空间
xmlns:c="http://www.springframework.org/schema/c"
测试类则保持 3.1 中的不变,直接执行,即可得到新的结果。



