简体   繁体   English

在Grails Controller中将服务包含在会话范围中吗?

[英]Include Service with Session Scope in Grails Controller?

Using Grails 2.4.5 I have the following service: 使用Grails 2.4.5,我有以下服务:

class CartService {

    static scope = 'session'
    static proxy = true

    def items = []

    def getItemCount() {
        items.size()
    }

}

I want to use this service in a controller: 我想在控制器中使用此服务:

class CartController {

    def cartService // unique per session

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

}

I tried version 0.3 of the Grails scoped-proxy plugin . 我尝试了Grails scoped-proxy插件的 0.3版。 But I get the following error: 但是我收到以下错误:

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

How do I include Service with Session Scope in Grails Controller? 如何在Grails Controller中将服务与会话范围一起包括在内?

Looks like you might have found an issue or two (and also reported) with the plugin with Grails 2.4.x. 看来您可能已经发现Grails 2.4.x的插件有一个或两个问题(并且也有报告)。 If that is the case, you can refer to a blog post by the plugin author: Scoped Services & Proxies in Grails . 在这种情况下,您可以参考插件作者的博客文章: Grails中的Scoped Services&Proxies

There he shows how to do it without the plugin. 他在那里展示了如何在没有插件的情况下进行操作。 I gave it a shot and it works in 2.4.5. 我试了一下,它在2.4.5中有效。

resources.groovy: resources.groovy:

import org.springframework.aop.scope.ScopedProxyFactoryBean

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

Proxy is done manually, so you can remove static proxy 代理是手动完成的,因此您可以删除static proxy

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

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

Controller then references the proxy 控制器然后引用代理

class CartController {
    def cartServiceProxy 
    ... 
}

Note that one point of confusion (for me at least) was that the Controller Documentation for 2.4.x says the default scope is prototype but in the section describing Scoped Services it says that as of 2.3.x the default controller scope is singleton . 请注意,至少在我看来,一个令人困惑的地方是2.4.x版Controller Documentation控制器的默认作用域是prototype但在描述Scoped Services的部分中,它说到2.3.x版的默认控制器作用域是singleton

Hope this helps. 希望这可以帮助。

Update: To clarify a bit, the default controller scope is still prototype after Grails 2.3, but when new applications are generated by grails, configuration is generated that sets default controller scope to singleton (which is really what you should shoot for if possible like Burt says below). 更新:为了澄清一点,默认控制器作用域仍然是Grails 2.3之后的prototype ,但是当grails生成新应用程序时,将生成配置,将默认控制器作用域设置为singleton (如果可能的话,这确实是应该为Burt拍摄的)在下面说)。

Generated config in Config.groovy 在Config.groovy中生成的配置

// The default scope for controllers. May be prototype, session or singleton.
// If unspecified, controllers are prototype scoped.
grails.controllers.defaultScope = 'singleton'

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

相关问题 Vaadin + TabSheet + Grails服务=当前线程没有会话 - Vaadin + TabSheet + Grails Service = No session for current thread Spring Controller作用域和存储会话数据 - spring controller scope and storing session data 从Grails控制器/服务获取OpenID URL - Getting the OpenID URL from a Grails Controller/Service 从Grails控制器启动的新线程的休眠会话问题 - Hibernate session issue with New thread started from Grails controller 当组件作为会话作用域时,无法将Spring控制器作为请求范围 - Cannot scope Spring controller as request when a component is scoped as session Spring RESTful Web服务和bean的“请求”和“会话”范围 - Spring RESTful Web service and bean “request” and “session” scope 会话无法正常工作 - Session not working in grails 如何在 Liferay 6.2 中的每个新选项卡上实例化一个新的会话范围控制器,同时保留旧的 - How to instantiate a new session scope controller on each new tab in liferay 6.2 while keeping the old one 将Grails生成的Java类包含在grails项目中 - Include Grails generated Java class into the grails project Grails-无法从Controller调用服务-&gt;始终获取“无法在空对象错误时调用方法” - Grails - Can't call service from Controller --> always get “Cannot invoke method on null object error”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM