19.3 The BeanBuilder DSL Explained

Using Constructor Arguments

Constructor arguments can be defined using parameters to each bean-defining method. Put them after the first argument (the Class):

  1. bb.beans {
  2. exampleBean(MyExampleBean, "firstArgument", 2) {
  3. someProperty = [1, 2, 3]
  4. }
  5. }

This configuration corresponds to a MyExampleBean with a constructor that looks like this:

  1. MyExampleBean(String foo, int bar) {
  2. ...
  3. }

Configuring the BeanDefinition (Using factory methods)

The first argument to the closure is a reference to the bean configuration instance, which you can use to configure factory methods and invoke any method on the AbstractBeanDefinition class:

  1. bb.beans {
  2. exampleBean(MyExampleBean) { bean ->
  3. bean.factoryMethod = "getInstance"
  4. bean.singleton = false
  5. someProperty = [1, 2, 3]
  6. }
  7. }

As an alternative you can also use the return value of the bean defining method to configure the bean:

  1. bb.beans {
  2. def example = exampleBean(MyExampleBean) {
  3. someProperty = [1, 2, 3]
  4. }
  5. example.factoryMethod = "getInstance"
  6. }

Using Factory beans

Spring defines the concept of factory beans and often a bean is created not directly from a new instance of a Class, but from one of these factories. In this case the bean has no Class argument and instead you must pass the name of the factory bean to the bean defining method:

  1. bb.beans {
  2. myFactory(ExampleFactoryBean) {
  3. someProperty = [1, 2, 3]
  4. }
  5. myBean(myFactory) {
  6. name = "blah"
  7. }
  8. }

Another common approach is provide the name of the factory method to call on the factory bean. This can be done using Groovy’s named parameter syntax:

  1. bb.beans {
  2. myFactory(ExampleFactoryBean) {
  3. someProperty = [1, 2, 3]
  4. }
  5. myBean(myFactory: "getInstance") {
  6. name = "blah"
  7. }
  8. }

Here the getInstance method on the ExampleFactoryBean bean will be called to create the myBean bean.

Creating Bean References at Runtime

Sometimes you don’t know the name of the bean to be created until runtime. In this case you can use a string interpolation to invoke a bean defining method dynamically:

  1. def beanName = "example"
  2. bb.beans {
  3. "${beanName}Bean"(MyExampleBean) {
  4. someProperty = [1, 2, 3]
  5. }
  6. }

In this case the beanName variable defined earlier is used when invoking a bean defining method. The example has a hard-coded value but would work just as well with a name that is generated programmatically based on configuration, system properties, etc.

Furthermore, because sometimes bean names are not known until runtime you may need to reference them by name when wiring together other beans, in this case using the ref method:

  1. def beanName = "example"
  2. bb.beans {
  3. "${beanName}Bean"(MyExampleBean) {
  4. someProperty = [1, 2, 3]
  5. }
  6. anotherBean(AnotherBean) {
  7. example = ref("${beanName}Bean")
  8. }
  9. }

Here the example property of AnotherBean is set using a runtime reference to the exampleBean. The ref method can also be used to refer to beans from a parent ApplicationContext that is provided in the constructor of the BeanBuilder:

  1. ApplicationContext parent = ...//
  2. def bb = new BeanBuilder(parent)
  3. bb.beans {
  4. anotherBean(AnotherBean) {
  5. example = ref("${beanName}Bean", true)
  6. }
  7. }

Here the second parameter true specifies that the reference will look for the bean in the parent context.

Using Anonymous (Inner) Beans

You can use anonymous inner beans by setting a property of the bean to a block that takes an argument that is the bean type:

  1. bb.beans {
  2. marge(Person) {
  3. name = "Marge"
  4. husband = { Person p ->
  5. name = "Homer"
  6. age = 45
  7. props = [overweight: true, height: "1.8m"]
  8. }
  9. children = [ref('bart'), ref('lisa')]
  10. }
  11. bart(Person) {
  12. name = "Bart"
  13. age = 11
  14. }
  15. lisa(Person) {
  16. name = "Lisa"
  17. age = 9
  18. }
  19. }

