依赖注入的实现方式,常见的有以下几种:
(基于注解的注入,这里暂时不做展开演示,它的本质就是使用注解实现自动装配,即用注解实现 bean 属性的自动注入)
新建一个类——Student.java
package com.yuhuofei.pojo;
/**
* @Description
* @ClassName Student
* @Author yuhuofei
* @Date 2022/6/21 22:33
* @Version 1.0
*/
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
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--普通实体类的bean注册-->
<bean id="student" class="com.yuhuofei.pojo.Student">
<constructor-arg index="0" value="yuhuofei"/>
<constructor-arg index="1" value="2022061801"/>
</bean>
</beans>
新建一个测试类——TestStudent.java
import com.yuhuofei.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description
* @ClassName TestSpring
* @Author yuhuofei
* @Date 2022/6/20 21:08
* @Version 1.0
*/
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;
/**
* @Description
* @ClassName Student
* @Author yuhuofei
* @Date 2022/6/21 22:33
* @Version 1.0
*/
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
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--普通实体类的bean注册-->
<bean id="student" class="com.yuhuofei.pojo.Student">
<!--基于set方法注入值-->
<property name="studentName" value="yuhuofei"/>
<property name="studentNo" value="2022061801"/>
<property name="course">
<array>
<value>大学物理</value>
<value>高等数学</value>
<value>概率论</value>
<value>通信原理</value>
<value>数据库概论</value>
<value>工程制图</value>
<value>计算机导论</value>
<value>操作系统</value>
<value>信息论</value>
<value>计算机网络</value>
<value>C语言程序设计</value>
<value>JAVA程序设计</value>
</array>
</property>
</bean>
</beans>
TestStudent.java
import com.yuhuofei.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description
* @ClassName TestSpring
* @Author yuhuofei
* @Date 2022/6/20 21:08
* @Version 1.0
*/
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;
/**
* @Description
* @ClassName Course
* @Author yuhuofei
* @Date 2022/6/22 21:03
* @Version 1.0
*/
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 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间注入-->
<bean id="course" class="com.yuhuofei.pojo.Course" p:courseName="语文" p:courseNo="0001"/>
</beans>
注意,一定要在 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;
/**
* @Description
* @ClassName TestCourse
* @Author yuhuofei
* @Date 2022/6/22 21:05
* @Version 1.0
*/
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 对象了。
改动 Course.java 类的内容
package com.yuhuofei.pojo;
/**
* @Description
* @ClassName Course
* @Author yuhuofei
* @Date 2022/6/22 21:03
* @Version 1.0
*/
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 的内容
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- c命名空间注入-->
<bean id="course" class="com.yuhuofei.pojo.Course" c:name="数学" c:num="0002"/>
</beans>
注意,要加入下面这一行,才能使用 c 命名空间
xmlns:c="http://www.springframework.org/schema/c"
测试类则保持 3.1 中的不变,直接执行,即可得到新的结果。