Vampire
Vampire

Reputation: 38704

How to use ConfigSlurper to configure a JavaBean

The JavaDoc of ConfigSlurper says

Settings can either be bound into nested maps or onto a specified JavaBean instance.

How does the latter work?
I only find examples of using the result as map or properties.

Upvotes: 2

Views: 510

Answers (1)

tim_yates
tim_yates

Reputation: 171124

Given two beans:

@groovy.transform.ToString
class Child {
    Integer age
}

@groovy.transform.ToString
class Bean {
    String name
    Child child
}

We can parse some config into a Bean:

Bean b = new ConfigSlurper().parse('''
   name = 'tim'
   child {
       age = 5
   }
''')

Then printing b gives:

 Bean(tim, Child(5))

Upvotes: 2

Related Questions