Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Grails make `params` available to a Singleton controller? (Grails internals)

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?

like image 235
Bosh Avatar asked Feb 15 '23 10:02

Bosh


1 Answers

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.

like image 128
Burt Beckwith Avatar answered Feb 17 '23 23:02

Burt Beckwith