Results 1 to 5 of 5

Thread: Singleton in-memory lookup and web

  1. #1
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    13

    Default Singleton in-memory lookup and web

    Hi there,

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Geneva (Switzerland)
    Posts
    304

    Default

    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 ...

  3. #3
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    13

    Default

    Hi,

    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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    13

    Smile

    Quote Originally Posted by mdeinum View Post
    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...

    Thanks! That points me in the right direction - I will go read up a bit more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •