Java record-spring two major features

Source: Internet
Author: User

1.IOC Control Inversion

Ioc-inversion of control, or "inversion of controls", is not a technique, but a design idea. In Java development, IOC means handing over your designed objects to container control rather than the traditional direct control within your object.

IoC is not a technology, but an idea, an important object-oriented programming law that can guide us in designing loosely coupled, better programs. 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.

In fact, the biggest change of the IOC to programming is not from the code, but from the thought, the "master-slave Transposition" change. 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.

2. Dependency Injection

Di-dependency injection, or "dependency injection": the dependencies between components are determined by the container at run time, in the image, that is, the container dynamically injects a dependency into the component. 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.

The key to Understanding di is: "Who depends on who, why they need it, who injects it, and what is injected into it," Let's look at it in depth:

Who depends on who: Of course the application relies on the IOC container;

Why you need to rely on: applications need an IOC container to provide the external resources required by the object;

Who injects who: it is obvious that an IOC container injects an object into an application, an application-dependent object;

What is injected: the external resources (including objects, resources, constant data) that are required to inject an object.

What is the relationship between the IOC and di? In fact, they are different angles of the same concept, because the concept of inversion of control is ambiguous (may be just understood as a container control object this level, it is difficult to think of who to maintain the object relationship), so the 2004 master figure Martin Fowler again gave a new name: "Dependency Injection", In contrast to the IOC, "dependency Injection" clearly describes the "injected object relies on the IOC container configuration dependent object".

    • Type Matching: Although we make service calls through interfaces (or abstract classes), the service itself is implemented in a specific service type, which requires a type registration mechanism to solve the matching relationship between the service interface and the service type;
    • Constructor Injection (Constructor injection): The IOC container intelligently chooses to select and invoke the appropriate constructors to create dependent objects. If the selected constructor has the corresponding parameters, the IOC container resolves the registered dependency and obtains the corresponding parameter object before invoking the constructor function;
    • Property Injection: If a property of the dependent object needs to be used, the IOC container will initialize the property automatically after the dependent object is created;
    • Method injection: If a dependent object needs to call a method for initialization, the IOC container automatically calls the method after the object is created.

Java record-spring two major features

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.