2014-12-03 93 views
3

我有一個Grails應用程序,它在我的resources.groovy文件中配置了Spring bean。我想知道是否可以從文件系統上的外部源導入我的bean配置,但仍然保持Groovy DSL樣式。將外部化的Groovy DSL Spring bean配置導入到Grails resources.groovy

我知道可以從XML文件中導入一個bean配置,詳見本文「Is it possible to import an external bean configuration xml file into resources.groovy?」,但想知道如何使用Groovy DSL bean配置來完成此操作。

回答

10

看起來Groovy DSL可以像導入Spring XML配置文件一樣進行。

This post對如何實現它有很好的解釋。

簡單地導入外部Spring配置到您的resources.groovy文件像這樣:

beans = { 
    importBeans('file:grails-app/conf/spring/common.xml') 
    importBeans('file:grails-app/conf/spring/job-definitions.xml') 
    importBeans('file:grails-app/conf/spring/integration.groovy') 
    // ... 
} 

然後你integration.groovy文件應該是這個樣子。

beans { 
    myBean(MyBean) { bean -> 
     property1 = 123 
     property2 = "abc" 
    } 
} 

需要注意的是在春天文件導入有beans後無=跡象是非常重要的。如果您指定beans = { ..... },則不會導入您的bean。

+3

感謝在'beans'之後提及'='標誌事物 – 2016-04-20 13:58:50