It’s spring, and I’m using it

This entry is about my current experience with the Springframework

I’m currently working on a project, which started as a „tool“ which was supposed to be used only internally for a short period of time. As usual it grew very fast over the time and now it is the core of a much bigger real application.
I started this project using struts in August last year and it contained only a handfull of servlets and jsps. As I had some experience with struts and hibernate I started with that as it lead to quick results and that’s what was needed there.
But then one feature after another came and was build in and I more and more got afraid of the complexety which resulted out of every new feature and I had good fun refactoring with every new feature.
In febuary I decided to give the springframework a try as I have kept on reading about it all the time.

The main advantage I have seen so far, was the ability to wire different technologies together. The worst thing I could imagine was to rewrite all the stuff for another framework, but with spring it wasn’t necessary.
With Spring I could leave the struts part untouched as it was doing fine and there was no need to change.

Why did I want to use spring?
– I wanted to start using velocity, as I have found the struts tags to limited and I wanted to get a bit more logic in the view layer.
– less boilerplate code in the DAOs using Templates
– I liked the idea of the dependency injection pattern and to inject this via an external xml configuration
– I know that there are lots of new requirements comming up which need to be wired together with the current application
– declarative configuration of scheduled jobs in the spring config

My main objective at first was to use spring in the persistence layer together with hibernate. What I really liked was the declarative transaction handling as it is very flexible and also the thread safety I have read about.

First steps
It took me about a day to rewrite all the DAOs to use HibernateDAO templates, which I found to be very risky, as almost all parts of the application was affected. I also had to get used to the way some things have to be done. Some of these things where SQLQueries I used in hibernate. The first big problem was that appearently the transactions did not work and that there was no commit to the database. The problem was, that no changes were written to the DB.
The problem was a configuration problem of the transaction handling but finally I have found a way to set this up correctly.
The first thing which disliked at the first moment was that I had to configure the transaction for every method in my DAOs which was a bit stressfull in the first moment, because I thought there might be a way to say: „I want every method in this DAO to be transactional“. But I had to do it method by method. But now I think this is the better and cleaner way. It’s just a bit of work if you introduce new things.

The second step was to get Velocity to work, which wasn’t hard at all, because you can get all information from the Spring documentation.

The struts part

The best part was, that I could continue to use the old struts part almost without any changes.
The only thing I had to do application-wide was to inject all the DAOs inside the current actions. I had to define the struts actions in the spring-application-context, had to create properties for the DAOs inside each action together with the corresponding setters for the spring dependency injection. After that I had to remove all current dependencies to the DAO s (new OrderDAO() …) This took me some time, because I’ve always overseen some old dependencies.

Since I got everything running I also started using SpringMVC in the web-tier and I like it more than struts as I think it’s much faster and easier to add a new action/servlet to the application.

Scheduled Jobs

Another thing I really like is the support for the Quartz Jobs Framework which I’m using. With Spring you are able to configure the Jobs also in the config XML which saved me lots of work. In my first version the configuration of the cron-trigger happened directly in my classes and I was able to configure only the cron-string in a config file.
Now I’m actually able to even change the trigger from e.g. a simpleTrigger to a cronTrigger without any code changes which I see as a huge benefit.


After all I could say that it took me about 3 days to introduce Spring into a running application and I would say another 2 ays of debugging and testing the things, because you have to get used to some new ways of doing things.
After this 1 week of introducing spring, all new features like configuring velocity, adding new DAOs, adding new actions for SpringMVC is only seconds and the most important thing – it is done in a good and clean way.
It took me one week of extra work but it already save me lots of time introducing new things.
It also had a huge impact on the way I design the application now. I think you really understand good design pricipals only if you start using a other software which is designed and this is the case with spring. It doesn’t force you to do anything, but it offers a lot of things for 90% of the work you have to do anyway but, this in a good way.

Resources that helped me during migration

Guide to Porting From Struts to Spring
Spring-based architectural approach
Webapplikationen mit Spring

Chapter 14. Integrating view technologies
Get a better handle on Struts actions, with Spring

a few more comming soon…

Dieser Beitrag wurde unter Allgemein, Software-Development veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.