This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring MVC-How to get WebApplicationContext in Spring without ContextLoaderListener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring MVC-How to get WebApplicationContext in Spring without ContextLoaderListener?" Watch "Spring MVC-How to get WebApplicationContext in Spring without ContextLoaderListener?" New topic
Author

Spring MVC-How to get WebApplicationContext in Spring without ContextLoaderListener?

selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Dear Guys,

To my understanding, in order to get a bean, we should get the application context and call getBean. In the servlet layer it is easy, all we need to do is

In web.xml file while configuration ContextLoaderListener its worki ng fine. But I have removed ContextLoaderListener in web.xml getting null pointer in exception (userSessionCache = (UserSessionCache) webAppCtxt.getBean(“User”); in this line.)

cache.xml:

<bean id="userSessionCache" class="com.xxx.cache.UserSessionCache"/>
// test the spring framework

sessionListener.java

WebApplicationContext webAppCtxt = WebApplicationContextUtils.getWebApplicationContext(event.getSession()
.getServletContext());
private UserSessionCache userSessionCache;
userSessionCache = (UserSessionCache) webAppCtxt.getBean(“User”); I am getting null pointer exception in this line.
ERROR [SessionListener] Exception Occured in Session Listener
java.lang.NullPointerException
at com.xxx.handler.SessionListener.sessionDestroyed(SessionListener.java:61)


Is it possible to get the “WebApplicationContext” in spring mvc without ContextLoaderListener?

Thanks,
SR
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

First if I was using Spring MVC, I would just have the beans auto-injected into the Controller classes. I wouldn't want to call WebApplicationContextUtils.getWebApplicationContext(getServletContext());

I can do




Much simpler, but you will need the ContextLoaderListener no matter what to create the ApplicationContext for your middle tier, when the web app gets deployed. Without it you will not get your Services etc created and put into an ApplicationContext that is placed in the ServletContext. Without any ApplicationContext in the ServletContext that line WebApplicationContextUtils.getWebApplicationContext(getServletContext()); will just return NULL.

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: Spring MVC-How to get WebApplicationContext in Spring without ContextLoaderListener?
 
Similar Threads
No WebApplicationContext found: no ContextLoaderListener registered?
Relation between ContextLoaderListener application context and WebApplicationContext
How to register ContextLoaderListener ?
Servlet
JSF and Spring