Java & J2EE The Spring Framework: Understanding IoC |
The first section will focus on the concepts of IoC, including its relationship with Dependency Injection. The focus of the second and third sections will be on the steps necessary to use IoC services provided by the Spring Framework. In the last section, an application will be developed that is based on the concepts explained in first section and whose steps are detailed in the second and third sections. That sets the agenda for this discussion. What is Inversion of Control? Inversion of Control or IoC is one of the techniques used to wire services or components to an application program. By definition, IoC is A software design pattern and set of associated programming techniques in which the flow of control of a system is inverted in comparison to the traditional interaction mode. Simply stated, in IoC, instead of an application calling the framework, it is the framework that calls the components specified by the application. This approach is similar to the one that Hollywood agents adopt with their clients. It is sometimes known as Dont call me, I will call you. This is why IoC is also known as the Hollywood approach. However, IoC is a broad and generic term. The aspect of IoC that the Spring Framework uses is "Injection of required resources or dependency at Run-time into the dependent resource," which is also known as Dependency Injection. Hence, the service provided by the IoC container of Spring is Dependency Injection. Therefore, I will be using the terms IoC and Dependency Injection in a lax way. There are three forms or styles of Dependency Injection. They are:
Of these, the Spring Framework directly supports the first and second forms whereas the third form is supported indirectly. Between the first and the second, the Spring Framework prefers the use of second rather than the first. Here are the details.
That completes the overview of IoC. Now let us see how to use each of these forms in Spring Framework.
blog comments powered by Disqus |