Tuesday, August 30, 2005

MVC using Spring

I started exploring the Spring Application framework yesterday and am very impressed by it. Not only does it let me create much simpler J2EE applications but it also decouples components pretty neatly. For this it uses the principle of inversion of control. I plan to use this excursion of mine to learn come concepts like inversion of control, learn frequently used features of Spring (like using a database and remoting) and brush up my newbie skills of JSTL etc.

Some ideas of Spring which really impressed me:
  • Code should be easy to test. Because we develop Spring applications with JavaBeans, testing is cheap. There is no J2EE container to be started since we will be testing a POJO. The application context can be loaded outside of a web server environment using a class that will load an application context. There are several available, and for the current task I used the FileSystemXmlApplicationContext. This loading of the context is done from directives on XML prperty files. This itself wins me over. The simplicity of testing like this cannot be overstated. Of course it does have its disadvantages but for individual POJO testing which are deployed inside an application server context I think this implementation is hard to beat.

  • JavaBeans loosely coupled through interfaces is a good model. With Spring, your beans depend on collaborators through interfaces. Since there are no implementation-specific dependencies, Spring applications are very decoupled, testable, and easier to maintain. And because the Spring container is responsible for resolving the dependencies, the active service lookup that is involved in EJB is now out of the picture and the cost of programming to interfaces is minimized. All you need to do is create classes that communicate with each other through interfaces, and Spring takes care of the rest.

At some point I plan to use Hibernate as the persistence engine in place of the default HSQL.

For newbies to Spring I would suggest two resources:
The excellent bootstap guide to Spring.
The book: Spring in Action

Disclaimer: I am experimenting with MVC implementation technologies and Spring is new to me.

0 Comments:

Post a Comment

<< Home