We have an interceptor that checks for an allowed (mobile) user-agent. We're using an internal database for recognizing them. So I've setup an interceptor that first recognizes the user-agent and creates a Phone object keeping all its capabilities for later or redirect to an error page if we don't like the agent. The interceptor's preHandle
method accepts a handler object which casts to the controller that handles our request.
I'd like to set the (request dependent) Phone object on that controller so it can reuse it in our business logic. Unfortunately the controller is (and afaik) should be a singleton and must not carry state of an explicit request. But if it's prohibited how to I communicate between interceptor and controller (why does the interceptor get an instance of it anyway)? I don't want to instantiate the Phone object twice.
As you might have guessed the Phone object is not the only problem. We're also wrapping the interpreted request information and set it on the controller so all the information acquired by the interceptor can be reused later. But what happens if two clients concurrently use the controller? I first thought that the servlet engine (weblogic and tomcat for development) handles that issue for us but as it seems the DispatcherServlet
itself is singleton.
Our current solution is to use prototype scoped controllers (annotate with @Scope(value="prototype")
or declare in spring.xml). So for every request the DispatcherServlet
will create another controller instance. Besides the theory break I'm afraid that this could have impact on the system's performance (when we're deploying on the large scale) since the controllers will be autowired with quite some objects (that itself most luckily are singletons).