This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)


<bean id="userSessionCache" class=""/>
// test the spring framework

WebApplicationContext webAppCtxt = WebApplicationContextUtils.getWebApplicationContext(event.getSession()
private UserSessionCache userSessionCache;
userSessionCache = (UserSessionCache) webAppCtxt.getBean(“User”); I am getting null pointer exception in this line.
ERROR [SessionListener] Exception Occured in Session Listener

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

Mark Spritzler
Posts: 17309
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!