20070331 Saturday March 31, 2007

Making your Spring configuration more "Springy"

In case you missed it (I almost did), Springy 0.2 was released a little over a week ago.

Springy is yet another way to configure Spring without XML. This time, the configuration is done with JRuby. I wanted to cook up an example to show it off, but the following code from Springy's own examples is far cooler than anything I could think of:

DAOS = [ :ZoneDAO, :EmailDomainDAO, :DayDAO, :PreferenceDAO,
                 :WhatEverDao... ]
DAOS.each do |dao|
    bean(dao, "daos.hibernate.#{dao}Hibernate")
      {|b| b.new("sonarSession")}
end

What this code does, in case you're wondering, is use Ruby metaprogramming to create several DAO beans all in just a few lines of code.

One of the interesting new features in 0.2 is a "serialize to XML" feature which transforms the Springy JRuby configuration into good old fashioned Spring XML configuration.

It's still pre-1.0, but this 0.2 release of Springy shows tons of promise and is another very interesting alternative to Spring's XML. Definitely something to look into.

(2007-03-31 18:17:44.0) Permalink Comments [5]
Comments:

FWIW, here's the equivalent in <a href="http://code.google.com/p/google-guice/">Guice</a>:

Class[] daos = { ZoneDao.class, EmailDomainDao.class, PreferenceDao.class... };
for (Class dao : daos)
bind(dao).to(Class.forName("daos.hibernate.Hibernate" + dao.getSimpleName()));

Posted by Bob Lee on April 01, 2007 at 07:43 PM EDT #

No question about it...once you break out of XML, it's dead easy to do this kind of stuff--whether it's with Springy, Guice, Spring JavaConfig, or Grails' Groovy Spring builder.

I still haven't made the jump away from XML, but this kind of configuration trickery sure makes it tempting.

Posted by Craig Walls on April 01, 2007 at 09:47 PM EDT #

How would you do this with Spring JavaConfig?

Posted by Bob Lee on April 01, 2007 at 10:14 PM EDT #

Oh my God. It looks even worse. The XML look clearer to me

Posted by Mateusz on April 02, 2007 at 01:54 AM EDT #

Obviously an April Fools joke. Good one guys!

Posted by David Tatum on April 02, 2007 at 02:04 PM EDT #

Post a Comment:

Comments are closed for this entry.