What Is Inversion of Control
Inversion of Control or IoC is one of the technique used wire services or components to an application program. By definition, IOC is 'A software design pattern and set of associated programming technique is which the flow of control of a system is inverted in comparison to the traditional interaction mode '. Simply stated, in IoC , instead of an application calling the framework, it is the framework that calls the components specified by the application.
This approach is similar to the one that Hollywood agent adopt with their client. It is some time known as “Don't call me, I will Call you” . This is why IoC is juga know as The Hollywood approach.
However IoC, is a broad and generic term. The aspect of IoC that the spring Framework uses is “Injection of required resources or Dependency at Run-time into the Dependent resource”, which is also known as Dependency Injection (DI). Hence the service provided by the IoC container of spring is Dependency Injection. Therefore, I will be using the terms IoC and Dependency Injection in a lax way.
There are three forms of styles of Dependency Injection. They are :
-
Constructor Injection
-
Setter Injection
-
Interface Injection
Of these the spring framework directly support the first and second forms whereas the third form is supported indirectly. Between the first and the second, the Spring Framework preference the use of second rather than the first, Here are the details.
-
Constructor Injection : in Constructor Injection, an IoC container uses the constructor to inject the dependency. All the dependencies, whether they are simple value or reference to other objects, are declared in the constructor. One of the advantages of constructor injection is that all the dependencies are declared in one go. This also helps in understanding whether the class depends on too many service.
-
Setter Injection : This form the dependency injection uses setters, also know as mutators(because they change the value of the corresponding instance variable), to inject the required resources or dependencies. In other words, each of the object that the class depends upon will have a setter and the IoC container will use the setter to provide the resource at run-time.
-
Interface Injection : Interface Injection provides the concrete implementations of an interface to the dependent object according to the configuration. The main deference between interface injection and the previous two is the in Interface Injection, any of the implementation of the interface can be injected, whereas with the order two, the object of the classic specified is injected. Spring does not provide direct support for interface Injection.
blog comments powered by Disqus