首页 > 解决方案 > Grails插件:将列表注入doWithSpring中的bean

问题描述

我有一个带有 List 属性的 bean。正如文档中提到的,您可以使用 beans DSL 轻松地将 List 属性注入 bean:

def example = exampleBean(MyExampleBean) {
        someProperty = [1, 2, 3]
    }

它适用于resources.groovy,但如果你在插件的doWithSpring闭包中这样做 - 相同的 bean 定义不起作用。

这是一个 Grails 错误(我使用的是 Grails 3.3.3)吗?是否有任何解决方法可以使其在插件中工作?

标签: springgrailsgrails-plugin

解决方案


请参阅https://github.com/jeffbrown/taraskahut上的项目。

https://github.com/jeffbrown/taraskahut/blob/df3df67cb8a6dd24317f45aa51b6fff449b60ed1/helper/src/main/groovy/helper/HelperGrailsPlugin.groovy#L43-L48的插件描述符包含以下内容:

    Closure doWithSpring() { {->
            exampleBean(MyExampleBean) {
                someProperty = [1, 2, 3, 5, 8]
            }
        }
    }

https://github.com/jeffbrown/taraskahut/blob/df3df67cb8a6dd24317f45aa51b6fff449b60ed1/app/grails-app/init/app/BootStrap.groovyBootStrap.groovy的应用程序中包含以下内容:

package app

import helper.MyExampleBean

class BootStrap {

    MyExampleBean exampleBean

    def init = { servletContext ->
        log.debug "someProperty is ${exampleBean.someProperty}"
    }
    def destroy = {
    }
}

运行应用程序表明属性初始化按预期工作:

$ ./gradlew app:bootRun
...
:app:processResources
:app:classes
:app:findMainClass
:app:bootRun
2018-11-06 13:19:52.983 DEBUG --- [           main] app.BootStrap
    : someProperty is [1, 2, 3, 5, 8]
Grails application running at http://localhost:8080 in environment: development

推荐阅读