首页 文章

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

提问于
浏览
3

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

我知道可以从XML文件中导入bean配置,详见本文“Is it possible to import an external bean configuration xml file into resources.groovy?”,但想知道如何使用Groovy DSL bean配置 .

1 回答

  • 11

    看起来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"
        }
    }
    

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

相关问题