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.MyBeanImplbeans = { 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 inresources.groovy
orresources.xml
can reference other beans by convention. For example if you had aBookService
class its Spring bean name would bebookService
, 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" />
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.companyclass MyBeanImpl { Integer someProperty String otherPropertySpringSecurityService springSecurityService or def springSecurityService }
No comments:
Post a Comment