Java Reference
In-Depth Information
Account a2 = (Account) ctx.getBean("prototypeAccount")
Account a3 = (Account) ctx.getBean("prototypeAccount")
then:
a3.id == a2.id + 1
a2.id == a1.id + 1
a1.balance == 100.0
a2.balance == 100.0
a3.balance == 100.0
}
}
This time the application context itself is autowired into the test, because I want to
call its getBean method myself multiple times. The test then gets three instances of
prototype-Account and verifies first that their account numbers are consecutive and
then that all three have the expected balance.
The bottom line is that you can use Groovy to create a Spring configuration file as easily as
you can use Java, and in both cases you have the full power of the language to do whatever
additional configuration you might want before releasing the beans.
All of the techniques so far have discussed how to use capabilities defined in Spring.
There's one new capability, however, that allows you to define complex beans using a
builder notation. This mechanism came from the Grails project but can be used anywhere.
7.6. Building beans with the Grails BeanBuilder
SofarinthisbookIhaven'tsaidmuchaboutGrails,thepowerfulframeworkthatcombines
GroovyDSLswithSpringMVCandHibernate.I'lldiscussGrailsmuchmorein chapter 10
on Groovy web applications, but part of it is relevant here. Normally innovations in Spring
find their way into Grails, usually in the form of a plugin, but every once in a while a Grails
innovation goes the other way.
The Grails BeanBuilder is an example. The grails.spring.BeanBuilder class
uses Groovy's builder syntax to create Spring configuration files. Everything you can do
in regular configuration files you can do using the Grails BeanBuilder class. The best
part, and the part most relevant for discussion here, is that you don't need to be working on
a Grails project to use the BeanBuilder .
Search WWH ::




Custom Search