File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 17249
    
    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
 
Consider Paul's rocket mass heater.
 
subject: Best way to instantiate Spring context in a Servlet ?
 
Similar Threads
Select in Spring
Servlet's constructor
what is the Best Frameworks combination Spring-Struts-Hibernate, or JDO, or etc...
Any books on Spring and iBatis?
Servlets and Swing