简体   繁体   English

Spring 4 / Groovy DSL-自动装配

[英]Spring 4 / Groovy DSL - autowiring

How do I enable autowiring when using the Groovy DSL in Spring 4? 在Spring 4中使用Groovy DSL时如何启用自动装配?

if my config.groovy file currently looks like this: 如果我的config.groovy文件当前如下所示:

beans {
    mongoClient(com.mongodb.MongoClient)

    hello(org.abiri.HelloImpl) {
        mongoClient = mongoClient
    }
}

Previously in XML configuration, we could have done this: 以前在XML配置中,我们可以这样做:

<bean id="hello" class="org.abiri.HelloImpl" autowire="byType" />

And we could even enable that for the whole file: 我们甚至可以为整个文件启用该功能:

<beans default-autowire="byType" />

What is the equivalent of those XML snippets in the new Groovy DSL, ie what do I need to do in order for the mongoClient to be autowired to hello ? 什么是新的Groovy DSL中的那些XML代码段等效,即,为了将mongoClient autowired到hello我需要做什么?

You need to use 您需要使用

hello(org.abiri.HelloImpl) { bean ->
  bean.autowire = "byType"
}

The configuration closure is passed a parameter that you can use to configure things that would be attributes on the bean element in an XML configuration. 配置闭包中传递了一个参数,您可以使用该参数来配置XML配置中bean元素上的属性。 In addition to autowire this includes scope , initMethod and destroyMethod . 除了autowire外,这还包括scopeinitMethoddestroyMethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM