Spring entry-control inversion (IOC) and dependency injection (DI)
1. Inversion of Control and Dependency Injection)
Control Inversion is IoC (Inversion of Control). It gives the call permission of objects that are traditionally directly controlled by program code to containers, and uses containers to assemble and manage object components. The so-called "control inversion" concept is the transfer of control over the component object, from the program code itself to the external container.
IoC is a big concept and can be implemented in different ways. There are two main implementation methods: <1> Dependency Lookup: The container provides the callback interface and context to the component. This method is used by both EJB and Apache aveon. <2> Dependency Injection: The component does not locate the query, but only provides a common Java method for the container to determine the Dependency. The latter is the most popular IoC type. It has three methods: Interface Injection, Setter Injection, and Constructor Injection.
Figure 1 concept structure of control reversal <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + examples/examples + examples/fT69Om08O0 + examples/examples + CiA8YnI + CjxzdHJvbmc + examples/examples + cDgo6iz6c/examples + cjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141101/20141101084410329.jpg" alt = "\">
Now let's take into account the implementation mechanism of IoC. components Define the entire process framework, and the implementation of some business logic needs to be added by adding other business objects, they can be involved in the business process in two ways. One is Dependency Lookup. Similar to JDNI implementation, the corresponding business object (Code 1) can be found through JNDI ), the other is dependency injection, which injects business objects into components through the IoC container.
3. Dependency Lookup)
The following code demonstrates the dependency Search Mechanism Based on JNDI.
public class MyBusniessObject{private DataSource ds;private MyCollaborator myCollaborator; public MyBusnissObject(){Context ctx = null;try{ctx = new InitialContext();ds = (DataSource) ctx.lookup(“java:comp/env/dataSourceName”);myCollaborator =(MyCollaborator) ctx.lookup(“java:comp/env/myCollaboratorName”); }……
The main problem with dependency lookup is that this code must be dependent on the JNDI environment, so it cannot run outside the application server. If you want to replace JNDI with other methods to find resources and collaboration objects, the JNDI code must be extracted and reconstructed into a policy method.
4. Dependency Injection)
The basic principle of dependency injection is that application components should not be responsible for searching resources or other dependent collaboration objects. The IoC container is responsible for the configuration object. The logic of "Search for resources" should be extracted from the code of the Application Component and handed over to the IoC container.
The following shows the injection mechanism in step 3.
Code 2 business object to be injected Content. java
package com.zj.ioc.di; public class Content { public void BusniessContent(){ System.out.println("do business"); } public void AnotherBusniessContent(){ System.out.println("do another business"); }}
The MyBusniess class shows a business component whose implementation requires object Content injection. Code 3, code 4, Code 5, and 6 demonstrate three methods: Constructor Injection, Setter Injection, and Interface Injection.
Code 3 Constructor Injection MyBusiness. java
package com.zj.ioc.di.ctor;import com.zj.ioc.di.Content; public class MyBusiness { private Content myContent; public MyBusiness(Content content) { myContent = content; } public void doBusiness(){ myContent.BusniessContent(); } public void doAnotherBusiness(){ myContent.AnotherBusniessContent(); }}
Code 4 Setter Injection MyBusiness. java
package com.zj.ioc.di.set;import com.zj.ioc.di.Content; public class MyBusiness { private Content myContent; public void setContent(Content content) { myContent = content; } public void doBusiness(){ myContent.BusniessContent(); } public void doAnotherBusiness(){ myContent.AnotherBusniessContent(); }}
Code 5 sets the InContent. java Injection Interface
package com.zj.ioc.di.iface;import com.zj.ioc.di.Content; public interface InContent { void createContent(Content content);}Code 6 Interface Injection MyBusiness. java
package com.zj.ioc.di.iface;import com.zj.ioc.di.Content; public class MyBusiness implements InContent{ private Content myContent; public void createContent(Content content) { myContent = content; } public void doBusniess(){ myContent.BusniessContent(); } public void doAnotherBusniess(){ myContent.AnotherBusniessContent(); }}
The above is the basic understanding of spring, but it takes a small step to understand spring. The true understanding should be put in practice.