In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to understand Spring IoC". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to understand Spring IoC" together!
First IoC
Several of the most important projects in the Spring family bucket are based on the Spring Framework, so let's look at the documentation using the Spring Framework as an example.
First of all, there is a link to Github on the right side of it, and click on "LEARN" here to see the various versions of the document.
Then we click on "Reference Doc" and we can see some of its modules:
(Wait...) Module? What is a module? This question is answered below.)
Chapter 1 Overview, describing its history, design principles and so on;
The second chapter Core, contains IoC containers, AOP, etc., that is naturally the core of Spring, the main points to go in and have a good look.
After clicking on it, I found valuable learning materials. Everything about what, why and how can be found here.
IoC - Inversion of Control-IoC - Inversion of Control
Every time you read it, you will have new experiences and gains.
I'll summarize it roughly: inversion of control is transferring the process of creating and managing beans to a third party. This third party is Spring IoC Container, and for IoC, the most important thing is the container.
The container is responsible for creating, configuring, and managing beans, i.e., it manages the life of the bean and controls the dependency injection of the bean.
In layman's terms, because it is troublesome to create objects every time in the project, we use Spring IoC containers to manage these objects. When you need them, you can use them directly, regardless of how they come or when they are destroyed.
For example, if parents don't have time to care for their children, they leave them in foster care and go to work without worrying about their children. Nursery, is a third-party container, responsible for managing children's eating, drinking and playing; parents, equivalent to programmers, just pick up children, do not care about them eating and drinking.
Wait, what's a bean?
Bean is actually wrapped Object, whether it is control inversion or dependency injection, their subject is object, and bean is wrapped object by a third party. (Think about it when someone gives you a gift, you have to wrap it up, and you can avoid making it yourself.)
IoC container
Since containers are the most important part of IoC, how does Spring design containers? Or go back to the official website, the second paragraph has an introduction:
A: Use ApplicationContext, which is a subclass of BeanFactory and better complements and implements BeanFactory.
BeanFactory is simple and crude, which can be understood as HashMap:
Key - bean name
Value - bean object
But it generally only has two functions, get and put, so it is called "low-level container".
ApplicationContext has a lot of functions, because it inherits multiple interfaces, which can be called "advanced container." We will use it in the build project below.
There are two specific implementation subclasses in ApplicationContext, which are used to read configuration accessories:
ClassPathXmlApplicationContext -loads configuration files from class path, more commonly;
FileSystemXmlApplicationContext -Load configuration files from local files, not very common, if you go to Linux environment, you have to change the path, not very convenient.
When we click ClassPathXmlApplicationContext, we find that it does not directly inherit ApplicationContext, it has many layers of dependencies, and the subclasses of each layer are complementary implementations of the parent class.
And looking up, I found that the top class went back to BeanFactory, so it was very important.
Note that there is also a FactoryBean in Spring. There is no special relationship between the two, but the names are close, so don't mix up the order.
To understand IoC, let's review the process of writing code without IoC.
Deep understanding of IoC
Here is an example of the classic class Rectangle:
Two variables: length and width
Automatic generation of set() and toString() methods
Note: Be sure to generate the set() method, because Spring IoC is injected through this set() method; toString() method is for our convenience to print and view.
public class Rectangle { private int width; private int length; public Rectangle() { System.out.println("Hello World! "); } public void setWidth(int widTth) { this.width = widTth; } public void setLength(int length) { this.length = length; } @Override public String toString() { return "Rectangle{" + "width=" + width + ", length=" + length + '}'; }}
Then manually assign values to variables in the test file using the set() method.
Well, this is actually the process of decoupling!
public class MyTest { @Test public void myTest() { Rectangle rect = new Rectangle(); rect.setLength(2); rect.setWidth(3); System.out.println(rect); }}
In fact, this is IoC to attribute assignment implementation method, we put the "process of creating objects" transferred to the set() method, rather than relying on their own to new, not their own creation.
Here I say "create yourself", refers to directly in the object to new, is the positive process of the program to actively create objects; here use the set() method, is someone else (test) to me; IoC is to use its container to create, manage these objects, in fact, is also using the set() method, do not believe, you remove this method or change the name try?
A few key questions:
What is control, what is control?
A: It is the right to create and manage beans and control the entire life cycle of beans.
What is reversal, what is reversal?
A: Give this right to the Spring container, instead of controlling it yourself, or reverse it. The process of actively creating objects by ourselves before becoming passive in receiving objects given to us by others is inversion.
Take an example from life, active investment and passive investment.
Those who speculate in stocks and choose stocks are active investors, and the initiative is in their own hands; while those who buy funds are passive investors, giving the initiative to the fund manager, unless you sell the fund, otherwise the specific investment products are decided by the fund manager.
dependency injection
Back in the documentation, the second sentence says IoC is also known as DI.
Let's talk about dependency injection.
What is dependence and what is dependence?
The program needs to rely on external resources to run, providing the data and resources needed by the objects in the program.
What is injection, what is injection?
Configuration files inject resources from outside to inside, containers load external files, objects, and data, and then inject these resources into objects within the program, maintaining dependencies between objects inside and outside the program.
Therefore, control inversion is achieved by dependent injection. But you taste, you taste, they are different, like "the same thing described from different angles":
IoC is the design idea, DI is the specific implementation;
IoC is theory, DI is practice;
So as to achieve decoupling between objects.
Of course, IoC can be implemented in other ways, and DI is just an option for Spring.
IoC and DI are not proposed by Spring Framework, Spring just applies this design idea and concept to its own framework.
why
So why use IoC? In other words, what good can IoC do for us?
A: Uncoupling.
It turns dependencies between objects into configuration files that are managed by Spring IoC Containers.
In a project, the underlying implementation consists of a number of objects that work together to implement the business logic of the project. However, many objects are closely combined together. Once one party has a problem, it will inevitably affect other objects. Therefore, there is this design idea of understanding lotus root.
As shown in the above figure, the ABCD is originally associated with each other. When the management of the third-party container is added, each object is associated with the IoC container of the third-party method, and there is no direct connection between them. There is no coupling relationship. All objects are controlled by the container, which reduces the intimacy of these objects. It is called "decoupling."
Thank you for reading, the above is the content of "Spring IoC how to understand", after the study of this article, I believe that we have a deeper understanding of Spring IoC how to understand this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.