Spring IoC注入三种方式

本文详细介绍了Spring IoC容器中的三种注入方式:setter & getter注入、构造方法注入和p命名空间注入。通过示例展示了在XML配置文件中如何使用这三种方式,并强调了构造方法注入时参数顺序的重要性以及p命名空间中不同类型属性的赋值规则。
摘要由CSDN通过智能技术生成

三种注入方式:

  1. setter & getter 注入
  2. 构造函数注入
  3. 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>

构造方法注入

  1. 直接写的方式,必须按照顺序
    <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>
    
  2. 不按照顺序,加上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

汇总

  1. 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 + "]";
    	}
    
    }
    
  2. 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 + "]";
	}
	
	
	
	
}

  1. 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>

  1. 测试类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);
	}
}

github代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值