Spring实例(DI注入)——女生追男生

第一步:搭建开发环境
(1)、创建maven项目;
(2)、导入jar包;
第二步:业务开发:
(1)、定义实体类;
(2)、添加并设置spring配置文件;
第三步:单元测试

需求

1:定义男生类,创建几个男生对象
2:定义女生类,创建一个女生对象
3:女生选择一个男生作为男朋友
4:后来女生换了一个男生作为男朋友

要求

1:不修改源代码的情况下更换男朋友
2:复习spring的IoC
3:引出spring的DI

第一步:搭建开发环境

1.1、创建maven项目

在这里插入图片描述

1.2、导入jar包

Maven仓库中获取以下两个jar

  • Spring
  • JUnit
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>luas</groupId>
  <artifactId>love</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.6.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
  
</project>

第二步:业务开发

2.1、定义实体类

Man.java

package cn.edu.model;

public class Man {
	private String name;
	private int height;
	private int weight;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}		
}

Girl.java

package cn.edu.model;

public class Girl {
	private String name;
	private Man man; //女生的男朋友
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Man getMan() {
		return man;
	}
	public void setMan(Man man) {
		this.man = man;
	}
}

2.2、添加并设置spring配置文件

创建spring配置文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


	<!-- 让spring创建对象 -->
	<!-- 
	<bean id="nan1" class="edu.luas.mediadigit.spring.Man">
	相当于
	Man nan1 = new Man();
	
	<property name="name" value="张杰"></property>
	相当于
	nan1.setName("张杰");
	 -->
	<bean id="man1" class="cn.edu.model.Man">
		<property name="name" value="张三"></property>
		<property name="height" value="177"></property>
		<property name="weight" value="100"></property>
	</bean>
	
	<bean id="man2" class="cn.edu.model.Man">
		<property name="name" value="李四"></property>
		<property name="height" value="165"></property>
		<property name="weight" value="160"></property>
	</bean>
	
	<bean id="man3" class="cn.edu.model.Man">
		<property name="name" value="王五"></property>
		<property name="height" value="170"></property>
		<property name="weight" value="140"></property>
	</bean>
	
	<bean id="gril" class="cn.edu.model.Girl">
		<property name="name" value="潘某某"></property>
		<property name="man" ref="man1"></property>
	</bean>
</beans>

第三步:单元测试

在这里插入图片描述
Tests.java

package cn.edu.test;

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

import cn.edu.model.Girl;

public class Tests {

	public static void main(String[] args) {
		//1:创建spring容器,将spring配置文件加载到spring容器中
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Girl girl = (Girl) ctx.getBean("girl");
		System.out.println(girl.getName()+"的男朋友是"+
						   girl.getMan().getName()+",他的身高是"+
				           girl.getMan().getHeight()+",体重是"+
						   girl.getMan().getWeight());
	}
	
	@Test
	public void test1() {
		//创建spring容器,将spring配置文件加载到spring容器中
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Girl girl1 = (Girl) ctx.getBean("girl");
		System.out.println(girl1.getName()+"的男朋友是"+
						   girl1.getMan().getName()+",他的身高是"+
						   girl1.getMan().getHeight()+",体重是"+
						   girl1.getMan().getWeight());
	}

}

第四步:结果展示

1、男朋友1(man1)
在这里插入图片描述
在这里插入图片描述
1、男朋友2(man2)
在这里插入图片描述
在这里插入图片描述

总结

1、什么是依赖注入(DI)

DI(依赖注入):全称为Dependency Injection

依赖注入中的依赖是什么意思?
孩子依赖父母成长。

依赖注入中的注入是什么意思?
注入就是给对方提供内容,输出内容的意思。

在上例中,女生需要男生作为男朋友,那么我们可以称之为将男生注入给女生。

java代码中实现注入的方法是

girl.setMan(man1);

表示将man1对象注入给girl对象。

spring配置文件中注入的方法是

<bean id="girl" class="cn.edu.model.Girl">
	<property name="name" value="潘xx"></property>
	<property name="man" ref="man2"></property>
</bean>

表示将man2注入给girlman属性。

2、依赖注入中的value和ref的区别?

Value注入的是简单类型(包括8个基本类型String);

ref注入的是复杂类型(除了简单类型意外的都是复杂类型)

3、IoC和DI有什么区别?

业界观点1:IoC就是DI,是同一个意思的两种表达。

业界观点2:IoCDI不同,IoC是创建对象,而DI是注入值,

创建对象和注入值不是一个意思。
我的观点:支持观点2
下面的配置能更准确的说明IoCDI的区别?
在这里插入图片描述

小结:
1:Spring和核心包括IoC和AOP
2:IoC和DI分别是创建对象和依赖注入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值