Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added info about classLoader property

...

Code Block
def company = builder.company( name: 'ACME' ) {
   address( id: 'a1', line1: '123 Groovy Rd', zip: 12345, state: 'JV' )
   employee(  name: 'Duke', employeeId: 1, address: a1 )
}
   
Code Block
def company = builder.company( name: 'ACME' ) {
   address( id: 'a1', line1: '123 Groovy Rd', zip: 12345, state: 'JV' )
   employee(  name: 'Duke', employeeId: 1 ){
      address( refId: 'a1' )
   }
}
  

Its worth mentioning that you can not modify the properties of a referenced bean.

For those rare occasions where OGB can't locate your classes (it happens when you run a script using groovyConsole) you may define a classLoader for OGB to resolve classes. Try for example running the following script inside groovyConsole and then comment out the classLoader property.

Code Block

class Conference {
   String name
   List speakers = []
}

class Speaker {
   String name
}

def ogb = new ObjectGraphBuilder( classLoader: getClass().classLoader )
def j1 = ogb.conference( name: 'JavaOne') {
   speaker( name: 'Duke' )
}
assert j1.speakers.size() == 1
assert j1.speakers[0].name == 'Duke'