2011-04-13 127 views
1

當我使用Grails提供的Spring DSL時,可以執行構造函數注入。如果是這樣,一個例子將不勝感激。使用Grails Spring DSL的依賴注入

如果構造函數注入是不可能的,有沒有其他的方式可以注入spring bean而不需要依賴關係公共屬性。在Java項目中使用Spring我能做到這一點

class Foo { 

    @Autowired 
    private Bar bar 
} 

它將自動裝配的Bar依賴通過名稱或使用@Autowired註解像你通常做鍵入

回答

4

,可以使用構造器注入即使使用BeanBuilder DSL

bb.beans { 
    exampleBean(MyExampleBean, "firstArgument", 2) { 
     someProperty = [1,2,3] 
    } 
} 

,每當你想引用其他豆類構造函數的參數,使用ref()方法

bb.beans { 
    exampleBean(MyExampleBean, "firstArgument", ref('anotherBean')) { 
     someProperty = [1,2,3] 
    } 
} 
+0

看到http://grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html#14.4%20The%20BeanBuilder%20DSL%20Explained – 2011-04-14 07:12:39

+0

這工作很好,但爲什麼不'constructorArgs = ref(' someBean')'也有效? – raffian 2012-09-14 17:39:55