Using XML

Beans can be configured using the grails-app/conf/spring/resources.xml file of your Grails application. This file is typical Spring XML file and the Spring documentation has an excellent reference on how to go about configuration Spring beans. As a trivial example you can configure a bean with the following syntax:

<bean id="myBean" class="my.company.MyBeanImpl"></bean>

Once configured the bean, in this case named myBean, can be auto-wired into most Grails types including controllers, tag libraries, services and so on:

class ExampleController {

def myBean }

Referencing Existing Beans

Beans declared in resources.xml can also reference Grails classes by convention. For example if you need a reference to a service such as BookService in your bean you use the property name representation of the class name. In the case of BookService this would be bookService. For example:

<bean id="myBean" class="my.company.MyBeanImpl">
	<property name="bookService" ref="bookService" />	
</bean>

The bean itself would of course need a public setter, which in Groovy is defined like this:

package my.company
class MyBeanImpl {
	BookService bookService
}

or in Java like this:

package my.company;
class MyBeanImpl {
	private BookService bookService;
	public void setBookService(BookService theBookService) {
		this.bookService = theBookService;
	}
}

Since much of Grails configuration is done at runtime by convention many of the beans are not declared anywhere, but can still be referenced inside your Spring configuration. For example if you need a reference to the Grails DataSource you could do:

<bean id="myBean" class="my.company.MyBeanImpl">
	<property name="bookService" ref="bookService" />	
	<property name="dataSource" ref="dataSource" />
</bean>

Or if you need the Hibernate SessionFactory this will work:

<bean id="myBean" class="my.company.MyBeanImpl">
	<property name="bookService" ref="bookService" />	
	<property name="sessionFactory" ref="sessionFactory" />
</bean>

For a full reference of the available beans see the Plug-in reference in the reference guide.

Using the Spring DSL

If you want to use the Spring DSL that Grails provides then you need to create a grails-app/conf/spring/resources.groovy file and define a property called beans that is assigned a block:

beans = {
	// beans here
}

The same configuration for the XML example could be represented as:

beans = {
	myBean(my.company.MyBeanImpl) {
		bookService = ref("bookService")
	}	
}

The main advantage of this way is that you can now mix logic in within your bean definitions, for example based on the environment:

import grails.util.*
beans = {
	switch(GrailsUtil.environment) {
		case "production":
			myBean(my.company.MyBeanImpl) {
				bookService = ref("bookService")
			}

break case "development": myBean(my.company.mock.MockImpl) { bookService = ref("bookService") } break } }

The GrailsApplication object can be accessed by the application variable, which can be used to access config (amongst other things):

import grails.util.*
beans = {
    if (application.config.my.company.mockService) {
        myBean(my.company.mock.MockImpl) {
            bookService = ref("bookService")
        }   
    } else {
        myBean(my.company.MyBeanImpl) {
            bookService = ref("bookService")
        }
    }
}