代码演示使用Spring框架

需求:女生追男生
分析:
定义男生类
定义女生类
使用Spring创建出男生和女生对象
在测试类中实现女生追男生
本例引出DI(依赖注入)
步骤1:创建maven项目
步骤2:导入jar包在这里插入图片描述

步骤3:创建男生女生类

package edu.luas.mediadigit.ioc;

public class Man {
    
    

	private String name;

	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
	}
	

}

package edu.luas.mediadigit.ioc;

public class Woman {
    
    
	
	private Man man;
	public Man getMan() {
    
    
		return man;
	}

	public void setMan(Man man) {
    
    
		this.man = man;
	}

	private String name;

	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
	}

}

步骤4:安装spring插件
网上取spring安装教程,及其spring插件包

步骤5:创建Spring配置文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>luas</groupId>
	<artifactId>mediadigit</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
	//spring安装配置
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.9.RELEASE</version>
		</dependency>
		//junit配置
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>



	</dependencies>
</project>

步骤5:在配置文件中创建男生和女生的对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--创建一批男生对象-->
<!--spring new的对象相当于Man m1 =new Man();  -->
  <bean id="m1" class="edu.luas.mediadigit.ioc.Man">
  <!-- property相当于m1.setname(); -->
  	<property name="name" value="宋江">
  	</property>
  
  <bean id="m2" class="edu.luas.mediadigit.ioc.Man">
  <property name="name" value="张文远"></property>
  </bean>
  
    <bean id="m3" class="edu.luas.mediadigit.ioc.Man">
  <property name="name" value="李冲"></property>
  </bean>
  
  
  </bean>
  <bean id="wo1" class="edu.luas.mediadigit.ioc.Woman">
  <property name="name" value="阎惜娇"> </property>
  <property name="man" ref="m1"></property>
  </bean>

</beans>

步骤6:创建测试类

package edu.luas.mediadigit.text;

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

import edu.luas.mediadigit.ioc.Man;
import edu.luas.mediadigit.ioc.Woman;

public class Testioc {
    
    
	@Test
	public void testioc() {
    
    
		ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		
		// Man m1=(Man)ctx.getBean("m1");//ctx包含了四个对象
//		 System.out.println("m1是"+m1.getName());
		 

		Woman wo1 = (Woman) ctx.getBean("wo1");
		System.out.println(wo1.getName() + "找的人是" + wo1.getMan().getName());
	}

}


重要概念:
*****依赖注入
什么是DI?
DI(Dependency Injection)翻译为依赖注入
DI是spring的核心Ioc的重点知识
解释依赖的含义:
注入的含义:
当某个对象需要另一个对象时,这个对象如何从多个对象中找到另一个对象呢?找到如何将这个找到 对象交给答需要的那个对象呢?:应该去spring中去找,如果找到则将spring容器中的某个对象交给需要的那个对象,这个就是依赖注入。即将找到的对象注入给需要的对象。
例如

<bean id="wo1" class="edu.luas.mediadigit.ioc.Woman">
		<property name="name" value="阎惜娇"></property>
		<property name="man" ref="m2"></property>
</bean>

就实现了将m1对象注入给wo1对象的man属性。(这里要在Moman中创建man对象呦)
spring的IoC和DI的区别
行业中从业人员的观点1:IoC就是DI,认为是一个意思的两种说法。
行业中从业人员的观点2:这两不是一个意思,认为IoC是创建对象,DI是将创建的对象注入给需要改对象的对象,也就是说IoC是new对象,DI是使用对象。

猜你喜欢

转载自blog.csdn.net/qq_44143902/article/details/109128467