Inversion of Control (IoC)

2 min readJan 8, 2022

Inversion of Control is a design principle. Basically, the control is in the framework. It is the approach of outrsourcing the construction and management of objects.
A coding scenario, we want to have a BaseballCoach class and access it from the MyApp class and call the getDailyWorkout() method.

It’s pretty easy to do with our normal knowledge of Java. The first method that comes to mind: is created. Then the getDailyWorkout() method is written and an object is created from the MyApp class and the BaseballCoach class and the method is accessed. However, this approach causes two problems. First of all, when we want to create a different coach class and write the same method, we will have to do the same operations again and this is very costly in large projects. We can define interface to solve this. Defining interfaces in such cases is Software Engineering’s best practice. Now let’s examine this solution by applying it to the code. Now it’s easier to define a new Coach.

So we solved our first problem, in this case we can create the Coach we want, but we need to create an object with new to create this Coach, but the code is still not 100% correct. Because creating an object with “new” is very unhealthy for memory. We may not notice this issue as we are creating two objects now. But in case we create a thousand Coaches, there will be a separate place for each of them in the memory and this will increase according to the size of the project. At the same time, this reduces the configuration of the code. The purpose of the Spring framework is to make the code configurable. Thus, the application is configurable, easily adapting to changes. If we had a configuration file, we would specify the Coach we wanted to create there and our application would become what we wanted. This is where Spring Framework comes into play. Spring’s configuration container: XML Configuration File, Java Annotations, Java Source Code.

Now we will make these configurations in our XML file.

Step 1: Configure Spring Beans in applicationContext.xml

Step 2: Create a Spring Container

Step 3: Retrieve Beans from Container




Computer Engineer, Java, Spring Boot, React, Microservices