可以在生产中使用RequestContextHolder.currentRequestAttributes()。request吗?

时间:2012-12-12 08:50:57

标签: spring grails request

RequestContextHolder.currentRequestAttributes().request会对性能产生任何影响吗?我知道在服务方法中访问请求并不好,但我确实需要它。因此,如果我每次请求拨打RequestContextHolder.currentRequestAttributes().request 20-30次会降低性能吗?

1 个答案:

答案 0 :(得分:1)

问题与性能无关。 Ipotetically可以在请求之外调用服务方法(例如在石英预定作业中)。在这种情况下,RequestContextHolder.currentRequestAttributes()。request可能会抛出异常。我认为最好的方法是将请求作为参数传递给需要它的服务方法。

class MyService{

def method(def request){
    //do what you want with the request
}

}

来自控制器

class MyController{

def myService

def index = { 

   myService.method(request)

}

}