Wednesday, August 29, 2007

Spring IoC - A Quick look

Spring's IoC is one major feature of Spring framework. Martin Fowler has beautifully made clear and explained what IoC is about. You can find his article on Ioc here.

Spring configuration file as XML describes how to configure the classes. Who is dependent on who and how to give them what they want. As per Martin Fowler's article, instead of MovieLister picking itself the specific implementation of a MovieLister, we configure Spring to inject this MovieFinder implementation into MovieLister. Thus MovieLister can act as a plugin with no dependencies within itself.

How to inject them ? Three ways exists as mentioned in the article

1. Type 1 - Interface injection
2. Type 2 - Setter injection
3. Type 3- Constructor injection

Spring supports both type 2 and type 3 injection. Constructor injection is favoured when working with Spring and it is simple of all for those who have been regularly working with JavaBeans specification.


Dependency Injection is opposite to Service Locator. When to choose between them depends on how you want your services to be utilised. For simple services which are not used much outside the application or the specific module, then simple Service Locator will suit. But if there exists a functionality which needs to be executed in simulated mode and production mode, you can very well use dependency injection. The simualted mode will have unit test cases which can be changed over and over and provides a clean testing of the functionality without any regression coming out of touching your actual application code.

Judgement is the Key. Shouldn't get carried away. Numerous solution exists for every problem but what suits ? This requires better understanding of the application.

Lets see how spring.xml would be for the moviefinder and movielister example





Spring - Part I

Disclaimer : This post starts with a different intent of putting "my" thoughts / understanding of Spring. This doesn't target any section of people who work with Spring or trying to learn Spring. Infact, I start with lot of questions as to why Spring and what makes it so unique in J2EE world.

Spring is an open-source framework created by Rod Johnson and it addresses the major complexities involved in developing enterprise application , especially when EJB is involved. At this junction to say simple benefit of Spring is that it is POJO (Plain Old Java Objects) based.

Spring is lightweight inversion of control and Aspect Oriented framework.

Inversion of Control
When IoC is applied, objects get their dependencies from the Spring container rather than the objects looking themselves for the dependencies. Reverse JNDI to say in simple terms.

Inversion of Control is a loose term. It is interchangeably used with dependency injection. Dependency injection aptly says what IoC is all about.

Aspect Oriented
Business logic are mostly in verticals. An enterprise would have core businesses align as verticals with dependencies among each other. OOP is a perfect paradigm for such business problems where reusability , maintenance , scalability are key. In each business function, we could find certain repetitive functionalities which are not 100% related to that business function. Rather they appear scattered across all business modules. Typically they are horizontal functions or simply cross-cutting concerns. OOP is best for vertical functions.
AOP solves the problem by giving aspects such as logging, transaction management, authentication to the business methods. Thereby the code is cleaner, easily testable and maintainable.

A cliche example of aspect : Logging. If you have already known this example, excuse me for not giving a different example. But to make someone understand what AOP, logging is the best feature to explain. Loggers typically appear at will. Typically at the start and end of the method. Why dirt the code with logger statement when it has cleanly done what it is expected to do. Logging is an aspect and you can ask Spring to do the task of giving the advice and when to give the advice.

Spring framework is made of many modules. It is not necessary to use all the modules to be able to completely work with spring. Thats what makes Spring unique. Struts is good but there is a tight coupling when it comes to integration with other frameworks. Spring allows easy integration for other frameworks. If you want to either TopLink, iBatis, Hibernate as your ORM quite easily with Spring. Ok.! Lets see what are the Spring modules

All modules are built on top of the container which is the heart of Spring. A container is J2EE context is that provides the low-level services which are essentially the building blocks of enterprise application.

  1. Spring Container : Fundamental of Spring. Spring's BeanFactory is the heart of Spring based application. BeanFactory provides the IoC.

  2. Application Context module : Extends the container module to provide internalization support, EJB integration, life cycle management. (Not very clear. Superficial now)

  3. Spring AOP module : As discussed earlier, this makes Spring an AO framework. Spring AOP API confirms with the AOP Alliance API specification and hence can support interoperability of AOP implementations

  4. JDBC abstraction and DAO module : Imagine flyweight pattern. You can bury complex database calls, connection establihsment under a set of classes and expose them to your business calls. Declarative transaction management possible through this module which inturn depends on AOP module. Meaningful exceptions are available and thus you needn't decipher the SQL Error code to track what the exception is and why it occured.

  5. ORM integration module : Integrates well with any ORM framework. It provides hooks to major ORM frameworks like Hibernate, JDO, iBatis

  6. Spring Web Module : Allows easy intergration with other MVC frameworks like Struts. It is built on top of application context module and very much into request-response management, programmatic binding of request parameters to business objects

  7. Spring MVC framework : Spring also gives a separate MVC framework eventhough it supports good integration with Struts. Importantly this uses IoC to segreggate neatly the role of controller from business objects. Multiple view possible using Spring MVC