Configuring Additional Beans
Using the Spring Bean DSL
You can easily register new (or override existing) beans by configuring them in
grails-app/conf/spring/resources.groovy which uses the Grails
Spring DSL. Beans are defined inside a
beans property (a Closure):
beans = {
// beans here
}
As a simple example you can configure a bean with the following syntax:
import my.company.MyBeanImpl
beans = {
myBean(MyBeanImpl) {
someProperty = 42
otherProperty = "blue"
}
}
Once configured, the bean can be
auto-wired into Grails artifacts and other classes that support
dependency injection (for example BootStrap.groovy and integration tests) by declaring a public field whose name is your bean's name (in this case myBean):
class ExampleController {
def myBean
…
}
Referencing Existing Beans
Beans declared in resources.groovy or resources.xml can reference other beans by convention. For example if you had a BookService class its Spring bean name would be bookService, so your bean would reference it like this in the DSL:
beans = {
productUtility ( ProductUtility )
{
someProperty = 42
otherProperty = "blue"
springSecurityService = ref ( "springSecurityService" )
}
}
or like this in XML:
"myBean" class="my.company.MyBeanImpl">
"someProperty" value="42" />
"otherProperty" value="blue" />
"springSecurityService" ref="springSecurityService" />
The
bean needs a public setter for the bean reference (and also the two
simple properties), which in Groovy would be defined like this:
package my.company
class MyBeanImpl {
Integer someProperty
String otherProperty
SpringSecurityService springSecurityService or def springSecurityService
}