• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best way to instantiate Spring context in a Servlet ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to instantiate a Spring applicationcontext in a Servlet. Is there any best way to do this ?

Also,

If the same ApplicationConext needs to be shared among many servlet, what would be the best way ?

Thanks and Regards,
Abhijit Salunkhe
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi Sal wrote:Hi,

I need to instantiate a Spring applicationcontext in a Servlet. Is there any best way to do this ?

Also,

If the same ApplicationConext needs to be shared among many servlet, what would be the best way ?

Thanks and Regards,
Abhijit Salunkhe



Best way is to use the ContextLoaderListener in your web.xml This will create an ApplicationContext for you when you deploy you war. This app context will be placed in your ServletContext for that app.

Then in your Servlets init method use

ApplicationContext context = ServletContextUtils.getRequiredWebApplicationContext(servletContext);

Now that code is not 100% compilable, you have to fill in the servletContext variable and my method call might be slightly wrong, but your IDE will tell you what methods are on the ServletContextUtils class.

Mark
reply
    Bookmark Topic Watch Topic
  • New Topic