Inversion of control and dependency Injection)

Source: Internet
Author: User

-- Excerpt from Rocky Ren's note

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

Dependency injection is more popular because it is a more desirable method: the container is solely responsible for dependency queries. The managed component only needs to expose the setter method of the JavaBean or a constructor or interface with parameters, this allows containers to assemble object dependencies during initialization. Compared with the dependency search method, this method has the following advantages: <1> the search and positioning operation has nothing to do with the application code. <2> container-independent APIs allow you to easily use application objects outside any container. <3> no special interfaces are required. Most objects do not need to depend on containers at all.

 

2. Hollywood principles

IOC embodies the Hollywood principle: "Don't call us, we will call you ". The first time I met the Hollywood principle was to understand the template method (template mathod) mode, the core of the template method mode was that the base class (abstract class) defined the skeleton of the algorithm, and delay some steps to the subclass.

 

Figure 2 template method pattern class diagram

 

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;

 

Public mybusnissobject (){

Context CTX = NULL;

Try {

CTX = new initialcontext ();

DS = (datasource) CTX. Lookup ("Java: COMP/ENV/CENAME ");

Mycollaborator =

(Mycollaborator) CTX. Lookup ("Java: COMP/ENV/mycollaboratorname ");

}......

Code 1: dependency lookup code implementation

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 ){

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 ){

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 );

}

 

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 ){

Mycontent = content;

}

Public void dobusniess (){

Mycontent. busniesscontent ();

}

Public void doanotherbusniess (){

Mycontent. anotherbusniesscontent ();

}

}

 

5. Dependency drag (dependency pull)

Finally, we need to introduce how to interact with components by dependency drag and drop.

Code 7 Dependency dragging example

Public static void main (string [] ARGs) throws exception {

// Get the Bean Factory

Beanfactory factory = getbeanfactory ();

Messagerender mr = (messagerender) Factory. getbean ("Renderer ");

Mr. Render ();

}

Generally, the configuration of the injected object can be completed through an XML file.

In this way, objects are centrally managed. The essential difference between dependency drag and drop and dependency search is that dependency search is performed in the Business Component code, rather than from a centralized registry, execute at specific locations

 

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.