Why and When to use Spring Framework : Part 1

September 23, 2008

It is really a tough deal for a new programmer to figure our why should he/she use a framework like Spring. Developer or programmers who worked with single tire framework like Struts or Hibernate may find it’s usability easier than everyone else. At the beginning of J2EE applications 1999/2000 (I was not even start my graduate school) it brings out a new fresh wave of in web based application through core middle-tire concepts. Though those applications takes much afford to develop and more complexity on overall process. Since new problems and challenges rushed to us day by day. J2EE and other web applications shows up with some crucial problems like followings:

J2EE applications usually carries excessive amounts of protocol code: Try/catch blocks for JDBC, or transfer objects requires a lot of bulk code that is totally unnecessary compare to business logic but mandatory for J2EE structure.
J2EE applications often use distributed object model: Distributed object model(a remote object whose methods can be invoked from another JVM, potentially on a different host) creates extra codes where most of those are simply duplication. Distributed application is much complicated than the co-located(same place) one.
Unit test in J2EE applications are tough The basic design of J2EE didn’t consider unit testing. As a result EJB and most of J2EE APIs are hard to test out side of server.
Over use of EJB: EJB was specifically designed for internally distributed and transactional applications. But it was used in many place where it was not appropriate.

It is proven by experience a framework is much flexible than traditional tool-enabled code generation. Configuring the behavior of simple piece of code much easier by using a framework. Many developers attempt to write a framework for these reasons. Spring comes up with solutions of many flexible facilities. Spring framework contains many features that help to commit a web application in a easier way. Here are some core features of Spring:

Inversion of Control (IoC): Most probably the best feature of this framework. Advertise the example of Hollywood principle of “Don’t call me, I will call you”. In traditional way of class library, Application code is responsible for total work flow. As a result calling out the class library is necessary. But by Inversion of Control framework code invokes application code, coordinate overall work flow.

Dependency Injection: A kind of IoC that maintains push configuration. At the runtime, the dependencies are pushed into application object. Thats why Dependency Injection objects never need to load custom properties or search a database to load configuration. The framework does the whole thing.

Aspect Oriented Programming (AOP): AOP separates crosscutting concerns into single units or aspects.

By: Md. Shahjalal

References:
1. http://www.wrox.com
2. Expert Spring MVC and Web Flow
3. Java Loby


HandlerAdapter of Spring Framework

June 16, 2008

One of the most exiting feature of the Spring Framework is allowing to integrate a third party framework with Spring MVC. Though most of the developer working with spring do not use because most of the application consists of only Controllers which is configured by default and no explicit configuration is required. To integrate with a system developed by other framework, this an exciting tool. HandlerAdapter (org.springframework.web.servlet.HandlerAdapter) is a system level interface that enables low coupling between different request handlers and the DispatcherServlet. The interface is like the following:


package org.springframework.web.servlet;
public interface HandlerAdapter {
    boolean supports(Object handler);
    ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception;
    long getLastModified(HttpServletRequest request, Object handler);
}

Here is a sample of implementation of HandlerAdapter Interface named SampleFrameworkHandlerAdapter. DispatcherServlet first call support() to check the HandleAdapter support a Handler, if yes then go to handle method for model and view

public class SampleFrameworkHandlerAdapter implements HandlerAdapter {
    public boolean supports(Object handler) {
        return (handler != null) && (handler instanceof ExoticFramework);
}
    public ModelAndView handle(HttpServletRequest req, HttpServletResponse res,
        Object handler) throws Exception {
        ExoticResult result = ((ExoticFramework)handler).executeRequest(req, res);
        return adaptResult(result);
}
    private ModelAndView adaptResult(ExoticResult result) {
        ModelAndView mav = new ModelAndView();
        mav.getModel().putAll(result.getObjectsToRender());
        return mav;
    }
    public long getLastModified(HttpServletRequest req, Object handler) {
        return -1;
    }
}

You have to make the configuration in the ApplicationContext by a bean with showing full class path. DispatcherServlet looks at the ApplicationContext by default for all HandlerAdapter and find all by their type. But you must notice that when you specify a HandlerAdapter, the SimpleControllerHandlerAdapter (the default one) will not be used, you have to specify all the HandlerAdapter explicitly.

By, Md. Shahjalal
Source: Expert Spring MVC and Web Flow


Follow

Get every new post delivered to your Inbox.