将外部化的Groovy DSL Spring bean配置导入Grails resources.groovy

Jam*_*gas 3 grails spring

我有一个Grails应用程序,在我的resources.groovy文件中配置了Spring bean.我想知道是否可以从文件系统上的外部源导入我的bean配置,但仍然保持它们的Groovy DSL风格.

我知道可以从XML文件导入bean配置,详见本文" 是否可以将外部bean配置xml文件导入resources.groovy? ",但想知道如何使用Groovy执行此操作DSL bean配置.

Jam*_*gas 11

看起来Groovy DSL可以实现这一点,就像导入Spring XML配置文件一样.

这篇文章很好地解释了如何实现它.

只需将外部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')
    // ...
}
Run Code Online (Sandbox Code Playgroud)

然后你的integration.groovy文件看起来应该是这样的.

beans {
    myBean(MyBean) { bean ->
        property1 = 123
        property2 = "abc"
    }
}
Run Code Online (Sandbox Code Playgroud)

重要的是要注意,在导入的spring文件中,=之后没有任何符号beans.如果指定beans = { ..... }您的bean将不会被导入.

  • 谢谢你在`beans`之后提到`=`符号的东西 (4认同)