1

使用 Grails 2.4.5 我有以下服务:

class CartService {

    static scope = 'session'
    static proxy = true

    def items = []

    def getItemCount() {
        items.size()
    }

}

我想在控制器中使用此服务:

class CartController {

    def cartService // unique per session

    def addItem = {
        cartService.items << new CartItem(product: Product.get(params.id))
    }

}

我尝试了Grails scoped-proxy 插件的 0.3 版。但我收到以下错误:

Error creating bean with name 'cartService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'cartService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

如何在 Grails 控制器中包含具有会话范围的服务?

4

1 回答 1

0

看起来您可能已经发现了 Grails 2.4.x 插件的一两个问题(并且也报告了)。如果是这样,你可以参考插件作者的一篇博文:Scoped Services & Proxies in Grails

他在那里展示了如何在没有插件的情况下做到这一点。我试了一下,它在 2.4.5 中工作。

资源.groovy:

import org.springframework.aop.scope.ScopedProxyFactoryBean

beans = {
    cartServiceProxy(ScopedProxyFactoryBean) {
        targetBeanName = 'cartService'
        proxyTargetClass = true
    }
}

代理是手动完成的,因此您可以删除static proxy

class CartService {
    static scope = 'session'
    def items = []

    def getItemCount() {
        items.size()
    }
}

控制器然后引用代理

class CartController {
    def cartServiceProxy 
    ... 
}

请注意,混淆的一点(至少对我而言)是2.4.x 的控制器文档说默认范围是prototype,但在描述Scoped Services的部分中它说从 2.3.x 开始,默认控制器范围是singleton.

希望这可以帮助。

更新:为了澄清一点,默认控制器范围仍然prototype在 Grails 2.3 之后,但是当 grails 生成新应用程序时,会生成将默认控制器范围设置为的配置singleton(如果可能的话,这确实是你应该为之努力的,就像 Burt 说的那样)。

在 Config.groovy 中生成的配置

// The default scope for controllers. May be prototype, session or singleton.
// If unspecified, controllers are prototype scoped.
grails.controllers.defaultScope = 'singleton'
于 2015-08-30T15:11:23.217 回答