SpringICO and the difference between DI

 Spring Framework learned some people will have heard of the Spring IoC (Inversion of Control), DI (dependency injection) of these two concepts for the beginner Spring people who always feel IoC, DI these two concepts is unclear It is difficult to understand, and share online today some of the technical understanding of the large cattle IOC Spring framework as well as talk about my understanding of the Spring Ioc.
A share Iteye opening Tao wonderful explanation of Ioc the
  first to share Tao is Iteye open the technical understanding of the cattle of the IOC Spring framework, written in a very easy to understand, following all from the original, the original address: http : //jinnianshilongnian.iteye.com/blog/1413846
1.1, IoC is what
  Ioc-inversion of control, that is, "inversion of control", not a technical, but a design idea. In Java development, Ioc means that you designed objects to the container control, rather than the traditional direct control over your internal object. Ioc understand how good it? The key to understanding good Ioc is to determine, "Who controls what control, why is reversed (reversal should have a positive turn), which reversed the terms," that we have to analyze in depth:
  ● Who controls What control: traditional Java SE programming, we directly inside the object through the new to create an object, a program initiative to create dependent objects; and IoC is a special container to create these objects, that is controlled by the Ioc container to create objects ; who controls? Of course IoC container control object; what control? That is the main control external access to resources (including not just objects such as files, etc.).
  ● Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in an object to acquire control directly dependent objects, that is, forward; while the reverse is from the container to help create and inject dependent objects; why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is the reverse; what reversed? The acquisition is dependent on the object reversed.
  With a legend to explain that traditional programming shown in Figure 2-1, the initiative to create the relevant objects are then combined:

1-1 a schematic view of FIG legacy application
  when there IoC / DI container, to create these objects are no longer active in the client class shown in Figure 2-2:

Figure 1-2 have the IoC / DI container program structure diagram
1.2, what do IoC
  IoC is not a technology, but an idea, an important rule of object-oriented programming that guides how we design a loosely coupled, and more excellent program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible.
  In fact, IoC programming bring the biggest change is not from the code, but ideologically, "master-slave transposition" of change. The application was originally boss, to get what resources are to take the initiative, but in IoC / DI thought, the application becomes passive, and passive waiting IoC container to create and inject the resources it needs.
  IoC good indication of one of the objects oriented design rules - Hollywood rule: "Do not come to us, we are looking for you"; that is, to help find the appropriate objects by the IoC container object and dependency injection, rather than take the initiative to find the object.
1.3, IoC and DI
  DI-Dependency Injection, namely "dependency injection": dependencies between components at runtime is determined by the vessel, said the image that will be injected by the container dynamic dependencies into a component. Dependency injection is not intended to bring more functionality to software systems, but to enhance component reuse frequencies, and for the system to build a flexible, scalable platform. Via dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who to achieve.
  DI is the key to understanding: "Who dependence, why rely on, who is who inject, inject something" that we have to analyze in depth:
  ● who depend on who is: of course, is application dependent on the IoC container;
  ● Why rely on: the application needs IoC container to provide external resources required objects;
  ● Who injection: Obviously IoC container is injected into an application object, the application-dependent objects;
  ● injected something: that is injected into a external resources required for the object (including an object, resources, constant data).
  IoC and DI by what to do with it? In fact, they are the same concept describing a different point of view, due to the inversion of control concepts vague (probably only be understood as a container control object this level, it is hard to think of going to protect object-relational), so in 2004 the master at Martin Fowler It has given a new name: "Dependency injection", relatively speaking IoC, "dependency injection" clearly describes the "object dependency is injected IoC container configuration dependent objects."
  Read a lot of articles on Spring's Ioc understand, a lot of people Ioc interpretation and DI are obscure, anyway, is a kind of inexplicable feeling, after reading it is still a loss, the feeling is open Tao the cattle were written especially user-friendly technology, he clearly explains IoC (inversion of control) and DI (dependency injection) in every word, giving a feeling of clear insight after reading it. I believe that for beginners who Ioc understanding of Spring Framework should be of great help.
