简体   繁体   中英

grails:spring:resources.groovy - is there any difference between refrencing beans with and without ref()

Help me with this small confusion as I am new to grails and working on grails with spring

Is there any difference between ref to firstBean in secondBean and thirdBean in resources.groovy

beans = {

    firstBean(someclass)

    secondBean(someotherclass) {
        property = firstBean
    }

    thirdBean(someotherclass) {
        property = ref(firstBean)
    }
}

In your example there is hardly any difference. You are getting basically the objects you just have defined there. So this only works, if you can order your code, so this works and if the refs are right within your resources.groovy . The more common case is to use ref with strings, which may "forward reference". Eg

beans = {
    // fails! print b1
    // fails! print ref(b1)
    print ref("b1")

    b1(Expando)

    print b1
    print ref(b1)
    print ref("b1")
}

I'd use ref(<String>) for good measure, to give the underlying spring injection framework the easiest way to handle its dependencies (eg so components need only to be created, if and when they are needed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM