构造方法和Set方法注入Bean
作者:互联网
spring注入bean:使用构造方法或者Set方法
这里用一个实例来解释构造方法与Set方法的区别
要求:
1、创建一个教师类,具有姓名、性别、年龄、职称这四个属性;
2、创建一个课程类,具有课程编号、课程名、课程类型、授课教师这四个属性;
3、分别使用属性注入和构造器注入的方式创建这两个类的Bean,并且要给每个属性都进行赋值;
4、课程类中的“授课教师”属性的值必须是具体的教师对象;
5、编写测试方法,在控制台输出教师类和课程类中每个属性的值。
- 导入jar包,在maven工程中导入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
即可获得必须的依赖
- 创建实体类,并编写set、get和tostring方法
注意:因为我们要分别实现构造方法与Set方法注入bean
所以我们选择Course为Set方法注入(set方法注入必须有无参构造)
Teacher为构造方法注入(构造方法注入必须有有参构造)
public class Course {
private int courseId;
private String courseName;
private String courseType;
private Teacher teacher;
public Course(int courseId, String courseName, String courseType, Teacher teacher) {
this.courseId = courseId;
this.courseName = courseName;
this.courseType = courseType;
this.teacher = teacher;
}
@Override
public String toString() {
return "Course{" +
"courseId=" + courseId +
", courseName='" + courseName + '\'' +
", courseType='" + courseType + '\'' +
", teacher=" + teacher +
'}';
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseType() {
return courseType;
}
public void setCourseType(String courseType) {
this.courseType = courseType;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class Teacher {
private String name;
private String sex;
private int age;
private String grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "teacher{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", grade='" + grade + '\'' +
'}';
}
}
- 在resource文件下建立xml文件
使用Set方法注入Teachar
<bean id="teacher" class="com.shan.bean.Teacher">
<property name="name" value="张三"></property>
<property name="sex" value="男"></property>
<property name="age" value="33"></property>
<property name="grade" value="高级"></property>
</bean>
使用构造器方法注入Course
<bean id="course" class="com.shan.bean.Course">
<constructor-arg index="0" value="110"></constructor-arg>
<constructor-arg index="1" value="爆破学"></constructor-arg>
<constructor-arg index="2" value="化学"></constructor-arg>
<constructor-arg index="3" ref="teacher"></constructor-arg>
</bean>
构造器方法注入有多种形式,目前使用的是使用下标赋值,index的数值为有参构造参数的顺序。
其他构造器注入方法:通过类型创建(不推荐使用)、直接使用参数名设置
- 通过测试类测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Teacher teacher = context.getBean("teacher", Teacher.class);
System.out.println(teacher.toString());
Course course = context.getBean("course", Course.class);
System.out.println(course.toString());
}
}
- 测试结果
标签:Set,return,String,构造方法,courseName,public,Bean,teacher,courseType 来源: https://blog.csdn.net/weixin_45750619/article/details/117986538