October 6
how i used Spring at ma’ workplace - Part 1 - Soft Introduction
Posted by AjayI’ve been relearning some of the base technologies we use in our project ( there are a lot fo them) but the most interesting one is Spring. I am sure all of u know what Spring is and how it works. Let me just talk about one interesting aspect of spring that we actually use in our project - dependency injection or in technical terms, the Spring IOC Container.
let’s talk about Inversion of Control first. It’s very popularly known as the “Hollywood principle” because of it’s underlying concept which is “you dont call me, I’ll call you“. These are the important principles behind the Hollywood Principle
- loose coupling - imagine a slice of bread. A slice of bread is loosely coupled because in itself, it is just bread. People buy bread as it is. But then the coolness is when bread gets converted to a vegetable sandwich or bread becomes bread omelet sandwich. That, my dear watson, is called loose coupling. There is a very minimal assumption about the interface of bread. If you decide to make a wheat bread instead of white bread, you can still make a vegetable sandwich or a bread omelet sandwich. Similarly we write a Java object that is independent by itself and has no dependencies. It is exposed through an interface. Any changes in the object will not have an effect on the classes that use this object.
- sacrifice of control - let the restaurant chef decide what to put in the bread rather than letting the bread maker dictates terms as to what should a bread contain. Similarly we let the system define how it wants to use the object and what it wants to add to the object.
So Inversion of Control is a broad term that encompasses the Hollywood Principle. It is a general principle of most frameworks. Simply stated the framework takes the responsibility of invoking a module’s functionality away fromt he module and puts it in the controlling framework itself. There are many different example of IOC in action. EJBs, JUnit uses IOC , dependency injection in Spring, etc.
What is the difference between Inversion of Control and Dependency Injection?
How about you wait for ma next post
??
References: