Spring Framework Learning-inversion of control (IOC)

Source: Internet
Author: User


Spring is an open source framework, and spring is a lightweight Java development box that emerged in the 2003 The frame, created by Rod Johnson.

In short, spring is a layered javase/eefull-stack (one -stop) lightweight , open-source framework that is primarily used to reduce the coupling between modules

Spring In addition to being able to other functions are also available through the IoC to reduce the coupling between modules.


1, the basic knowledge and principles of the IOC:

The background of IOC theory: in a software system with object-oriented approach design, the underlying implementation is made up of N objects, all of which work together and ultimately

Implement the business logic of the system. That is , the coupling between objects in a software system, object A and object B are related, and object B is dependent on object C, so that objects and objects

Have complex dependencies, so there is the theory of inversion of control.


2. What is inversion of control (IoC):


(1) The IOC is an abbreviation for inversion of control, some translated into "inversion of controls", and translation as "control reversal" or "control inversion".

(2) In 1996, Michael Mattson, in an article on the object-oriented framework, first proposed the concept of IOC. In simple terms, the decomposition of complex systems

into mutually cooperative objects, these object classes are encapsulated, the internal implementation is transparent to the outside, which reduces the complexity of solving the problem and is flexible to be reused

and extensions. The idea of IOC theory is broadly like this: With the help of a "third party" to achieve decoupling between objects that have dependencies, as shown in:




after encapsulating each object class, the object classes are associated with the IOC container. This allows the object to be contacted by the IOC container, but between the object and the object

And there is no direct contact. If you remove the IOC container, object A in the system has a direct relationship to object B, as shown in:



For example, if a lot of object classes are to be associated, it becomes very complex, as shown in:



Therefore, it is necessary to put forward the IOC control inversion.


3. Why is this method called control reversal?


(1) software system before an IOC container is introduced, object a relies on object B, so a object must be actively created when it is instantiated or run to a point

B or Using object B that has already been created, whether it's creating or using object B that you've created, control is on our own hands.

( 2) If the software system introduces an IOC container, the direct contact between object A and object B is lost, so when object A is instantiated and run, if object B is required to

, the IOC container takes the initiative to create a place where object B is injected into object A.

(3) Through the previous comparison, you can see that object a obtains dependent on the object B process, from the active behavior into the passive behavior, that is, the creation of the object to the IOC container processing, control

The system is reversed, this is the origin of control reversal!




4. IOC Aliases: Dependency Injection (DI)


(1) In 2004, Martin Fowler explored the same issue, since the IOC is a control reversal, so what is the control being reversed? ", after detailed sub-

After analysis and argument, he came to the answer: "The process of acquiring dependent objects has been reversed". After control is reversed, the process of acquiring a dependent object is changed from its own management object to the IOC capacity

The active injection of the device. So he gave the "inversion of control" a more appropriate name called "Dependency Injection (Dependency injection,di)". He's this answer that actually

The method for implementing the IOC is given: injection.

(2) The so-called dependency injection is the dynamic injection of a dependency into an object by the IOC container during operation.

(3) Therefore, Dependency injection (DI) and control inversion (IOC) are the same thing described from different angles, that is, by introducing an IoC container, using dependency Injection

The decoupling of objects is realized by the method.



5. Benefits of using IOC:


(1) maintainability is good, very convenient for unit testing, easy to debug programs and diagnose faults. Each class in the code can be individually tested,

effect, as long as the function is correct, this is the benefits of low coupling or no coupling between components.

(2) The members of each development team only need to focus on the business logic they want to implement, without having to care about other people's work, because your task is not with anyone else.

Relationships, your tasks can be tested individually, and your tasks don't depend on other people's components, and you don't have to be responsible anymore. So, in a medium-to-large project, teams become

The Division of labor is clear, responsibility is clear, it is easy to divide a large task into a small task, the development efficiency and product quality will be greatly improved.

(3) reusability is good, we can separate the common components that are universal, apply to other parts of the project repeatedly, or other projects, of course, this is also for

The basic characteristics of the object. Clearly, IOC has implemented this principle better and improved the reusability of modules. Implementations that conform to the interface standards can be plugged into a module that supports this standard

Block.


(4) The way the IOC generates objects is changed to an external way, that is, the object generation is placed in the configuration file to define, so that when we replace a implementation subclass will become very

Simple, as long as the configuration file can be modified, fully with hot-swappable features.




6, the IOC principle:

Control inversion is the core of the spring framework. The principle is based on the object-oriented (OO) design principle of the Hollywood Principle:don ' t call us, we'll call you

(Don't look for me, I'll come to you). In other words, all components are passive, and all component initialization and invocation is the responsibility of the container. Component in a container when

is managed by the container. In simple terms, it is the relationship between the container control program, rather than the traditional implementation, controlled directly by the program code, that is, in a class called

Another class. This is the concept of the so-called "inversion of Control": control is transferred from the application code to the external container, the transfer of control, the so-called reversal.



See here, I believe you have a preliminary understanding of the IOC, then we look at the spring framework.

Lightweight-Spring is lightweight, both in terms of size and overhead. The full spring framework can be published in a jar file with a size of more than 1MB. And

and the processing overhead required by spring is negligible. In addition, spring is non-intrusive: Typically, objects in spring applications do not depend on a particular class of spring.

control reversal--spring facilitates low coupling through a technique called inversion of Control (IoC). when an IOC is applied, other objects that an object relies on are

Instead of creating or locating dependent objects by the object itself. You can think of the IOC as opposed to Jndi--not the object looking for dependencies from the container, but

The container actively passes the dependency to it when the object is initialized without unequal object requests.

Faceted --spring provides rich support for aspect-oriented programming, allowing business logic to decouple applications from system-level services such as auditing (auditing) and

(transaction) management) for the development of cohesion. The Application object only implements what they should do-complete the business logic-that's all. They are not responsible for (very

Awareness) Other system-level concerns, such as log or transaction support.

The container--spring contains and manages the configuration and lifecycle of the Application object, and in this sense it is a container where you can configure how each of your beans is created

Built-based on a configurable prototype (prototype), your bean can create a separate instance or generate a new instance each time it is needed--and they are

How they relate to each other. However, spring should not be confused with traditional heavyweight ejb containers, which are often bulky and cumbersome and difficult to use.

Framework--spring can be used to configure and assemble simple components into complex applications. in spring, application objects are combined declaratively, typically in an XML

In the file. Spring also provides a number of basic functions (transaction management, persistence framework integration, etc.), leaving the development of application logic to you.

The role of mvc--spring is to integrate, but not just to integrate, the Spring framework can be seen as an enterprise solution-level framework. Client Send please

The server controller (implemented by Dispatcherservlet) completes the forwarding of the request, and the controller invokes a class handlermapping for the mapping that is used to request

Mapped to the corresponding processor to process the request. Handlermapping maps the request to the corresponding processor controller (equivalent to action) in spring if you write a

Processor components, generally implement the Controller interface, in the controller can call some service or DAO for data manipulation Modelandview for storing

The data that is taken from the DAO can also hold some data from the response view. If you want to return the processing results to the user, a view group is also available in the Spring framework

Viewresolver, the component finds the corresponding view based on the indicator returned by the controller, and returns the response response to the user.


All of these features of spring enable you to write cleaner, more manageable, and easier-to-test code. They also provide basic support for the various modules in spring.





Spring Framework Learning-inversion of control (IOC)

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.