Skip to main content

[Spring] Spring Inversion of Control(IoC) through XML Configuration


Inversion of Control is one of the most well-known characteristics of the Spring framework. But what does the term exactly mean?


Inversion of Control

Inversion of Control literally means inverting the control structure of a program compared to the traditional procedural design. This sounds very vague, doesn't it?

Nowadays with the vast popularity of frameworks, the term is mainly used to talk about externalizing the construction and the management of objects, thus inverting control of instantiation of objects. 

There are some controversies about whether the term represents the correct notion, if interested you might want to read this article from Martin Fowler - it explains Inversion of Control and Dependency Injection in detail. 


Anyways, so what's all good about inverting control of instantiation of objects?


An important point here is that with Inversion of Control, the execution of a specific task is decoupled from the implementation, thus increasing the modularity and extensibility of the program. Simply put, less code, no need to change the existing code, and thus less error prone.

This is where Spring Container (application context) comes in handy. Through the object factory in Spring Container, we can have the application talk to Spring to request an object, based on a configuration file, and Spring calls into the appropriate implementation, thus making the application configurable. 


There are three ways of configuring a spring container:

  • XML configuration file
  • Java annotation
  • Java source code

Although configuration through XML file is the oldest and not often used nowadays, a lot of the legacy apps are still configured this way.



Spring IoC through XML Configuration 

To make use of the Spring Container (application context), we first need an interface. Below, I demonstrate this step by step using very simple examples.

Let's say we are making a program for saying Hello World in various languages. We first create an interface HelloWorld.java in the package.



We also create two concrete classes implementing the interface - HelloEnglish.java and HelloDutch.java.




Next, we create an xml file for configuration. This is done by creating the file in the source of the package. We put it in the application's class path to make the class path the application context.


Now we create the main app where the appropriate implementation will be called. One thing to keep in mind is that when retrieving a bean from the spring container, be sure to mention the interface class that the bean implements.


And below is the result after running the program.



That was it! This is how we make use of Inversion of Control using XML configuration file. 




Comments

Popular posts from this blog

IntelliJ IDEA Error: Cannot determine path to 'tools.jar' library for 17

[Spring] Spring MVC Configuration - How to set up Spring MVC in IntelliJ