In the process of working with the Spring Framework, I encountered two concepts : IOC Management Inversion and DI Dependency Injection and I can’t see the difference. It seems that everything is simple, we just use @Authowired or if it is a boot, then @Repository / @Service / @Controller and calculate that Spring will create us instances in the form of Singleton or Prototype. If you wrote a nonsense correct please.

Question: Where is here in this system the border between dependency injection and control inversion (with the same dependency, if I understand correctly). Well, like Spring created instances and manages their life cycle ... Please explain how to correctly understand this separation? IOC and DI.

Thank.

2 answers 2

I came across two concepts of Inversion of Control (IoC) and Dependency Injection (DI) and I can’t see the difference

IoC is the name of the object design principle, according to which the lifecycle of a component occurs outside this component. DI is a special case, one of the possible implementations of the concept of Inversion of Control, used, in particular, in Spring. So from the point of view of Spring, DI and IoC are one and the same . It seems that the compilers of the Spring documentation themselves hardly understand the differences, because sometimes they use the terms DI and IoC as synonyms. Of course, the name IoC is frankly unfortunate, because sometimes it’s not particularly clear what the “inversion” actually is, and the authors of various articles and classifications only add uncertainty, referring to IoC techniques and generating patterns, and DI frameworks, and JNDI.

    A simple explanation (as I understood in my time).

    IOC is the design principle. When one component may depend on the interface of some other component, but not on its implementation. This principle has 2 kinds of patterns:

    DI is a template, when we specify in the interface of our component (through a constructor or a setter), on which it depends.

    There is also a DL (Dependency Lookup) - in this case, only ApplicationContext is passed to us, and we ourselves get the necessary dependencies through ApplicationContext.getBean(...) . In this case, we do not specify in the interface what exactly we will receive.