In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you what are the knowledge points of Spring IOC, the content is very detailed, interested friends can use for reference, hope to be helpful to you.
Generalized IOC
Before we do that, let's remember one sentence. Hollywood principle: Don't call me, we will call you. That is, "Don't call, we'll call you".
Inversion of Control (IoC for short) is a design principle in object-oriented programming, which can be used to reduce the coupling between computer code. One of the most common methods is called Dependency Injection (DI), and another is called dependency lookup (Dependency Lookup). Through control inversion, when an object is created, an external entity that regulates all objects in the system passes (injects) the references of the objects it depends on.
Two implementations: dependency lookup (DL) and dependency injection (DI).
The relationship between IOC and DI, DL:
DI (Dependency Injection) is the way used by Spring, and the container is responsible for the assembly of components.
DL (Dependency Lookup) has been abandoned.
The principle of IOC container
The IOC container is actually a big factory that manages all our objects and dependencies.
The principle is realized through the reflection technology of Java! Through reflection, we can get all the information about the class (member variables, class names, etc.)!
Then describe the relationship between classes through configuration files (xml) or annotations
We can use these configuration information and reflection techniques to build the corresponding objects and dependencies!
Let's take a brief look at how the actual Spring IOC container implements object creation and dependency:
Create a Bean definition registry inside the container based on Bean configuration information
Load and instantiate bean according to the registry, and establish the dependency relationship between Bean and Bean
Put these ready Bean into the Map cache pool and wait for the application to call
Spring containers (Bean factories) can be simply divided into two types:
BeanFactory
This is the most basic and Spring-oriented
ApplicationContext
ApplicationContext is a subclass of BeanFactory
If there are no special requirements, it should be done using ApplicationContext. Because BeanFactory can do everything that ApplicationContext can do, and it provides more features that are closer to what is currently being developed.
Understanding of IOC
First, IOC control reversal
Who controls who, controls what, and what is a reversal (if there is a reversal, there should be a positive reversal)
Who controls whom and what:
In the past, when there was no IOC, we created objects directly inside the object through new, and the program took the initiative to create dependent objects.
Now, IOC has a special container to create these objects, that is, the Ioc container controls the creation of objects.
Who controls who? Of course, it is the IOC container that controls the object; controls what? That is, it mainly controls access to external resources (not only objects but also files, etc.).
So-called reversal:
If there is a reversal, there should be a positive reversal. In traditional applications, we take the initiative to control ourselves in the object to directly obtain the dependent object, that is, the positive reversal.
The so-called reversal is actually the reversal of control, and the life cycle of the object is controlled by Spring, then the control of the object is completely out of our control and the control is handed over to Spring. This reversal means that we have changed from the controller of the object to the passive controller of IOC.
Second, when you think of IOC, you will think of DI dependency injection (IOC is implemented through DI), so:
Who depends on whom and why; who injects whom and what?
Who depends on whom: the An object depends on the IOC container.
Why do you need to rely on: an object needs the IOC container to provide data, B objects and other external resources that the object needs. Without these resources, business processing cannot be completed.
Who injects who: the IOC container injects the An object.
What is injected: the IOC container injects data needed by An objects, B objects, and other external resources into the object on demand.
Benefits: reduce the cost of development, improve the code reuse rate, software flexibility.
IOC: it's not a technology, it's a design idea. In Spring development, the creation, initialization and destruction of objects are controlled by the IOC container. This also realizes the reversal of the control of the object, from our control of the object to the control of the object by Spring IOC.
DI: is the concrete implementation of IOC. The program gives the dependency to the container, and the container helps you manage the dependency.
What are the Spring IOC knowledge points to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.