Jan 17th, 2008, 12:28 AM
Singleton in-memory lookup and web
I'm quite new to the Spring framework. I have a situation where I have a Spring application hosted in Tomcat. In the service layer I need a singleton which contains an in-memory lookup table. I will have web service (CXF) requests coming in which will need to access that lookup.
I know I can mark the bean as singleton, but will Tomcat start a different ApplicationContext to service each new request?
Can you guys recommend a different strategy for achieving something like this? The in-memory lookup will be a type of write-back cache, so I really need only one instance.
Thank you in advance for any assistance
Jan 17th, 2008, 02:11 AM
By default the Spring beans are singleton. So you can just declare that bean in your application context and inject it in the needed services. Of course, that singleton will have to be threadsafe ...
Tell us if you need a more precise description ...
Jan 17th, 2008, 03:01 AM
Thanks. I just need to understand the way that a web container like TomCat instantiates a Spring application context. Is it per unique web service request?
Jan 17th, 2008, 03:32 AM
It depends on your configuration/code
If you use the ContextLoaderListener to load your context there will be one context (the root context). For each DispatcherServlet also an applicationcontext instantiated at startup.
Now if you have in your code something like new ApplicationContext(yourfiles) then you have a problem and indeed for each request an ApplicationContext will be instantiated.
However that is all explained in the reference guide of spring...
Jan 17th, 2008, 03:43 AM
Originally Posted by mdeinum
Thanks! That points me in the right direction - I will go read up a bit more.