Thursday, January 7, 2016

IoC - Inversion of Control

Main idea of Inversion of Control concept :


  • component dependencies
  • lifecycle events
and
  • configuration

reside outside of the component itself (In Spring : In the Framework).



Though it is like giving up too much control,  it can make your code :

  • more manageable
  • more testable
  • more portable

Best practice patterns of IoC Framework :


  • Aspects of Spring IoC    -     resemble and include Factory and Observer pattens
  • Implementation of the Framework    -    Dependency Injection pattern


Spring IoC Container 



  • Core of Spring Framework.
  • Spring's IoC container manages the objects (beans) that it is configured to instantiate through its factory, from instantiation to destruction, manages a bean's scope, lifecycle events, and any AOP features for which it has been configured and coded.
  • Spring's management of the container objects adds flexibility and control to your application, and provides a central place of configuration management for your POJOs. 
  • For example, through Spring IoC you can configure the number of instances of the component, whether the component is a singleton or not, and at what point the component is created and destroyed in memory. 
  • In Spring, the initialization of a bean by the framework is exactly equivalent to using the new keyword to instantiate an object in Java code. Once the framework has instantiated the object, it manages the scope of the bean, based on its configuration. 
  • Because the IoC container is managing the beans, JNDI lookups that are typical in Java EE containers are no longer required, leaving your code container-agnostic and easier to unit test both inside and outside of the framework. And while you are coding to interfaces as part of good OO practice, 
  • Spring allows you to manage what implementations are used by leveraging dependency injection, resulting in cleaner, decoupled components. 
  • The IoC container can also be configured to receive instantiation and destruction callback events for a particular bean. Certain components such as a database connection pool obviously need to be initialized and destroyed when the application is shutdown. Instead of using your custom code, Spring can manage these lifecycle events.
  • The IoC container enforces the dependency injection pattern for your components, leaving them loosely coupled and allowing you to code to abstractions. 







The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM programming language. It is defined to be independent of any specific directory service implementation.




s inform Spring through an XML configuration file that the recorder bean is implemented by the LocalVoteRecorder class :-

<bean id="recorder" class="com.vij.nondip.LocalVoteRecorder" />

map the recorder bean to the VotingBooth bean by setter injection in that beans definition :-

<bean id="votingBooth" class="com.vij.nondip.VotingBooth">
         <property name="voteRecorder" ref="recorder" />
</bean>

with this configuration and the Spring framework, and through dependency injection, we have finally removed the low-level component dependencies and have achieved true dependency inversion



No comments:

Post a Comment