三种注入方式:
- setter & getter 注入
- 构造函数注入
- p命名空间注入
有两个实体类
student:
public class Student {
String name;
int number;
int age;
public Student() {
}
public Student(String name, int number, int age) {
super();
this.name = name;
this.number = number;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", age=" + age + "]";
}
}
teacher:
public class Teacher {
String name;
Student student;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", student=" + student + "]";
}
}
setter & getter 注入
这个要求在实体类中有setXxxx()
比如
public void setName(String name) {
this.name = name;
}
在xml中的形式:
<bean id="student" class= "org.ljn.entity.Student">
<property name="name" value="ljn" />
<property name="age" value="20" />
<property name="number" value="28" />
</bean>
构造方法注入
- 直接写的方式,必须按照顺序
<bean id="student1" class= "org.ljn.entity.Student"> <!-- name,number,age --> <constructor-arg value = "jack"></constructor-arg> <constructor-arg value = "123"></constructor-arg> <constructor-arg value = "35"></constructor-arg> </bean>
- 不按照顺序,加上index(从0开始)
<bean id="student2" class= "org.ljn.entity.Student"> <constructor-arg index="1" value = "35"></constructor-arg> <constructor-arg index="2" value = "123"></constructor-arg> <constructor-arg index="0" value = "jack"></constructor-arg> </bean>
p命名空间
首先要在namespace里面加上p的那个
这么写👇
<bean id="teacher" class = "org.ljn.entity.Teacher"
p:student-ref="student1"
p:name="ls">
</bean>
两种写法:
- 简单类型、String:p:属性名=“属性值”
- 引用类型(除了String外):p:属性名-ref=“引用的id”
注意多个 p赋值的时候 要有空格。
比如上面的name是String类型就写 p:name=xxx
是引用类型:p:student-ref=xxxx
汇总
- Student
package org.ljn.entity; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Student { String name; int number; int age; public Student() { } public Student(String name, int number, int age) { super(); this.name = name; this.number = number; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", number=" + number + ", age=" + age + "]"; } }
- Teacher
package org.ljn.entity;
public class Teacher {
String name;
Student student;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", student=" + student + "]";
}
}
- 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"
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="student" class= "org.ljn.entity.Student">
<property name="name" value="ljn" />
<property name="age" value="20" />
<property name="number" value="28" />
</bean>
<bean id="student1" class= "org.ljn.entity.Student">
<!-- name,number,age -->
<constructor-arg value = "jack"></constructor-arg>
<constructor-arg value = "123"></constructor-arg>
<constructor-arg value = "35"></constructor-arg>
</bean>
<bean id="student2" class= "org.ljn.entity.Student">
<!-- name,number,age -->
<constructor-arg index="1" value = "35"></constructor-arg>
<constructor-arg index="2" value = "123"></constructor-arg>
<constructor-arg index="0" value = "jack"></constructor-arg>
</bean>
<bean id="teacher" class = "org.ljn.entity.Teacher" p:student-ref="student1" p:name="ls"></bean>
</beans>
- 测试类Test
package org.ljn.test;
import org.ljn.entity.Student;
import org.ljn.entity.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)context.getBean("student");
Student student1 = (Student)context.getBean("student1");
Student student2 = (Student)context.getBean("student2");
Teacher teacher = (Teacher)context.getBean("teacher");
System.out.println(student);
System.out.println(student1);
System.out.println(student2);
System.out.println(teacher);
}
}