Inversion of Control (IoC) and Dependency Injection

Dheeraj Kumar
2 min readMar 21, 2021

Inversion of Control (IOC)

Inversion of control is a common characteristic of frameworks. As the name suggests it inverts something. So, What aspect of control it is inverting?

Early user interfaces were controlled by the application program. You can see commands like “Enter name”, “Enter address”; Your program would drive the prompts and pick up a response to each one. The main control of the program was inverted, moved away from you to the framework.

The inversion is about how we lookup a plugin implementation. The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the class.

Inversion of Control is a key part of what makes a framework different to a library. EJBs are a good example of inversion of control. When we implement various methods of EJB container at various lifecycle points. Example: The session Bean interface defines ejbRemove, ejbPassivate and ejbActivate. You don’t get to control when these mthods are called, just what they do. The container calls us, we don’t call it.

Dependency Injection

The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in a specific class with an appropriate implementation for the particular interface.

Types of Dependency Injection:-

  1. Constructor Injection:- uses a constructor to decide how to inject a finder implementation into the lister class.

Example:-

class MovieLister…

public MovieLister(MovieFinder finder) {
this.finder = finder;
}

class ColonMovieFinder…

public ColonMovieFinder(String filename) {
this.filename = filename;
}

The implementation container then needs to be told which implementation class to associate with each interface, and which string to inject into the finder.

private MutablePicoContainer configureContainer() {
MutablePicoContainer pico = new DefaultPicoContainer();
Parameter[] finderParams = {new ConstantParameter("movies1.txt")};
pico.registerComponentImplementation(MovieFinder.class, ColonMovieFinder.class, finderParams);
pico.registerComponentImplementation(MovieLister.class);
return pico;
}

To use the container you write code something like this.

public void testWithPico() {
MutablePicoContainer pico = configureContainer();
MovieLister lister = (MovieLister) pico.getComponentInstance(MovieLister.class);
Movie[] movies = lister.moviesDirectedBy("S. S. Rajamouli");
assertEquals("Bahubali", movies[0].getTitle());
}

.2. Setter Injection

It is used in setter method and more preferred by developers.

Example:-

To get my movie lister to accept the injection I define a setting method for that service

class MovieLister…

private MovieFinder finder;
public void setFinder(MovieFinder finder) {
this.finder = finder;
}

Similarly I define a setter for the filename.

class ColonMovieFinder…

public void setFilename(String filename) {
this.filename = filename;
}

The third step is to set up the configuration for the files.

<beans>
<bean id="MovieLister" class="spring.MovieLister">
<property name="finder">
<ref local="MovieFinder"/>
</property>
</bean>
<bean id="MovieFinder" class="spring.ColonMovieFinder">
<property name="filename">
<value>movies1.txt</value>
</property>
</bean>
</beans>

The test then looks like this.

public void testWithSpring() throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("spring.xml");
MovieLister lister = (MovieLister) ctx.getBean("MovieLister");
Movie[] movies = lister.moviesDirectedBy("S. S. Rajamouli");
assertEquals("Bahubali", movies[0].getTitle());
}

Conclusion:-

So, basically IoC means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source. Where as Dependency Injection means that this is done without the object intervention, usually by a framework component that passes constructor parameter and set properties.

--

--