Control reversal (IoC) and Dependency injection (DI)

Source: Internet
Author: User

1. Control reversal (inversion of controls) and dependency injection (Dependency injection)

Control inversion, the IOC (inversion of controls), gives the container the invocation of objects traditionally manipulated directly by program code, and implements assembly and management of object components through containers. The so-called "inversion of control" concept is the transfer of control over component objects, from the program code itself to the external container.

The IOC is a very large concept that can be implemented in different ways. There are two main implementations of:<1> dependency lookup (Dependency Lookup): The container provides the callback interface and the context environment to the component. Both EJB and Apache Avalon Use this approach. <2> Dependency Injection (Dependency injection): Components do not make positional queries, only provide common Java methods to allow containers to determine dependencies. The latter is the most popular type of IOC, with interface injection (Interface injection), set value injection (Setter injection) and construction sub Injection (constructor injection) three ways.

Figure 1 Control reversal conceptual structure

Dependency injection is more prevalent because it is a preferable way to allow a container to be solely responsible for dependent queries, and the managed component simply exposes the JavaBean setter method or the constructor or interface with parameters, allowing the container to assemble the object's dependencies at initialization time. The main advantage is that the:<1> lookup location operation is completely unrelated to the application code, compared with the dependent lookup method. <2> does not rely on the container's API to easily use application objects outside of any container. <3> does not require a special interface, and most objects do not have to rely on the container at all.

2. The Hollywood principle

The IOC embodies the Hollywood principle that "don't call, we'll call you". The first time you encounter the Hollywood rule is when you understand the template method (Template Mathod) pattern, the core of the template method pattern is that the base class (the abstract Class) defines the skeleton of the algorithm and delays some steps into the subclass.

Figure 2 Template method pattern class diagram

Now consider the IOC implementation mechanism, where the components define the entire process framework, and some of these business logic implementations need to be joined by other business objects that can participate in the business process in two ways, a dependency lookup (Dependency Lookup), similar to the JDNI implementation, Using Jndi to find the appropriate business object (code 1), 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();
   ds = (DataSource) ctx.lookup(“java:comp/env/dataSourceName”);
   myCollaborator =
(MyCollaborator) ctx.lookup(“java:comp/env/myCollaboratorName”);
   }……

Code 1 Dependency lookup (Dependency lookup) code implementation

The main problem with dependency lookup is that this code must depend on the JNDI environment, so it cannot run outside the application server, and if you want to find resources and collaboration objects in a different way instead of Jndi, you must extract the Jndi code to refactor into a policy method.

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.