aspose file tools*
The moose likes Spring and the fly likes Best way to instantiate Spring context in a Servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Best way to instantiate Spring context in a Servlet ?" Watch "Best way to instantiate Spring context in a Servlet ?" New topic
Author

Best way to instantiate Spring context in a Servlet ?

Abhi Sal
Greenhorn

Joined: Dec 21, 2009
Posts: 4
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to instantiate Spring context in a Servlet ?