Spring——IOC(依赖注入)的三种方式

依赖注入的三种方式(注入的底层是通过反射实现的)

第一种:set方式的依赖注入

 

applicationContext.xml

id:bean的唯一标识符

class:是实体的包类名

property:name是实体类的属性名;value指的是属性值

<bean id="teacher" class="com.dt.entity.Teacher">
    <property name="name" value="zs"></property>
    <property name="age" value="23"></property> 
</bean>

<bean id="course" class="com.dt.entity.Course">
    <property name="courseName" value="java"></property>
    <property name="courseHour" value="100"></property>
    <property name="teacher" ref="teacher"></property>
</bean>

Teacher.java

package com.dt.entity;

public class Teacher {
	private String name;
	private int age;
	
	
	public Teacher() {
		
	}
	
	
	public Teacher(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	

}

Course.java

 

package com.dt.entity;

public class Course {
	
	private String courseName;
	private int courseHour;
	private Teacher teacher;//授课老师;依赖于Teacher类
	
	public Course() {
		
	}
	
	
	public Course(String courseName, int courseHour, Teacher teacher) {
		super();
		this.courseName = courseName;
		this.courseHour = courseHour;
		this.teacher = teacher;
	}
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public int getCourseHour() {
		return courseHour;
	}
	public void setCourseHour(int courseHour) {
		this.courseHour = courseHour;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	//打印课程信息
	public void showInfo(){
		System.out.println(this.courseName+"," + this.courseHour+","+this.teacher.getName());
	}
	
	

}

Tset.java

package com.dt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dt.entity.Course;
import com.dt.entity.Student;

public class test {

	public static void main(String[] args) {
		
		testDI();

	}
	
	public static void testDI(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Course course= (Course)context.getBean("course");
		course.showInfo();
	}

}

第二种,构造器(构造方法注入)

通过构造方法赋值:
             constructor-arg和构造方法的参数顺序一致,若是不一致,可以通过name或index(索引)或type(类型)指定参数

<bean id="teacher" class="com.dt.entity.Teacher" >
    <constructor-arg value="李四"></constructor-arg>
    <constructor-arg value="24"></constructor-arg>
</bean>

<bean id="course" class="com.dt.entity.Course">
    <constructor-arg value="c"></constructor-arg>
    <constructor-arg value="100"></constructor-arg>
    <constructor-arg ref="teacher"></constructor-arg>
</bean>

其他测试类和实体类不变

第三种,p命令空间赋值

        1、引入p命名空间:xmlns:p="http://www.springframework.org/schema/p"

        2、简单类型为p:属性名="属性值"

             引用类型:p:属性名-ref="引用的id"

<bean id="teacher" class="com.dt.entity.Teacher" p:age="25" p:name="王五">
<bean id="course" class="com.dt.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">

其他类不变

 

个人学习心得,希望能帮助到你们,一起学习吧=.=!!!也可以去b站看颜群老师的视频讲解,挺好的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值