Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is IoC of Spring

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is IoC of Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is Spring IoC"!

Spring Hello World Review

In the Hello Spring instance, we define the class

Public class HelloSpring {private String name= "hello"; HelloSpring () {System.out.println ("HelloSpring");} public String getName () {return this.name;} public void setName (String name) {this.name = name;}}

The configuration information in the configuration file is as follows:

The code for calling the getName method in HelloSpring is as follows:

Public static void main (String [] args) {ApplicationContextcontext = new ClassPathXmlApplicationContext ("beans1.xml"); HelloSpringhello = (HelloSpring) context.getBean ("hellospring"); System.out.println (hello.getName ());}}

The running results are as follows:

Output value in Hello Spring / / constructor value output in Hello / / getName method

For details, please refer to the article to test students' use of Spring from 0 to 1.

IOC interpretation

We can see that there is no new instance process for spring, but the instantiation operation is completed and the getName method is successfully called. This is the inversion of Control Technology (IoC) in Spring.

Spring promotes loose coupling through a technique called Inversion of Control-IoC. When IoC is applied, other objects that an object depends on are passed in passively, rather than the object itself creating or looking for dependent objects. In other words, Spring controls the generation of objects, and applications no longer need to actively create objects!

When we usually talk about SpringIoC, we actually mean the IoC container implementation (IoC Container) provided by the Spring framework.

What happens in Spring's IoC container is actually very simple, which can be summed up in two phases:

Pick and collect "coffee beans" (bean)

Grind and cook coffee

The first phase can be thought of as the stage of building and collecting bean definitions, in which we can define some bean in the form of XML or Java code, and then collect these bean definitions into the IoC container by manually assembling or having the container automatically scan based on some mechanism. We usually define the bean in the xml file, and then the container collects these bean into the IoC container. When the first phase of work is complete, we can temporarily assume that the IoC container is full of separate bean, and there is no relationship between them. But in fact, there are dependencies between them, so what the IoC container does in the second phase is to analyze the bean of these devices that are already in the IoC container, and then assemble them according to their dependencies. If IoC finds that a bean depends on another bean, it will inject that other bean into the bean that depends on it until all the bean dependencies are injected and all the bean is "ready to go", and the work of the entire IoC container is complete.

The basic running process of IOC is shown in the following figure:

When Spring starts, it reads the Bean configuration information provided by the application, and generates a corresponding Bean configuration registry in the Spring container, then instantiates Bean according to this registry, assembles the dependency relationship between Bean, and provides a ready running environment for upper applications.

IOC has a deep understanding of IoC containers

The container that implements the idea of IoC is the IoC container. The functions of IoC container include instantiating, initializing components, assembling component dependencies, and being responsible for component life cycle management.

Characteristics of IoC container

There is no need for an active new object; it describes how the object should be created. The IoC container is created for you, that is, passive instantiation

You don't need to actively assemble the dependencies between objects, but describe which services (components) you need. The IoC container will assemble them for you (that is, responsible for associating them together) and passively accept the assembly.

IoC is a way of component design that makes service consumers not directly dependent on service providers, and is a design principle to reduce class-to-class dependencies.

The key to understanding the IoC container problem: what aspects of control have been reversed?

1. Who controls whom? Why is it called reversal? IoC container control, which was previously controlled by the application, so it is called inversion

2. Control what? Control the resources (objects, files, etc.) required by the application

3. Why control? Decouple the relationship between components

4. What aspects of control have been reversed? Control of the program is reversed: it is transferred from the application to the IoC container.

Ideas that need to be changed to develop using IoC containers

1. The application does not actively create objects, but describes how to create them.

2. The service is not assembled directly in the application code, but which component needs which service is described in the configuration file. The container is responsible for assembling these together.

The most popular explanation

All classes will register in the spring container to tell spring what you are and what you need, and then spring will give you what you want when the system is running properly, and give you what you need. The creation and destruction of all classes are controlled by spring, that is, the life cycle of an object is no longer controlled by the object that references it, but by spring. For a specific object, it used to control other objects, but now all objects are controlled by spring, so this is called control inversion.

At this point, I believe you have a deeper understanding of "what is IoC of Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report