Second, easy to understand explanations of IoC and DI on the blog to share Bromon of
2.1, IoC (Inversion of Control)
  First of all want to say that IoC (Inversion of Control, Inversion of Control). This is the core of spring, throughout. The so-called IoC, for spring framework, it is the relationship between the spring be responsible for controlling the lifetime of the object and the object. What does it mean, give a simple example of how we are to find a girlfriend? Common situation is that we see everywhere where there was pretty tall and good mm, then inquire about their hobbies, qq number, phone number, ip number, iq No. ........., think of ways to know them, cast good to send it to, then hey ...... this process is complex esoteric, we must confront their own design and every aspect. The traditional application development, too, in an object, if you want to use another object, you have to get it (himself a new, or a query from JNDI), even after you are finished using the object is destroyed (such as Connection, etc.), and other objects will always interface or class coupled together.
  So IoC is how to do it? A bit like looking for a girlfriend through matchmaking between my girlfriend and introduces a third party: marriage agency. Matchmaking management information on a lot of men and women, I can make a list of the matchmaking, tell it what I want to find a girlfriend, for example, looks like Michelle Reis, built like Lin Xi Lei, sing like Jay, like Carlos speed, techniques like Qi Dardenne and the like, and matchmaking will be in accordance with our requirements, providing a mm, we just go to her and fall in love, get married on the line. Simple and clear, if we give matchmaking candidates do not meet the requirements, we will throw an exception. The whole process is no longer controlled by my own, but there is such a matchmaking agency similar container to control. Spring encourages development approach is the case, all classes will be registered in the spring container, tell spring you are something, you need something, then spring will run the appropriate time in the system, the things you want to take the initiative to give you as well as other needs you to put your stuff. All of the class is created, destroyed by the spring to control, that control object is no longer referenced its object life cycle, but the spring. For a specific subject, before it is to control other objects, all objects are now controlled spring, so called inversion of control.
2.2, DI (dependency injection)
  IoC is a focus of the system is running, the dynamic of other objects it needs to provide to an object. This is achieved by DI (Dependency Injection, dependency injection). A need to manipulate database objects such as, in the past, we always want to write their own code A, to get a Connection object, with the spring we just need to tell spring, A requires a Connection, Connection as to how this structure, when constructed, A need to know. The system is running, spring will create a Connection at the appropriate time, and then the same as injections, which is injected into A, thus completing the control of the relationships between the various objects. A need to rely on Connection to run properly, and this Connection is injected into A by the spring, and dependency injection name it came. So DI is how to achieve it? An important feature of Java is 1.3 after reflection (Reflection), when the program is running that allows the generation of dynamic objects, the object execution method, changing object properties, spring implantation is achieved by reflection.
  After understanding the concept of IoC and DI, everything will become simple and clear, the rest of the work is only just piled wood in the framework of the spring.
Third, I IoC (Inversion of Control) and DI (dependency injection) understanding
  Two or more objects need to collaborate completed at least in the usual java application development, we want to achieve a certain function or is a complete business logic when not using Spring, each object needs to use his when partner, are themselves to be used as new object () syntax such cooperation will create objects out of this cooperation is to create objects out of their own initiative, to create a partner of the initiative in their own hands, which their own partner needs to take the initiative to create, create partner of the initiative and create opportunity by themselves to control, and this will make a high degree of coupling between objects, a partner B using the object needs to have completed one thing, a to use the B, then a to B to produce dependence, that is, there is a coupling between a and B, and is tightly coupled together, but after using Spring is not the same, to create a working partner B is to do by the Spring, Spring objects created B, and then stored in a container which, when the object B requires the use of the object a, the object stored on Spring Remove the container inside the B object A to be used, and then to use the object A, as Spring is how to create that object, and when the object is created, the object A do not care about these details (When did you born , how that comes I do not care, help me to work on the line), a Spring to get behind our target, two people can work together collaboratively to accomplish.
  So Inversion of Control IoC (Inversion of Control) is said to create a control object is transferred, the previous initiative to create objects and create opportunity by themselves to control, and now this power transferred to a third party, such as transfers to the IoC container, which is designed to create a factory object, what you want to target, it will give you what objects, with the IoC container, dependency had changed, the original dependency gone, they all depend on IoC container, and to establish a relationship between them through IoC container.
  This is my understanding of Spring's IoC (Inversion of Control) in. DI (dependency injection) is actually another way of saying the IOC, DI by Martin Fowler in a paper in early 2004 first proposed. He concluded: what controls are reversed? It is: way to get dependent objects reversed.
IV Summary
  for Spring Ioc the core concept, I believe that every person will have to learn Spring's own understanding. Understanding of this concept is no absolute standard answer, eyes of the beholder wise see wisdom. If there is understanding where not in place or understand correctly, the Friends are welcome to park correct me!

Guess you like

Origin www.cnblogs.com/xiaohouye/p/11134267.html