Spring's IOC principle (plain)

  1. IoC theoretical background

We all know that in object-oriented software design methods, which are from the underlying implementation Ñ composed of objects, all objects through mutual cooperation, and ultimately the business logic of the system.

If we open the back cover mechanical watches, you will see similar to the above case, the gears were driven by the hour, minute and second hands clockwise rotation, resulting in the correct time on the dial. Is described such a gear set of FIG. 1, it has a plurality of individual gears, the gears meshed with each other, work together to accomplish a common task. We can see that in such a gear set, a gear if there is a problem, it may affect the normal operation of the entire gear set.

The engagement between the gear set, coupling between the objects is very similar to the system software. Coupling between objects is inevitable and necessary, which is the basis for collaborative work. Now, along with the industrial application of increasingly large scale, dependencies between objects have become increasingly complex, often multiple dependencies between objects would appear, therefore, architects and designers for systems analysis and design, will face even greater challenges. Coupling system between objects is too high, the situation is indeed affect the body's inevitable.

Coupling relationship will not only appear between the object and the object will appear between the modules of the software system, as well as between software and hardware systems. How to reduce between systems, coupling between modules and between the objects is one of the eternal pursuit of the goal of software engineering. In order to solve the coupling between objects too high a problem, Michael Mattson software experts proposed IOC theory, to achieve the "decoupling" between objects, the current theory has been successfully applied to practice, a lot of J2EE projects We have adopted the IOC spring framework products.

2. What is Inversion of Control (IoC) IOC is Inversion of Control acronym, most books translated as "inversion of control", and some books translated into "reverse-control" or "control upside down." In 1996, Michael Mattson in the article about the object-oriented framework to explore a, first proposed the concept of IOC. For the basic idea of ​​object-oriented design and programming, We have already talked a lot, and not repeat, in short, is to break down complex systems into a cooperative interaction of objects, these object classes after the adoption of the package, the internal implementation is transparent to external , thereby reducing the complexity of the problem, and can be flexibly reused and extended views .IOC generally proposed theory is this: by means of a "third party" to achieve, as shown between objects having dependency:

We see it, with the introduction of an intermediate position of the "third party", i.e. IOC container, such that A, B, C, d four objects no coupling relationship between the drive gear all rely on the "third party" the , control of all objects of all turned over to the "third party" IOC container, therefore, IOC container has become a key core of the system, which plays a similar "glue" role, all the objects in the system of bonded together play a role, if not the "glue" between the object and the object will be lost contact with each other, which is the IOC container was likened to the origin of the "glue".

We come to a test: the IOC container between the figure above removed, and then look at the system:

We now see the picture that we want to realize the full contents of the entire system needs to be done. At this time, A, B, C, d between these four objects has no coupling relationship, there is no contact with each other, so, when you achieve A's, no need to go to consider the B, C and d, and dependencies between objects has been reduced to a minimum. So, if you really can achieve IOC container, for systems development, which it would be a wonderful thing, as long as each member involved in the development of its class to achieve it, has nothing to do with others!

Let us look, Inversion of Control (IOC) in the end why play such a name we compare?

The system software is not introduced until the IOC container shown in Figure 1, the object A is dependent on the object B, then the object A to a point of time, he must take the initiative to create the object or the object B has been used or created during the initialization operation B. whether you are creating or using an object B, control in their own hands. After the introduction of the software system IOC container, completely changed this situation, shown in Figure 3, since the addition of IOC containers between the object A and the object B loses direct contact, when the object A moves to a subject in need B when, IOC container will automatically create an object to place the object methyl acetate injection needed.

By comparing before and after, we do not hard to see: A process of obtaining the object of dependent objects B, by the active behavior becomes a passive behavior, control over reversed, this is the "inversion of control" the origin of the name.

  1. IOC alias: Dependency Injection (DI) in 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, then in the end it is "what is inverted control of it?" After a detailed analysis and after the demonstration he got the answer: "after the process of obtaining object is dependent on the inverted" control is reversed, the process of obtaining their dependent objects managed by the IOC container active injection so he "invert" take a more appropriate the name is "dependency injection (Dependency injection)". His answer, in fact, shows how to achieve the IOC: the injection. The so-called dependency injection, that is, the IOC container during operation, dynamic dependencies injected into certain objects.

Therefore, dependency injection (DI) and reverse control (IOC) is the same thing from different angles described, by introducing the IOC container means is by way of injection dependency, decoupling between objects. We give an example of a life, to help understand dependency injection process. Everyone on the USB interface and USB devices should be very familiar with it, USB provides a great convenience for us to use the computer, there are a lot of external devices support USB interface.

Now, we use the host computer and the USB interface to achieve a mission: to read a file from an external USB device.

The host computer reads the file, it would not be concerned about the connection of the USB interface is what external device, but it also does not need to know. Its mission is to read the USB interface, an external device attached that they meet the USB interface standard can be. So, if I give a host computer connected to the U disk, then the host will read files from U disk; if I connect to a host computer on an external hard drive, then the host computer to read files from an external hard drive. Power external devices attached decide for me, that control rests with me, as to what is the USB interface mounted device, the host computer is not decided, it can only passively accept. When the host computer needs an external device, do not tell me it, I will take the initiative to help it put an external device it wants, you see how my service is in place. This is an example of a dependency injection of our common life. In the process, I played the role of IOC container.

Through this example, the idea of ​​dependency injection has been very clear: When the host computer to read the file, I put it to rely on an external device, to help him on the hook. The entire injection processes and the external device is a dependent object is injected into the interior of another object in the system is running exactly the same procedure.

We dependency injection applied to software systems, again a description of the process:

