Recognize IOC

What is IOC

IOC is the abbreviation of Inversion of Control, and most books are translated as "Inversion of Control". Inversion of control means that the right of control is reversed: originally controlled by the object itself, it is now controlled by the container. (Previously, when creating an object, it was actively created by the new method, but because the controller was inverted to the container, when an object needs another object, it does not need to actively create the object by going to new, but passively wait The container creates objects for it) This is also called the Hollywood principle: Hollywood principle: Don’t call me (director), I will call you (actor)
where "I" refers to resources, and you are the application system (Module) In 1983, Richard E. Sweet put forward the "Hollywood Principle" (Hollywood Principle)

and DI (Dependency Injection) in "The Mesa Programming Environment" . In 2004, Martin Fowler discussed the same issue. Since IOC is an inversion of control, what aspects of control have been inverted? "The process of obtaining dependent objects is reversed." After the control is reversed, the process of obtaining dependent objects changes from self-management to active injection by the IOC container.

Dependency injection (DI) and inversion of control (IOC) are the same thing described from different perspectives, that is, through the introduction of IOC containers, the use of dependency injection to achieve decoupling between objects.

IoC main implementation strategy

  • Using a service locator pattern 服务定位模式( a pattern defined by JavaEE to obtain DataSource these things)
  • Using dependency injection,for example 依赖注入
    • Constructor injection 构造器注入
    • parameter injection 参数注入
    • Setter injection set注入
    • Interface injection 接口注入
  • Using a contextualized lookup 上下文依赖查询(implemented by JavaBeans)
  • Using template method design pattern 模版方法模式
  • Using strategy design pattern 策略模式

In fact, it is mainly through two categories, the main ones are:
Dependency lookup: The container provides a callback interface and context conditions for the component. Both EJB and Apache Avalon use this approach. In this way, the component must use the API provided by the container to find resources and collaborating objects. The only inversion of control is only reflected in those callback methods: the container will call these callback methods so that the application code can obtain the relevant resources.
Dependency injection: The component does not perform positioning queries, but only provides ordinary Java methods for the container to determine dependencies. The container is solely responsible for the assembly of components, and it will pass the objects that meet the dependencies to the required objects through the JavaBean properties or constructors. The method of injecting dependencies through JavaBean properties is called Setter Injection; the method of passing dependencies as constructor parameters is called Constructor Injection.
From: Baidu Encyclopedia

Responsibilities of the IoC container

  • Dependency processing
    • Dependent lookup
    • Dependency injection
  • Life cycle management
    • container
    • Hosted data
  • Configuration
    • container
    • External placement
    • Hosted data

Guess you like

Origin blog.csdn.net/qq_45422703/article/details/109021777