19.2 Runtime Spring with the Beans DSL

This Bean builder in Grails aims to provide a simplified way of wiring together dependencies that uses Spring at its core.

In addition, Spring’s regular way of configuration (via XML and annotations) is static and difficult to modify and configure at runtime, other than programmatic XML creation which is both error prone and verbose. Grails' BeanBuilder changes all that by making it possible to programmatically wire together components at runtime, allowing you to adapt the logic based on system properties or environment variables.

This enables the code to adapt to its environment and avoids unnecessary duplication of code (having different Spring configs for test, development and production environments)

The BeanBuilder class

Grails provides a grails.spring.BeanBuilder class that uses dynamic Groovy to construct bean definitions. The basics are as follows:

  1. import org.apache.commons.dbcp.BasicDataSource
  2. import org.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean
  3. import org.springframework.context.ApplicationContext
  4. import grails.spring.BeanBuilder
  5. def bb = new BeanBuilder()
  6. bb.beans {
  7. dataSource(BasicDataSource) {
  8. driverClassName = "org.h2.Driver"
  9. url = "jdbc:h2:mem:grailsDB"
  10. username = "sa"
  11. password = ""
  12. }
  13. sessionFactory(ConfigurableLocalSessionFactoryBean) {
  14. dataSource = ref('dataSource')
  15. hibernateProperties = ["hibernate.hbm2ddl.auto": "create-drop",
  16. "hibernate.show_sql": "true"]
  17. }
  18. }
  19. ApplicationContext appContext = bb.createApplicationContext()
Within plugins and the grails-app/conf/spring/resources.groovy file you don’t need to create a new instance of BeanBuilder. Instead the DSL is implicitly available inside the doWithSpring and beans blocks respectively.

This example shows how you would configure Hibernate with a data source with the BeanBuilder class.

Each method call (in this case dataSource and sessionFactory calls) maps to the name of the bean in Spring. The first argument to the method is the bean’s class, whilst the last argument is a block. Within the body of the block you can set properties on the bean using standard Groovy syntax.

Bean references are resolved automatically using the name of the bean. This can be seen in the example above with the way the sessionFactory bean resolves the dataSource reference.

Certain special properties related to bean management can also be set by the builder, as seen in the following code:

  1. sessionFactory(ConfigurableLocalSessionFactoryBean) { bean ->
  2. // Autowiring behaviour. The other option is 'byType'. <<autowire>>
  3. bean.autowire = 'byName'
  4. // Sets the initialisation method to 'init'. [init-method]
  5. bean.initMethod = 'init'
  6. // Sets the destruction method to 'destroy'. [destroy-method]
  7. bean.destroyMethod = 'destroy'
  8. // Sets the scope of the bean. <<scope>>
  9. bean.scope = 'request'
  10. dataSource = ref('dataSource')
  11. hibernateProperties = ["hibernate.hbm2ddl.auto": "create-drop",
  12. "hibernate.show_sql": "true"]
  13. }

The strings in square brackets are the names of the equivalent bean attributes in Spring’s XML definition.

Using BeanBuilder with Spring MVC

Include the grails-spring-<version>.jar file in your classpath to use BeanBuilder in a regular Spring MVC application. Then add the following <context-param> values to your /WEB-INF/web.xml file:

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/applicationContext.groovy</param-value>
  4. </context-param>
  5. <context-param>
  6. <param-name>contextClass</param-name>
  7. <param-value>
  8. grails.web.servlet.context.GrailsWebApplicationContext
  9. </param-value>
  10. </context-param>

Then create a /WEB-INF/applicationContext.groovy file that does the rest:

  1. import org.apache.commons.dbcp.BasicDataSource
  2. beans {
  3. dataSource(BasicDataSource) {
  4. driverClassName = "org.h2.Driver"
  5. url = "jdbc:h2:mem:grailsDB"
  6. username = "sa"
  7. password = ""
  8. }
  9. }

Loading Bean Definitions from the File System

You can use the BeanBuilder class to load external Groovy scripts that define beans using the same path matching syntax defined here. For example:

  1. def bb = new BeanBuilder()
  2. bb.loadBeans("classpath:*SpringBeans.groovy")
  3. def applicationContext = bb.createApplicationContext()

Here the BeanBuilder loads all Groovy files on the classpath ending with SpringBeans.groovy and parses them into bean definitions. An example script can be seen below:

  1. import org.apache.commons.dbcp.BasicDataSource
  2. import org.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean
  3. beans {
  4. dataSource(BasicDataSource) {
  5. driverClassName = "org.h2.Driver"
  6. url = "jdbc:h2:mem:grailsDB"
  7. username = "sa"
  8. password = ""
  9. }
  10. sessionFactory(ConfigurableLocalSessionFactoryBean) {
  11. dataSource = dataSource
  12. hibernateProperties = ["hibernate.hbm2ddl.auto": "create-drop",
  13. "hibernate.show_sql": "true"]
  14. }
  15. }

Adding Variables to the Binding (Context)

If you’re loading beans from a script you can set the binding to use by creating a Groovy Binding:

  1. def binding = new Binding()
  2. binding.maxSize = 10000
  3. binding.productGroup = 'finance'
  4. def bb = new BeanBuilder()
  5. bb.binding = binding
  6. bb.loadBeans("classpath:*SpringBeans.groovy")
  7. def ctx = bb.createApplicationContext()

Then you can access the maxSize and productGroup properties in your DSL files.