Java Reference
In-Depth Information
when: def result = accountProcessor.processAccounts()
then:
result == 3.0
accounts. every { account ->
account.balance.toString().endsWith "9"
}
}
}
Both the AccountProcessor and the AccountDAO beans are autowired into the test.
The DAO is used to retrieve the accounts. Then, when the processor processes the ac-
counts, three dollars are returned.
The other test condition relies on the fact that the initial balance for each account is divis-
ible by 10. Therefore, after subtracting one from each account, the updated balances should
all end in the digit 9. It's kind of kludgy, but it works.
The point of this exercise was to show that with the Java configuration option you can
write whatever code you want to configure the bean before releasing it. There's not much
Groovy can add to that, though it's worth proving that the Java configuration option works
on a Groovy class as well.
Normally I wouldn't use Spring to manage basic entity instances. Spring specializes
in managing back-end services, especially those that would normally be designed as
singletons. Spring beans are all assumed to be singletons unless otherwise specified. Still,
you can tell Spring to provide a new instance each time by making the scope of the bean
equal to prototype .
Listing 7.24 shows a Java (actually, a Groovy) configuration file, with a single bean defini-
tion of type Account called prototypeAccount . It uses the AccountDAO to gener-
ate a new bean each time a prototypeAccount is requested, essentially making Spring
a factory for Account beans, all of which start with an initial balance of 100.
Listing 7.24. A Spring configuration file in Groovy as a factory for Accounts
package mjg.spring.config
import mjg.spring.dao.AccountDAO
import mjg.spring.entities.Account
 
Search WWH ::




Custom Search