In the above example we set the marge bean’s husband property to a block that creates an inner bean reference. Alternatively if you have a factory bean you can omit the type and just use the specified bean definition instead to setup the factory:

  1. bb.beans {
  2. personFactory(PersonFactory)
  3. marge(Person) {
  4. name = "Marge"
  5. husband = { bean ->
  6. bean.factoryBean = "personFactory"
  7. bean.factoryMethod = "newInstance"
  8. name = "Homer"
  9. age = 45
  10. props = [overweight: true, height: "1.8m"]
  11. }
  12. children = [ref('bart'), ref('lisa')]
  13. }
  14. }

Abstract Beans and Parent Bean Definitions

To create an abstract bean definition define a bean without a Class parameter:

  1. class HolyGrailQuest {
  2. def start() { println "lets begin" }
  3. }
  1. class KnightOfTheRoundTable {
  2. String name
  3. String leader
  4. HolyGrailQuest quest
  5. KnightOfTheRoundTable(String name) {
  6. this.name = name
  7. }
  8. def embarkOnQuest() {
  9. quest.start()
  10. }
  11. }
  1. import grails.spring.BeanBuilder
  2. def bb = new BeanBuilder()
  3. bb.beans {
  4. abstractBean {
  5. leader = "Lancelot"
  6. }
  7. ...
  8. }

Here we define an abstract bean that has a leader property with the value of "Lancelot". To use the abstract bean set it as the parent of the child bean:

  1. bb.beans {
  2. ...
  3. quest(HolyGrailQuest)
  4. knights(KnightOfTheRoundTable, "Camelot") { bean ->
  5. bean.parent = abstractBean
  6. quest = ref('quest')
  7. }
  8. }
When using a parent bean you must set the parent property of the bean before setting any other properties on the bean!

If you want an abstract bean that has a Class specified you can do it this way:

  1. import grails.spring.BeanBuilder
  2. def bb = new BeanBuilder()
  3. bb.beans {
  4. abstractBean(KnightOfTheRoundTable) { bean ->
  5. bean.'abstract' = true
  6. leader = "Lancelot"
  7. }
  8. quest(HolyGrailQuest)
  9. knights("Camelot") { bean ->
  10. bean.parent = abstractBean
  11. quest = quest
  12. }
  13. }

In this example we create an abstract bean of type KnightOfTheRoundTable and use the bean argument to set it to abstract. Later we define a knights bean that has no Class defined, but inherits the Class from the parent bean.

Using Spring Namespaces

Since Spring 2.0, users of Spring have had easier access to key features via XML namespaces. You can use a Spring namespace in BeanBuilder by declaring it with this syntax:

  1. xmlns context:"http://www.springframework.org/schema/context"

and then invoking a method that matches the names of the Spring namespace tag and its associated attributes:

  1. context.'component-scan'('base-package': "my.company.domain")

You can do some useful things with Spring namespaces, such as looking up a JNDI resource:

  1. xmlns jee:"http://www.springframework.org/schema/jee"
  2. jee.'jndi-lookup'(id: "dataSource", 'jndi-name': "java:comp/env/myDataSource")

This example will create a Spring bean with the identifier dataSource by performing a JNDI lookup on the given JNDI name. With Spring namespaces you also get full access to all of the powerful AOP support in Spring from BeanBuilder. For example given these two classes:

  1. class Person {
  2. int age
  3. String name
  4. void birthday() {
  5. ++age;
  6. }
  7. }
  1. class BirthdayCardSender {
  2. List peopleSentCards = []
  3. void onBirthday(Person person) {
  4. peopleSentCards << person
  5. }
  6. }

You can define an aspect that uses a pointcut to detect whenever the birthday() method is called:

  1. xmlns aop:"http://www.springframework.org/schema/aop"
  2. fred(Person) {
  3. name = "Fred"
  4. age = 45
  5. }
  6. birthdayCardSenderAspect(BirthdayCardSender)
  7. aop {
  8. config("proxy-target-class": true) {
  9. aspect(id: "sendBirthdayCard", ref: "birthdayCardSenderAspect") {
  10. after method: "onBirthday",
  11. pointcut: "execution(void ..Person.birthday()) and this(person)"
  12. }
  13. }
  14. }