Spring--understanding of the basic concept of Ioc/di

Source: Internet
Author: User
Tags java se

Spring--Ioc/di basic concept

Mind Mapping:

-------------------------------------------------------

The basic concept of Ioc/di

 What is the IOC?

IoC--inversion of control, inversion of controls

In Java development, IOC means handing over your designed objects to container control rather than the traditional direct control within your object. IOC is a design approach that allows service consumers to not rely directly on service providers, and is designed to reduce dependencies between classes and classes.

The key to understanding IOC is " who controls who, what controls, why it's reversed, what's reversed ", and Below is an in-depth look at:

   who controls who, what controls : Traditional Java SE programming, we directly within the object through the creation of new objects, is the program actively to create objects; The IOC has a special container to create these objects, that is, the IOC container to control the creation of objects; Control what---> mainly control the acquisition of external resources

Why reverse, what is reversed : There is a reversal of the forward, the traditional application is our own active control in the object to directly obtain the dependent object, that is, the reverse is the container to help create and inject dependent objects;       Why reverse?---> because the container helps us to find and inject the dependent object, the object is only passively accepting the dependent object, so it is reversed;  What aspects are reversed? The acquisition of---> Dependent objects is reversed.

Traditional program design, are to create the relevant objects and then combined together, the following diagram illustrates:

    

When you have a Ioc/di container, you are no longer actively creating these objects in the client class, such as:

    

  What can the IOC do?

    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 actively create dependent objects within the class, which leads to high coupling between classes and classes, which is difficult to test; with the IOC container, the control of creating objects 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, which facilitates testing, facilitates functional reuse, and, more importantly, makes the entire architecture of the program very flexible.

In fact, the most important change of the IOC to the programming is not from the code, but from the thought, occurred " master-slave transposition " of the change, the application is originally the eldest, to obtain what resources are active attack, but in Ioc/di thinking, the application becomes passive, Passively waits for the IOC container to create and inject the resources it needs.

  What is di?

DI--Dependency injection, Dependency injection

that is, the dependencies between components are determined by the container at run time, in the image, that is, the container dynamically injects some kind of dependency into the component . the goal of dependency injection is not to bring more functions to the software system, but to improve the probability 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 concern 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," and here's an in-depth analysis:

    Who depends on who : The application relies on the Ioc/di container

    Why you need to rely on: Because after the reversal, the application relies on all the resources in the Ioc/di container

    who injects who : Ioc/di container Injection Application

    What is injected : the external resources that the application needs are injected, such as: dependencies

  

  Is the same concept between control inversion and dependency injection?

They are not the same concept, but They are a description of the different angles of the same concept.

Control reversal (IOC) from the perspective of the IOC container

Dependency Injection (DI) from an application perspective

  

  What is an external resource?

For a class, the so-called external resource refers to something that cannot be obtained or implemented within its own class.

For example, in a class to read a configuration file, then this configuration file is equivalent to this class of external resources

For example, in Class A to invoke Class B, then for Class A, class B is an external resource.

-------------------------------------------------------------------

Ioc/di Basic Thought Evolution

 (1) Java Foundation phase

    

In Class C, you need to use interface A, the most primitive of which is:

A = new A1 ();  or a A = new A2 (); Then through a to call the method of the interface, for C is the active instantiation of the object, directly to obtain the dependency .

The disadvantages of this implementation approach:

A, replacement implementation needs to recompile the source code

B, difficult to replace the implementation, difficult to test

C, coupling instance producers and instance consumers

 (2) Factory stage

  

Using the Factory mode-Factory, you need to use interface A in Class C, the application Factory mode is:

A = Factory.createa (); Then through a to invoke the method of the interface, so that for C is a passive instantiation of the object, indirectly obtain dependency, but for the factory class is the active instantiation of the object, get dependency

  What are the drawbacks of the above implementations:

1, just to instantiate the object in Phase 1, get dependencies into the factory, but the active problem is still there

2, replace the implementation needs to recompile the source code

3, difficult to replace the implementation, difficult to test

 (3) factory+xml+ reflex Stage

  

Continue to evolve to use Factory mode plus config file---Factory + XML + Reflection

To use interface A in Class C, the application factory pattern is:

A = Factory.createa (); The method of the interface is then invoked through a. Use a configuration file inside the factory class to determine the specific class to instantiate. For the Class C is passive creation of objects, indirect access to dependencies, for the factory is also passive .

The disadvantages of the above implementation methods:

1. How to instantiate a class with parameters

2. How to pass a value to the calling method

 (4) Ioc/di stage

  

Continue to evolve, to the level of the Ioc/di, in this design, there is a Ioc/di container, the container for the ioc/di involved in the overall control of the elements, and provide more and better General Service.

In this case, you need to use interface A in Class C, with Sprin as the column:

A = Beanfactory.getbean ("XXX"); Then the method of invoking the interface through a

At this point, the Ioc/di container is created to create objects and assembly objects and to manage the object life cycle. For applications, passive instantiation and passive acceptance are dependent.

-------------------------------------------------------------------

The concept of Ioc/di containers and the concept of beans

  The basic idea of Ioc/di :

1. Remove the dependency between programs

2, set the program object to the Ioc/di container configuration, as a bean to unified management

3. To manage the creation and instantiation of bean by Ioc/di container

4. The Ioc/di container to inject the relationship between the beans into the objects that need these relationships

Simply put: The dependencies between objects are stripped away, and the Ioc/di container manages the dependencies between objects and objects, enabling loose coupling between objects.

IOC container: The simple understanding is that implementing IOC ideas and providing object creation, object assembly, and object lifecycle management software is the IOC container

IOC understands:

1, the application does not need the active new object , but the description of how the object should be created, the IOC container to help you create, that is, passive instantiation ;

2, the application does not need to actively assemble the dependencies between objects , but to describe which service needs,IOC container will help you assemble, passively accept assembly;

3, active Passive , previously created objects, the dependencies of the assembly objects are actively completed by the application, and now become passive by the IOC container to complete;

4, the application does not know the specific implementation of dependency, only know the need to provide some kind of service object, to achieve and loose coupling;

5, is a service consumer does not directly depend on the service provider's component design method, is a kind of reducing the dependency between class and class design principle.

  using Ioc/di container development requires a change of mindset :

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

2, the Assembly of the service is not directly in the application code, but to describe which component requires which service, the container is responsible for assembling these together

That is, all components are passive, component initialization and assembly are handled by the container, and the application simply acquires the appropriate components and implements the functionality of the application.

(Ps:ioc/di is thought, not pure implementation technology.) IOC is a framework commonality, but the control of the transfer to the framework, it is not because the implementation of the IOC is called the IOC container, and generally in addition to implementing the IOC, but also has the DI function is called the IOC container, because the container is responsible for creating and assembling the component relationship, but also need to manage the life cycle of the component

--------------------------------------------------------------------------

(PS: I do not produce things, this article is collected from the Internet, collation, thinking after a summary)

Spring--understanding of the basic concept of Ioc/di

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.