gpt4 book ai didi

grails - 在grails应用中,服务中的字段是否由用户共享?

转载 作者:行者123 更新时间:2023-12-02 15:21:15 25 4
gpt4 key购买 nike

我对服务中的字段是否由用户共享有疑问吗?我的grails版本是2.3.4。
现在,我有一个 Controller ,该 Controller 具有两个操作,并试图设置并从服务 Realm 获取值(value)。

//This is a controller
class TestController{
def testService

def setValue(){
testService.setValue("123")
}

def getValue(){}
println testService.getValue()
}
}

//This is a service
class TestService{
def var

def setValue(def value){
var = value
}

def getValue(){}
return var
}
}

换句话说,如果几个用户在 Controller 中使用 Action getValue,他们是否在服务中共享 var
谢谢!

最佳答案

是的,默认情况下,所有services都是单例,因此每个Web应用程序只有一个服务实例,但是内部的功能不同步:

By default, access to service methods is not synchronised, so nothing prevents concurrent execution of those methods. In fact, because the service is a singleton and may be used concurrently, you should be very careful about storing state in a service. Or take the easy (and better) road and never store state in a service.

You can change this behaviour by placing a service in a particular scope. The supported scopes are:

prototype - A new service is created every time it is injected into another class
request - A new service will be created per request
flash - A new service will be created for the current and next request only
flow - In web flows the service will exist for the scope of the flow
conversation - In web flows the service will exist for the scope of the conversation. ie a root flow and its sub flows
session - A service is created for the scope of a user session
singleton (default) - Only one instance of the service ever exists

关于grails - 在grails应用中,服务中的字段是否由用户共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com