First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-219  评论-1047  文章-0  trackbacks-0
An interesting post by Craig Walls that I hadn't noticed before shows how you can create Spring configs with a little JRuby DSL:

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

The Groovy version with Grails' SpringBuilder would look like:

def DAOS = [ZoneDAO, EmailDomainDAO, DayDAO, PreferenceDAO, WhateverDAO]
DAOs.each { dao ->
"${dao}"("daos.hibernate.${dao.simpleName}Hibernate") {
sessionFactory = ref("sonarSession")

Another important difference between the two is that Springy, the JRuby version, serializes the JRuby code into XML and then reads the beans from that. We used to do this in Grails, but it had serious performance implications for load time, Grails'BeanBuilder constructs the ApplicationContext programmatically on the fly.

Bob Lee also offered his alternative using Guice:

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

Since Groovy does annotations it is possible to make this code even Groovier:

def daos = [ZoneDao, EmailDomainDao, PreferenceDao...]
daos.each { bind(it).to(Class.forName("daos.hibernate.Hibernate${it.simpleName}") }

posted on 2007-04-19 06:13 山风小子 阅读(676) 评论(2)  编辑  收藏 所属分类: Groovy & Grails