Java Reference
In-Depth Information
18 Leaving out Maggie, who sadly always seems to be an afterthought.
You're not limited to defining a single bean, of course. The next listing shows a test that
creates several beans and sets their relationships.
Listing 7.29. Defining several related beans with the BeanBuilder
void testBeanReferences() {
def bb = new BeanBuilder()
bb.beans {
homer(Bean1) {
person = "homer"
age = 45
props = [overweight:true, height:"1.8m"]
children = ["bart", "lisa"]
}
bart(Bean1) {
person = "bart"
age = 11
}
lisa(Bean1) {
person = "lisa"
age = 9
}
marge(Bean2) {
person = "marge"
bean1 = homer
children = [bart, lisa]
}
}
def ctx = bb.createApplicationContext()
def homer = ctx.getBean("homer")
def marge = ctx.getBean("marge")
def bart = ctx.getBean("bart")
def lisa = ctx.getBean("lisa")
assertEquals homer, marge.bean1
assertEquals 2, marge.children.size()
assertTrue marge.children.contains(bart)
assertTrue marge.children.contains(lisa)
}
The beans named homer , bart , and lisa are all instances of the Bean1 class. The
marge bean is an instance of Bean2 , which adds a reference of type Bean1 called
bean1 .Here the bean1 reference in marge isassigned to homer .The Bean1 class also
 
Search WWH ::




Custom Search