如何在 Groovy spring DSL 中定义原型作用域 bean
How to define a prototype scoped bean in Groovy spring DSL
在 grails 中,使用 spring groovy DSL
在 resources.groovy
中定义 spring bean
beans = {
myBean(MyBeanImpl) {
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
如何使用此 DSL 定义原型作用域 bean?我在 documentation
中找不到任何相关内容
这应该有效:
beans = {
myBean(MyBeanImpl) { bean ->
bean.scope = 'prototype'
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
我同意 Jeff Scott Brown 的观点。
你怎么知道它不起作用?我们正在使用 Grails 2.3.9。
我的 resources.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 中有效。
在 grails 中,使用 spring groovy DSL
在resources.groovy
中定义 spring bean
beans = {
myBean(MyBeanImpl) {
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
如何使用此 DSL 定义原型作用域 bean?我在 documentation
中找不到任何相关内容这应该有效:
beans = {
myBean(MyBeanImpl) { bean ->
bean.scope = 'prototype'
someProperty = 42
otherProperty = "blue"
bookService = ref("bookService")
}
}
我同意 Jeff Scott Brown 的观点。
你怎么知道它不起作用?我们正在使用 Grails 2.3.9。
我的 resources.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 中有效。