【问题标题】:Grails plugin: inject list into bean inside doWithSpringGrails插件:将列表注入doWithSpring中的bean
【发布时间】:2018-11-06 17:51:31
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: spring grails grails-plugin


    【解决方案1】:

    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.groovy 应用程序中的BootStrap.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
    

    【讨论】:

    • 谢谢,杰夫,你的例子有效。我在我的项目中使用了相同的结构,并且由于某种原因,它仍然没有在插件中注入列表。顺便说一句,我正在注入服务。可能我错过了一些东西。无论如何,我使用一种解决方法修复了它:在使用 ListFactoryBean 单独定义一个列表 bean 之后,它可以很好地注入它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多