If I make a Grails controller a singleton via:
static scope = "singleton"
... how does Grails expose a params
variable to my controller's actions, where the params
are request specific?
I would understand if params
were passed into my action methods as a variable, but here params
are just available and in-scope (and different for simultaneous requests, despite the fact that there's only one instance of my controller).
How is this implemented under the hood?
Each request gets its own thread, so the request, response, params, session, etc. are available independent of whether the controller is a singleton or created new for every request. It would be different if these variables were fields in the class, but they're not.
Under the hood this is implemented by an AST transform that mixes in org.codehaus.groovy.grails.plugins.web.api.ControllersApi
into the controller classes, which adds methods such as getParams()
(which you can use as the params
property). These call RequestContextHolder.currentRequestAttributes()
to get the thread-local information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With