如何在Groovy spring DSL中定义原型范围的bean
在grails中,可以使用SpringGroovyDSL在如何在Groovy spring DSL中定义原型范围的bean,spring,grails-3.0,Spring,Grails 3.0,在grails中,可以使用SpringGroovyDSL在resources.groovy中定义Springbean beans = { myBean(MyBeanImpl) { someProperty = 42 otherProperty = "blue" bookService = ref("bookService") } } 如何使用此DSL定义原型范围的bean?我在中找不到关于此的任何信息。这应该可以: beans =
resources.groovy
中定义Springbean
beans = {
myBean(MyBeanImpl) {
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
如何使用此DSL定义原型范围的bean?我在中找不到关于此的任何信息。这应该可以:
beans = {
myBean(MyBeanImpl) { bean ->
bean.scope = 'prototype'
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
我同意杰夫·斯科特·布朗的观点 你怎么知道它不起作用?我们使用的是Grails2.3.9 我的资源中有这个。groovy:
httpBuilderPool(HTTPBuilder) { bean ->
bean.scope = 'prototype' // A new service is created every time it is injected into another class
client = ref('httpClientPool')
}
...
在Spock集成测试中:
import grails.test.spock.IntegrationSpec
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.log4j.Logger
class JukinHttpBuilderSpec extends IntegrationSpec {
private final Logger log = Logger.getLogger(getClass())
def httpBuilderPool
def grailsApplication
void "test jukinHttpBuilder instantiates"() {
expect:
httpBuilderPool
httpBuilderPool.client instanceof CloseableHttpClient
}
void "test httpBuilderPool is prototype instaance"() {
when: 'we call getBean on the application context'
def someInstanceIds = (1..5).collect { grailsApplication.mainContext.getBean('httpBuilderPool').toString() }.toSet()
log.info someInstanceIds.toString()
then: 'we should get a new instance every access'
someInstanceIds.size() == 5
}
void "test injected httpBuilderPool is prototype instance"() {
when: 'we access the injeted httpBuilderPool'
def someInstanceIds = (1..5).collect { httpBuilderPool.toString() }.toSet()
log.info someInstanceIds.toString()
then: 'it uses the same instance every time'
someInstanceIds.size() == 1
}
}
这向我展示了它在2.3.9中的工作原理。“我在文档中找不到任何与此相关的内容”-在第18.3节中,有一个示例将sessionFactory bean配置为请求范围。该示例没有具体说明如何配置原型作用域bean,但所有作用域的语法都是相同的。只需将“请求”替换为“原型”。@JeffScottBrown抱歉,我错过了。我浏览并搜索了那个文档中的原型,但这对我不起作用。我正在使用Grails2.4.4。你能提供一些帮助吗?@Richa不知道你在做什么,特别是什么不起作用,提供帮助是非常困难的。上面显示的内容确实有效,并且始终有效。应用程序中一定还有其他因素使事情变得复杂。