A target depends on the object B, when the need to use the object A object B, IOC will create a container object B immediately sent to the object A. IOC container manufacturing plant is an object, what you need, it will give you send, you directly on the line, and no longer have to care about what you used is made of how, they do not care about how he has been finally destroyed , and it all arranged by the IOC container.

In conventional implementations, the program code to control the relationship between the internal components. We often use the new keyword combinations to achieve the relationship between the two components, this implementation will cause coupling between components. IOC a good solution to this problem, it will realize the relationship between the components mentioned outer container from within the program, that is to say by the container at runtime will dynamically inject some component dependencies between components.

  1. IOC brought us any good

We start with the example of USB, USB external devices using the built-in hard disk than, in the end bring any good? First, USB device as an external host computer device, before inserting the host, and the host computer does not have any relationship, only after we are together, both before contact occurs, relevant. So, no matter what the problem either both appear, it will not affect the operation of the other. This feature is embodied in software engineering, maintainability is good, very good for unit testing, debugging and easy to diagnose. Each Class code can be tested separately, independently of each other to each other, as long as their functions can correct, that is, low or no coupling between components coupled brings benefits. Second, between USB devices and the host computer's independence, but also brings another benefit, the production of USB device manufacturers and manufacturers producing mainframe computer may be completely unrelated people, all doing things, among them the only need is to comply with the standard USB interface. This characteristic is reflected in the software development process, but the benefits too. Each development team members only need to be concerned to achieve their own business logic, do not need to care about other people who work in progress, because your job has nothing to do with others, you can test tasks alone, you do not have to depend on the task other people's assembly, no longer have to pull clear responsibility. So, in a medium-sized project, team members clear division of responsibility clear, it is easy to be a big task into small tasks, development efficiency and product quality will be greatly enhanced. Third, with a USB external devices can be plugged into any USB-enabled device, you can plug into the host computer, the machine can be plugged into DV, USB external device may be used repeatedly. In software engineering, this feature is a good reusability, we can have common components universal independent, repeatedly used to other parts of the project or other projects, of course, this is also the basic features of object-oriented. Obviously, IOC not only to better implement this principle, increase the reusability of modules. Achieve compliance with the interface standard, can be plugged into the module supports this standard. Fourth, with the external USB device, the module has a hot swappable. IOC generates object method to the external mode, that is, the resulting object is defined in the configuration file, so that when we replace an implementation subclass will become very simple, just modify the configuration files on it, complete with Hot Swap feature.

  1. IOC containers Technical Analysis

IOC most basic technology is the "reflection (Reflection)" program, currently .Net C #, Java, and other languages ​​are PHP5 support, PHP5 technical books, also sometimes translated as "mapping." The concept and uses of reflection, we should be very clear, popular terms is to dynamically generate objects given class name (character string). This style of programming can make decisions only if the object is generated in the end is what kind of object. Application of reflection is very broad, many mature frameworks, such as Java, Hibernate, Spring Framework, .Net in NHibernate, Spring.Net framework is the "reflection" as the most basic techniques. Reflective technology actually appeared very early, but has been ignored, not being further utilized. Reflecting the time programming mode with respect to the normal to the object generation slow manner at least 10 times. Current technology through improvements optimized reflection, is well established, and typically generate an object in reflection object generation mode, the speed has little difference, about 1-2 times the gap.

We can work mode IOC containers seen as a sublimation of the factory model, it can be seen as an IOC container factory, this factory to produce the object definitions given in the configuration file, and then use programming language programming reflection to form the corresponding object class name given in the configuration file. From the implementation point of view, IOC is put before the factory method to write the dead object code generation, by changing the configuration file to define, that is, the plant generates both independent and objects separated by, the purpose is to improve the flexibility and maintainability.

  1. Some IOC container products

IOC container under Sun ONE technology system are: there is a lightweight Spring, Guice, Pico Container, Avalon, HiveMind; heavyweight has the EJB; there is no light not heavy JBoss, Jdon like. As one of the Spring Framework Java development SSH (Struts, Spring, Hibernate) Three Musketeers, medium and small projects have to use, very mature and widely used, EJB also used in critical industrial projects, such as certain telecommunications business. IOC container under the .Net technology system has: Spring.Net, Castle and so on. Spring.Net is a transplant from the Spring Java over the IOC container, Castle Windsor is part of IOC container. They are lightweight framework, more mature, which Spring.Net has been increasingly used in various projects.

  1. What should be noted that the use of IOC framework

Using an IOC Framework products can bring great benefits to our development process, but also fully aware of the shortcomings with IOC framework, be aware of, prevent abuse framework. First, the software system due to the introduction of third-party IOC container, the step of generating objects become somewhat complicated, could have been something between the two, but nothing out of a procedure, so we just started using the framework of the IOC when You will feel the system becomes less intuitive. Therefore, the introduction of a new framework, will increase the cost of learning and training team members met, and after the operation and maintenance, the new entrants have had to make the same body of knowledge. Second, since the objects are generated by the IOC container reflection mode, a certain loss in operating efficiency. If you want to pursue operational efficiency, it must be weighed against this. Third, the IOC framework specific to the product (for example: Spring) is concerned, it requires a lot of preparation work, more complicated, for some small projects, the objective is also likely to increase the cost of some of the work. Fourth, the product itself IOC framework maturity need to be evaluated, if a product is introduced IOC framework immature, it will affect the whole project, so this is a hidden risk. We can roughly conclude that: Some small projects or work product, not suitable for use IOC framework products. In addition, if the lack of knowledge and ability of team members, IOC framework for the lack of deep understanding of the product, and do not rush to introduce. Finally, with particular emphasis on the efficiency of project or product, it is not suitable for the introduction of products IOC framework, as is the case WEB2.0 website.

Reproduced in: https: //juejin.im/post/5d036363f265da1b9163902b

Guess you like

Origin blog.csdn.net/weixin_34221332/article/details/93181928