top of page

Inversion of control - SpringBoot help

What is Inversion of control?

The approach of outsourcing the constructions and management of objects is known as Inversion of control


So for some real motivation let's work on the idea of Inversion of control(IOC)


Scenario:

Suppose There is a boy Ravi who wants to learn some outdoor games and he has hired some personal coaches and these coaches gives you the daily workout like Basketball coach gives you some workout so that you can work on your basketball playing skills ,Tennis coach gives you some workout so that you can work on your tennis skills like these cases there are some other coaches also.


How we can represent above scenario in Java programming?

We will create three classes and one interface

  1. MySelf.java It will be our main class this class will represent Ravi(Who will get the daily workout from a coach)

  2. BaseballCoach.java This class will represent the basketball coach

  3. Coach.java This is an interface so that the similar behaviors of all the coaches can be implemented directly

  4. TennisCoach.java This class will represent the tennis coach

Program :-


MySelf.java

public class MySelf {

    public static void main(String[] args) {
        
        BaseBallCoach baseBallCoach= new BaseBallCoach();

        System.out.println(baseBallCoach.getDailyWorkOut());

        TennisCoach tennisCoach = new TennisCoach();

        System.out.println(tennisCoach.getDailyWorkOut());

    }
}

Coach.java

public interface Coach {
    public String getDailyWorkOut();
}

TennisCoach.java

public class TennisCoach implements Coach {

    @Override
    public String getDailyWorkOut() {
        return "Run a hard 5K";
    }


}

BaseBallCoach.java

public class BaseBallCoach implements Coach{

    @Override
    public String getDailyWorkOut(){
        return "Spend 30 minutes in jumping jacks practice";
    }
    
}

Output:

Spend 30 minutes in jumping jacks practice
Run a hard 5K

So that's how we can implement our scenario in programming


Now suppose that someone says the program should be configurable i.e It should be easy for us to plugin different different coaches according to the game like for Hockey there should be a hockey coach for cricket there should be a cricket coach


How we will be able do that?

So here comes the idea of Inversion of control(IOC)

To see the Inversion of control(IOC) in action we first have to setup the environment with spring


Environment setup:

  1. Download the Spring framework click here

  2. Unzip the downloaded Spring framework

  3. Go to project structure in the current project

  4. Click on libraries and then '+' icon

  5. Pass the lib folder from the unzipped folder

  6. And hit enter

Create a file named applicationContext.xml and then past this XML code in the applicationContext.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Define your beans here -->


    <bean id="myCoach"
          class="com.company.TennisCoach"></bean>

</beans>

copy this file and paste it to the src folder and then make changes to the MySelf.java file

delete all the code inside the MySelf.java and add the below written code inside the MySelf.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MySelf {

    public static void main(String[] args) {
        //Load the spring configuration file
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Retrieve bean from spring container
        Coach coach= context.getBean(TennisCoach.class,"myCoach");
        //Call methods on the bean
        System.out.println(coach.getDailyWorkOut());
        //Close the context
        context.close();

    }
}

So applicationContext.xml is our config file and inside that we have created a bean and using the MySelf.java class we are first loading the config file and then retrieving a bean with ID "myCoach" and then we are instantiating the TennisCoach class after instantiating

we are calling the methods of TennisCoach class and printing the output


Now our program has become configurable without changing the source code we can easily change the coach using xml config file and that is the Inversion of control(IOC)


We have outsourced the process of constructing and managing the objects and that is known as the Inversion of control(IOC).


How does CodersArts helps you ?

CodersArts provide :

  • Spring boot assignment Help

  • Help in development Projects

  • Mentorship from Experts Live 1:1 session

  • Course and Project completions

  • CourseWork help



bottom of page