Code demonstration using Spring framework

Requirement: girls chase boys
Analysis:
define boys classes
Define girls classes
Use Spring to create boys and girls objects
Realize girls chasing boys in the test class
This example leads to DI (dependency injection)
Step 1: Create maven project
Step 2: Import jar packageInsert picture description here

Step 3: Create boys and girls classes

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;
	}

}

Step 4: Install the spring plug-in.
Take the spring installation tutorial and its spring plug-in package online

Step 5: Create Spring configuration file

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

Step 5: Create objects for boys and girls in the configuration file

<?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>

Step 6: Create a test class

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());
	}

}


Important concepts:
*****Dependency injection
What is DI?
DI (Dependency Injection) translates to dependency injection
DI is the key knowledge of spring's core Ioc
Explain the meaning of dependency:
the meaning of injection:
when an object needs another object, how does this object find another object from multiple objects? ? How to find out how to deliver the found object to the one needed to answer? : You should go to spring to find it, and if you find it, give an object in the spring container to the one you need. This is dependency injection. The object that is about to be found is injected into the object that is needed.
E.g

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

Inject the m1 object into the man attribute of the wo1 object. (Here we need to create a man object in Moman)
The difference between spring's IoC and DI
Viewpoints of practitioners in the industry 1: IoC is DI, two terms that are considered the same.
Viewpoint of practitioners in the industry 2: These two do not mean the same thing. IoC is to create objects, and DI is to inject the created objects into objects that need to be changed. That is to say, IoC is a new object, and DI is a use object.

Guess you like

Origin blog.csdn.net/qq_44143902/article/details/109128467