Control inversion (IOC) and dependency injection (DI)

Source: Internet
Author: User

Control inversion (IOC) and dependency injection (DI) 22:57:47

Tags: dependency injection workplace leisure control reversing original works, allowing reprinting, please be sure to mark the reprinting in hyperlink form Article Original source, author information, and this statement. Otherwise, legal liability will be held. Http://zhangjunhd.blog.51cto.com/113473/126530 1. Control reversal (Inversion of control) And dependency Injection (Dependency injection) IOC (inversion of control) Program Code Call permissions of directly controlled objects are handed over to containers to implement Assembly and Management of object components through containers. 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 ): Container Provide the callback interface and context Components . 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. 650) This. width = 650; ". Click =" window. Open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. SRC)" http: = "" blog.51cto.com = "" viewpic. php? Refimg = "+ this. SRC) "src =" http://zhangjunhd.blog.51cto.com/attachment/200901/200901141231944972691.jpg "alt =" "border =" 0 "> Figure 1 controls reverse conceptual structure dependency injection because it is more popular: the container is solely responsible for dependent queries. The managed component only needs to expose the setter method of the JavaBean or the constructor or interface with parameters so that the container can be assembled during initialization. Object . Compared with the dependency search method, it has the following advantages: <1> the search and location operations are irrelevant to 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 Algorithm And delay some steps to the subclass. 650) This. width = 650; ". Click =" window. Open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. SRC)" http: = "" blog.51cto.com = "" viewpic. php? Refimg = "+ this. SRC) "src =" http://zhangjunhd.blog.51cto.com/attachment/200901/200901141231944986707.jpg "alt =" "border =" 0 "> Figure 2 template method pattern class diagram now consider the implementation mechanism of IOC, the component defines the entire process framework, some of the business logic must be implemented by adding other business objects. They can be involved in the business process in two ways. One is dependency lookup ), similar to the implementation of jdni, the corresponding business object (Code 1) is found through JNDI, and the dependency injection is used to inject business objects into components through the IOC container. 3. Dependency search ( 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/datasourcename"); mycollaborator = (mycollaborator) CTX. lookup ("Java: COMP/ENV/mycollaboratorname ");}......
Code 1 Dependency lookup the main problem with code 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 in other ways to find resources and collaboration objects, you must extract the JNDI code and reconstruct it into a policy method. 4. Dependency injection ( 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
PackageCom. ZJ. IOC. Di;Public ClassContent {Public VoidBusniesscontent () {system.Out. Println ("do business ");}Public VoidAnotherbusniesscontent () {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
PackageCom. ZJ. IOC. Di. ctor;ImportCom. ZJ. IOC. Di. content;Public ClassMybusiness {PrivateContent mycontent;PublicMybusiness (content) {mycontent = content ;}Public VoidDobusiness () {mycontent. busniesscontent ();}Public VoidDoanotherbusiness () {mycontent. anotherbusniesscontent ();}}
Code 4 setter injection mybusiness. Java
PackageCom. ZJ. IOC. Di. Set;ImportCom. ZJ. IOC. Di. content;Public ClassMybusiness {PrivateContent mycontent;Public VoidSetcontent (content) {mycontent = content ;}Public VoidDobusiness () {mycontent. busniesscontent ();}Public VoidDoanotherbusiness () {mycontent. anotherbusniesscontent ();}}
Code 5 sets the incontent. Java Injection Interface
PackageCom. ZJ. IOC. Di. iface;ImportCom. ZJ. IOC. Di. content;Public InterfaceIncontent {VoidCreatecontent (content );}
Code 6 interface injection mybusiness. Java
PackageCom. ZJ. IOC. Di. iface;ImportCom. ZJ. IOC. Di. content;Public ClassMybusinessImplementsIncontent {PrivateContent mycontent;Public VoidCreatecontent (content) {mycontent = content ;}Public VoidDobusniess () {mycontent. busniesscontent ();}Public VoidDoanotherbusniess () {mycontent. anotherbusniesscontent ();}}
5. Drag dependency (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 factorybeanfactory 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 a specific location.

This article is from the "sub-blog", please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/126530

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.