INVERSION OF CONTROL AND DEPENDENCY INJECTION
Neha Rajesh
3 min read
Introduction to Inversion of Control
Java applications, from applets to n-tier server side enterprise applications, typically consist of objects that collaborate to form the application. The objects in an application have dependencies on each other. To support this, the Java platform provides extended application development functionality. But, it lacks the means to organize the basic building blocks of an application to form a whole, leaving this task to architects and developers.
You can use design patters such as Factory, Abstract Factory, Builder, Decorator and Service Locator to constitute the various classes and object instances that make up an application. However, these patterns are just formalized best practices which are given a name, with a description of what the pattern is, where to apply it, the problems it addresses, etc., that you must implement yourself.
The Spring Framework Inversion of Control however, addresses this concern by providing a formalized means of composing separate components into a fully working application ready for use.
The Spring Framework codifies formalized design patterns as first-class objects that you can integrate into your own applications. In this manner, Spring Framework can be used to engineer robust and maintainable applications.
What is Inversion of Control?
Inversion of control is a principle in software engineering by which the control of objects or portions of programs is transferred to a container or a framework. It is mostly used in object oriented programming. Inversion of control enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behaviour built in. If we want to add our own behaviour, we need to extend the classes of the framework or plugin our own classes.
What is Dependency Injection?
One way in which we can implement ioc is by using dependency injection. Dependency Injection is a pattern where the control being inverted is the setting of object's dependencies. The act of connecting objects with other objects or "injecting" objects into other objects is done by an assembler rather that by the objects themselves.
Advantages of this type of architecture :