Java control inversion and dependency injection

Source: Internet
Author: User
Tags java se

Let's take a look at the basic meaning of control reversal:

The inversion of Control (IOC) mode (also known as di:dependency injection) is inversion of control, which controls inversion. In Java development, IOC means handing out your designed classes to the system rather than controlling them within your class. This is called control inversion. In other words, the task of creating a management object is given to the container.

Traditional Java SE Programming, we create objects directly within the object through new, is the program to create dependent objects, and the IOC has a special container to create these objects, that is, the IOC container to control the creation of objects; who controls who? The IOC container, of course, controls the object; That is the primary control of external resource acquisition (not just objects such as files, etc.).

The traditional application is the active control of our own objects in the object directly to obtain the dependent object, that is, the reverse is the container to help create and inject dependent objects; because the container helps us to find and inject dependent objects, the object is only passively accepting dependent objects, so it is reversed; The acquisition of the dependent object is reversed.

Traditional applications are created by us to create dependent objects within the class, which causes the class to be highly coupled to the class and difficult to test; with the IOC container, the control of creating and finding dependent objects is given to the container, which is injected by the container, so that the object is loosely coupled with the object, so it is also easy to test. Facilitates functional reuse and, more importantly, makes the entire architecture of the program very flexible.

    The biggest change the IOC has made to programming is not from the code, but from the mind, where the "master-slave transposition" has changed. The application was originally the boss, to get what resources are active attack, but in Ioc/di thought, the application becomes passive, passively wait for the IOC container to create and inject the resources it needs.

    IOC is a good embodiment of the object-oriented design of one of the rules of Hollywood: "Do not call us, we find you", that is, the IOC container for the object to find the corresponding dependent objects and injected, rather than the object to be actively looking for.

    a dependency is injected into the component dynamically by the container . the purpose of dependency injection is not to bring more functionality to a software system, but to increase the frequency of component reuse and to build a flexible and extensible platform for the system. through the dependency injection mechanism, we only need to use simple configuration, without any code to specify the resources required by the target, to complete its own business logic, without the need to care about where the specific resources come from and by whom.

This is like the idea of object-oriented programming,"I" acts as a portal class, in this portal class, I have to buy a pair of disposable chopsticks every time I eat (every use is new), in such a relationship, is "I" (that is, the caller) every time to " active "to buy disposable chopsticks (another class), I said to the chopsticks you honestly come over my hand, I control the chopsticks, that good, in this control is the relationship, put in real life, is certainly unrealistic, and people are lazy, he always to create more convenient for their own life ideas, More precisely, buy a pair of ordinary chopsticks (not disposable), put him in a container (called the IOC Container in spring), you need to use the container said:IOC I want to use chopsticks (request to the container), and then the chopsticks will be "injected" into the hand, In this process, you are no longer the controller , but instead become a requester (although itself or the caller), relying on the container to give you resources, control is located in the container, so this is commonly known as the control reversal .

The IOC's focus is on design thinking, from a conventional approach to creating objects, that is, the new object, to submit a "resume" to the IOC container, passively waiting for the IOC container to return resources to you. Control reversal refers to the "show company control Actors", and when it comes to dependence, "actors need company bread where", we need to rely on the object of the container to obtain, the process is to rely on injection. in essence, IOC and di are manifestations of different dimensions under the same thought .

The process of acquiring resources in control inversion is called Dependency injection, and the code implementation here is focused on dependency injection. There are 3 ways of dependency injection, namely, construction injection, set injection, and interface injection.

Java control inversion and dependency injection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.