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

标签 spring grails request

RequestContextHolder.currentRequestAttributes().request对性能有影响吗?我知道在服务方法内部访问请求不是很好,但我确实需要它。因此,如果我每个请求调用RequestContextHolder.currentRequestAttributes().request 20-30次,是否会降低性能?

最佳答案

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

class MyService{

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

}

并从 Controller
class MyController{

def myService

def index = { 

   myService.method(request)

}

}

关于spring - 可以在生产中使用RequestContextHolder.currentRequestAttributes()。request吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835972/

相关文章:

grails - 如何使第一列在一对多关系联接表中为空?

python - 在 Python 中获取数组作为 GET 查询参数

java - Spring Security - 身份验证的用户名在 AuthenticationSuccessHandler 中为空

java - 在 JPA 查询中包含枚举

jquery - 用于Grails中javascript的GSP片段替代

grails - 如何从同一插件中的 src 文件访问我的 Grails 插件中的文件?

javascript - Node - Request.js ECONNRESET 错误仅在服务器上

javascript - 验证 2 个条件时执行函数 : ajax response and timer

java - Spring MVC 中整数(最终类)的 CGLib 代理

spring - 使用 maven(不是 gradle)生成带有 spring rest docs 的 asciidoctor doc