The principle of IOC (inversion of control) in the spring framework

Source: Internet
Author: User
Tags define abstract

I. Fundamentals and principles of the IOC:

The background of 1.IoC theory: in the software system with object-oriented method design, the bottom-level implementation is composed of N objects, all the objects through each other's cooperation, finally realizes the system business logic. 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 there is a complex dependency between objects and objects, so there is a theory of control inversion.



2. What is inversion of control (IoC):

(1). The IOC is the abbreviation for the 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 exploring an object-oriented framework, first proposed the concept of IOC. In simple terms, the complex systems are decomposed into objects of mutual cooperation, and after encapsulation, the internal implementation is transparent to the outside, which reduces the complexity of solving the problem and can be reused and extended flexibly. 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. The object and the object are then contacted by the IOC container, but there is no direct connection between the object and the object.

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?

-Software system before an IOC container is introduced, object a relies on object B, so when an object is instantiated or run to a point, it must either actively create object B or use an already created object B, whether creating or using the Created object B, control is in our own hands.

-If the software system introduces an IOC container, there is a loss of direct contact between object A and object B, so when object A is instantiated and run, if object B is required, the IOC container will proactively create a place where object B is injected into object A.

-By the previous comparison, you can see that object a obtains dependent object B process, from the active behavior into the passive behavior, that is, the creation of the object to the IOC container processing, control reversed, this is the origin of control reversal!



Aliases for 3.IoC: Dependency Injection (DI)

(1). In 2004, Martin Fowler explored the same issue, since the IOC was a reversal of control, so "what controls are reversed?" After detailed analysis and argument, he came to the answer: "The process of acquiring dependent objects has been reversed." After the control is reversed, the process of acquiring a dependent object becomes an active injection by the IOC container from its own management object. So he gave the "inversion of control" a more appropriate name called "Dependency Injection (Dependency injection,di)". His answer, in effect, gives a way to implement the IOC: inject.

(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 inversion of control (IoC) are the same thing that is described from different angles, that is, by introducing an IoC container, the decoupling of objects is realized by means of dependency injection.



4. 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 tested individually, without affecting each other, as long as the function is correct, which is the benefit of low coupling or no coupling between components.

(2). Each member of the development team only needs to focus on the business logic they want to implement, without having to care about other people's work, because your tasks are not related to others, your tasks can be tested individually, your tasks don't depend on other people's components, and you don't have to be confused about the responsibilities. Therefore, in a large and medium-sized project, the team members clear division of labor, responsibility is clear, it is easy to divide a large task into small tasks, development efficiency and product quality will be greatly improved.

(3). Reusability is good, we can separate the common components with universality, apply to other parts of the project repeatedly, or other projects, of course, this is the basic object-oriented characteristics. Clearly, IOC has implemented this principle better and improved the reusability of modules. Implementations that conform to the interface standards can be plugged into modules that support this standard.

(4). IOC generates objects in 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 is modified, fully with the hot-plug features.



5.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 find 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. The component is in a container and is managed by the container. In simple terms, it is the relationship between the container control program, rather than the traditional implementation, which is manipulated directly by the program code, that is, calling another class in one 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.



6. Factory mode:

(1). A design pattern, the Factory mode, is often used in the spring IOC. Factory mode provides an interface for creating objects.

(2). Factory mode means that when a component in an application requires the assistance of a component B, it is not directly instantiating the B Component object in a component, but rather through the factory of the B component, that is, the factory can generate instance objects of a type of component. In this mode, a component does not have to be coupled with the B component in a hard-coded manner, but only with the plant of the B-component.

(3) The following example is attached to the previous factory model:

First, create a new Java project Factorytest, and then create a new interface animal, placed under the Com.inter package:

The Animal.java file code is as follows:

Package Com.inter;public interface Animal {void eat ();//define abstract eating method void shout ();//define abstract called method}


Then the new two JavaBean, the dog class and the Cat class, were placed under the Com.bean package and implemented the animal interface:

The Dog.java file code is as follows:

Package Com.bean;import Com.inter.animal;public class Dog implements animal{@Overridepublic void Eat () {//TODO Auto-gene Rated Method StubSystem.out.println ("Dog Eats dog Food");} @Overridepublic void Shout () {//TODO auto-generated method StubSystem.out.println ("Barking Dog");}}


The Cat.java file code is as follows:

Package Com.bean;import Com.inter.animal;public class Cat implements animal{@Overridepublic void Eat () {//TODO Auto-gene Rated method StubSystem.out.println ("Cat Eats cat food"); @Overridepublic void Shout () {//TODO auto-generated method StubSystem.out.println ("Cat meow Meow");}}


Then create a new factory class factory, placed in Com.factory, so that the dog and cat classes are associated with the factory class, and the dog and cat classes are not directly related:

The Factory.java file code is as follows:

Package Com.factory;import Com.bean.cat;import Com.bean.dog;import Com.inter.animal;public class factory {public Animal getanimal (String name) {if (Name.equals ("Dog")) {return new Dog ();} else if (Name.equals ("Cat")) {return new Cat ();} Else{throw new IllegalArgumentException ("argument is incorrect! ");}}}

Finally, write a test class that is placed under the Com.test package with the following code:

Package Com.test;import Com.factory.factory;import Com.inter.animal;public class Test {public static void main (string[] args) {Animal a=null;a= (Animal) New Factory (). Getanimal ("dog"); A.eat (); A.shout (); a= (Animal) New Factory (). Getanimal ( "Cat"); A.eat (); A.shout ();}}


After running, the effect is as follows:






Two. Here we first introduce the fundamentals and principles of the IOC, the main components of the IOC and the two methods of injection, and I will write an additional article to avoid too many people to understand, we first understand the principles of the IOC well!

The above is the whole content, not written well, please forgive me, if there are errors, please point out, thank you!

The principle of IOC (inversion of control) in the spring framework

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.