Spring Primer-Control inversion (IOC) and Dependency injection (DI)

Source: Internet
Author: User

1.Control Reversal(inversion of Control)and Dependency Injection(Dependency injection)

Control inversion, the IOC (inversion of control), gives the container the right to assemble and manage object components by means of a container, by handing over the call of objects that are traditionally manipulated directly by the program code. The so-called "inversion of Control" concept is the transfer of control over a Component object, from the program code itself to the external container.


The IOC is a big concept that can be implemented in different ways. There are two main implementations of:<1> dependent lookup (Dependency lookup): The container provides the callback interface and context to the component. Both EJB and Apache Avalon Use this approach. <2> Dependency Injection (Dependency injection): The component does not do a location query, only provides a common Java method for the container to determine the dependency relationship. The latter is the most popular type of IOC nowadays, and it has interface injection (Interface injection), set value injection (Setter injection) and construction sub-injection (Constructor injection) three ways.


Figure1 control inversion conceptual structure

Dependency injection is more prevalent because it is a more desirable way: to have the container take full responsibility for relying on the query, The managed component only needs to expose the JavaBean setter method or the constructor or interface with parameters so that the container can assemble the dependency of the object at initialization time. The main advantage is that the:<1> find-and-locate operation is completely unrelated to the application code, compared to the dependent lookup method. <2> does not depend on the container's API, it is easy to use the Application object outside any container. <3> does not require a special interface, and most objects do not have to rely on containers at all.
 
2. Hollywood principles The
IOC embodies the Hollywood principle that "don't call, we'll call you." The first time I met the Hollywood principle is that when you understand the template Mathod pattern, the core of the template method pattern is that the base class (abstract class) defines the skeleton of the algorithm, and some steps are deferred to the subclass.


Now consider the IOC implementation mechanism, the component defines the entire process framework, and some of the implementation of the business logic with the help of other business objects to join, they can participate in the business process in two ways, one is dependent on lookup (Dependency lookup), similar to the implementation of Jdni, The corresponding business object (code 1) is found through Jndi, and the other is dependency injection, which injects the business object into the component through the IOC container.

3. Dependency lookup (Dependency lookup)
The following code shows the dependency lookup mechanism based on the JNDI implementation.

public class Mybusniessobject{private DataSource ds;private mycollaborator mycollaborator, public mybusnissobject () { Context CTX = Null;try{ctx = new InitialContext ();d s = (DataSource) ctx.lookup ("Java:comp/env/datasourcename"); Mycollaborator = (mycollaborator) ctx.lookup ("Java:comp/env/mycollaboratorname");    } ......

The main problem with dependency lookups is that this code must be dependent on the JNDI environment, so it cannot run outside the application server, and if you want to replace Jndi in other ways to find resources and collaboration objects, you must extract the Jndi code to refactor into a policy method.

4. Dependency Injection (Dependency injection)
The basic principle of dependency injection is that the application component should not be responsible for locating resources or other dependent collaboration objects. The work of the configuration object should be the responsibility of the IOC container, and the logic for "find resources" should be taken from the code of the application component and given to the IOC container.
The following shows the injection mechanism in 3 respectively.
Code 2 business object to be injected Content.java

Package com.zj.ioc.di; public class Content {public     void Busniesscontent () {       System.out.println ("does business");    }       public void Anotherbusniesscontent () {       System.out.println ("does another Business");}    }

the Mybusniess class shows a business component whose implementation requires the injection of object content. Code 3, Code 4, code 5,6 demonstrates the construction of sub-injection (Constructor injection), set value injection (Setter injection) and interface injection (Interface injection) three ways.

Code 3 Construction Sub-injection (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 ();    }}

Code4Set Value injection (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 Setting the injection interface Incontent.java

Package Com.zj.ioc.di.iface;import com.zj.ioc.di.Content; Public interface Incontent {    void createcontent (content content);}
Code6Interface Injection (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 an understanding of the most basic concepts of spring, just a small step towards understanding spring, a real understanding, to be put into practice.

Spring Primer-Control inversion (IOC) and Dependency injection (DI)

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.