Home > Java > javaTutorial > Detailed explanation of Spring IOC and DI

Detailed explanation of Spring IOC and DI

Guanhui
Release: 2020-06-24 13:46:14
forward
3446 people have browsed it

Detailed explanation of Spring IOC and DI

What is Spring

Spring is a framework with IoC and AOP as its core.

IoC (Inversion of Control, Inversion of Control) is the foundation of Spring.

IoC simply means that when creating an object, the previous programmer called the new constructor method instead of leaving it to Spring to create the object.

DI (Dependency Inject, dependency injection) has the same meaning as IoC, except that these two names describe the same concept from two perspectives.

Simply put, DI is the attribute of the object. The relevant values ​​have been injected and can be used directly.

Detailed explanation of Spring IOC and DI

IoC-Inversion of Control

After encapsulating each object class, associate these object classes through the IoC container. In this way, objects are connected through the IoC container, and there is no direct connection between objects.
Detailed explanation of Spring IOC and DI

 Before the application introduces the IoC container, object A depends on object B. Then when object A is instantiated or runs to a certain point, it must actively create object B or use it. Created object B, whether creating or using the created object B, the control lies with the application itself.

If the application introduces the Ioc container, the direct connection between object A and object B is lost. Therefore, when object A is instantiated and run, if object B is needed, the IoC container will Actively create an object B and inject it (i.e. dependency injection) into the place where object A needs it. As a result, the process of object A becoming dependent on object B changes from active behavior to passive behavior, that is, the creation of the object is handed over to the IoC container for processing, and the control rights are reversed. This is the so-called inversion of control.

DI-Dependency Injection

The IoC container dynamically injects certain dependencies into objects during runtime. For example, inject (assign) object B to a member variable of object A.

 In fact, Dependency Injection (DI) and Inversion of Control (IoC) are different descriptions of the same thing. In a certain aspect, they describe it from different perspectives. Dependency injection is described from the perspective of the application, that is, the application relies on the container to create and inject the external resources it needs; while inversion of control is described from the perspective of the container, that is, the container controls the application, and the container reverses the direction of the application. The application injects external resources required by the application. The external resources mentioned here can be external instance objects, external file objects, etc.

IoC and DI implementation

 (1) Property setter method injection: refers to the IoC container using the setter method to inject dependent instances. After instantiating a Bean by calling the parameterless constructor or parameterless static factory method, and calling the setter method of the Bean, dependency injection based on the setter method can be implemented. This method is simple, intuitive, and easy to understand, so Spring's settings injection is widely used.

package com.ssm.entry;

public class UserServiceImpl implements UserService {

	private UserDao userDao;
	
	// setter()实现依赖注入
	public void setUserDao(UserDao userDao){
		this.userDao = userDao;
	}
	
	public void login() {
		this.userDao.login();
		System.out.println("UserService login");
	}

}
Copy after login

 (2) Constructor injection: refers to the IoC container using the constructor method to inject dependent instances. Dependency injection based on constructor is implemented by calling the constructor with parameters, each parameter represents a dependency.

applicationContext.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"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    		
    <!-- 将指定类配置给Spring,让Spring创建其对象的实例 控制反转 -->
		<bean id="UserDao" class="com.ssm.entry.UserDaoImpl"></bean>
		
		<!-- 添加一个id为userService的Bean 依赖注入-->
		<bean id="UserService" class="com.ssm.entry.UserServiceImpl">
			<!-- 将name为UserDao的Bean注入UserService实例中 -->
			<property name="UserDao" ref="UserDao"/>
		</bean>
</beans>
Copy after login

IoC test

package com.ssm.entry;

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

public class IoC {
	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userDao = (UserDao)applicationContext.getBean("UserDao");
		userDao.login();
		
	}
}
Copy after login

DI test

package com.ssm.entry;

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

public class DI {
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new 
				ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService)applicationContext.getBean("UserService");
		userService.login();
	}
	
}
Copy after login

Recommended tutorial: "Java Tutorial"

The above is the detailed content of Detailed explanation of Spring IOC and DI. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template