使用Grails Services上传照片 [英] Uploading photos using Grails Services

查看:99
本文介绍了使用Grails Services上传照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下,在Grails中最适合我的上传照片服务的范围是什么?我在Grails 2.3.4 Web应用程序中创建了此PhotoService,它所做的就是获取request.getFile("myfile")并执行必要的步骤,以在用户每次要上传图像时将其保存在硬盘上.为了说明它的外观,我给出了这些类的骨架.

I would like to ask, What would be the most suitable scope for my upload photo service in Grails ? I created this PhotoService in my Grails 2.3.4 web app, all it does is to get the request.getFile("myfile") and perform the necessary steps to save it on the hard drive whenever a user wants to upload an image. To illustrate what it looks like, I give a skeleton of these classes.

PhotoPageController {

    def photoService

    def upload(){
       ...
       photoService.upload(request.getFile("myfile"))
       ...
    }
}

PhotoService{

     static scope="request"
     def upload(def myFile){
        ...
        // I do a bunch of task to save the photo 
        ...
     }
}

上面的代码不是确切的代码,我只是想显示流程.但是我的问题是:

The code above isn't the exact code, I just wanted to show the flow. But my question is:

问题: 我找不到这些不同的grails范围的确切定义,它们有一个衬里说明,但是我无法弄清楚请求范围是否意味着向控制器的每个请求都注入了一个bean,或者每次请求都上传控制器的动作?

Question: I couldn't find the exact definition of these different grails scopes, they have a one liner explanation but I couldn't figure out if request scope means for every request to the controller one bean is injected, or each time a request comes to upload action of the controller ?

思路: 基本上,因为许多用户可能会同时上传,所以使用单例作用域不是一个好主意,因此我想我的选择应该是原型或请求.那么,其中哪一个效果很好,并且只有在仅访问PhotoService时才创建哪个?

Thoughts: Basically since many users might upload at the same time, It's not a good idea to use singleton scope, so my options would be prototype or request I guess. So which one of them works well and also which one only gets created when the PhotoService is accessed only ?

我正在尝试最大程度地减少注入到应用程序上下文中的服务的数量,并且只要该Web应用程序处于活动状态就一直存在,我基本上希望该服务实例在Web应用程序生命周期中的某个时刻死掉或进行垃圾收集时间,而不是在没有用的时候在内存中闲逛.我当时正在考虑将其设置为会话范围,以便在用户会话终止时也可以清理该服务,但是在某些情况下,用户可能不想上传任何照片,并且无缘无故创建了该服务.

I'm trying to minimize the number of services being injected into the application context and stays as long as the web app is alive, basically I want the service instance to die or get garbage collect at some point during the web app life time rather than hanging around in the memory while there is no use for it. I was thinking about making it session scope so when the user's session is terminated the service is cleaned up too, but in some cases a user might not want to upload any photo and the service gets created for no reason.

P.S:如果我在"upload()"中移动"def photoService",是否仅在调用上载请求时才注入"def photoService"?我认为这可能会引发异常,因为在Spring注入服务之前会有延迟,然后对def photoService的引用将为n

P.S: If I move the "def photoService" within the upload(), does that make it only get injected when the request to upload is invoked ? I assume that might throw exception because there would be a delay until Spring injects the service and then the ref to def photoService would be n

推荐答案

我发现Singleton范围很好,因为我没有为每个请求/用户维护状态.仅当服务应该保持状态时,我们才能继续使用原型或其他合适的作用域.如果您认为单例可能会导致意外的行为,但是使用原型进行测试,则使用原型更为安全.

I figured out that Singleton scope would be fine since I'm not maintaining the state for each request/user. Only if the service is supposed to maintain state, then we can go ahead and use prototype or other suitable scopes. Using prototype is safer if you think the singleton might cause unexpected behavior but that is left to testing.

这篇关于使用Grails Services上传照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