Spring实例"ID注入"——女生追男生
第一步:搭建开发环境
(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
注入给girl
的man
属性。
2、依赖注入中的value和ref的区别?
Value
注入的是简单类型(包括8个基本类型
和String
);
ref
注入的是复杂类型(除了简单类型意外的都是复杂类型)
3、IoC和DI有什么区别?
业界观点1:IoC
就是DI
,是同一个意思的两种表达。
业界观点2:IoC
与DI
不同,IoC
是创建对象,而DI
是注入值,
创建对象和注入值不是一个意思。
我的观点:支持观点2
下面的配置能更准确的说明IoC
和DI
的区别?
小结:
1:Spring和核心包括IoC和AOP
2:IoC和DI分别是创建对象和依赖注入。