Announcement Announcement Module
Collapse
No announcement yet.
MVC-How to get Spring WebApplicationContext in Spring without ContextLoaderListener? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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.getWebApplicationContex t(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(S essionListener.java:61)


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

    Thanks,
    SR
    Last edited by selvapa; Nov 10th, 2010, 12:26 AM.

  • #2
    No, it is not possible to get the root WebApplicationContext without using ContextLoaderListener, since the ContextLoaderListener loads the root WebApplicationContext and puts it in the ServletContext where you can then look it up from any Servlet or Filter.

    A WebApplicationContext isn't just going to create itself, obviously, you're responsible for creating it. If you have defined a Spring MVC DispatcherServlet in web.xml, it will also create its own local WebApplicationContext so it is possible to rely on that one only. However, you'll find that some frameworks, such as Spring Security, require a root WebApplicationContext bound to the ServletContext with the ContextLoaderListener to function.

    Keith
    Last edited by Keith Donald; Nov 10th, 2010, 07:37 AM.

    Comment


    • #3
      Originally posted by Keith Donald View Post
      No, it is not possible to get the root WebApplicationContext without using ContextLoaderListener, since the ContextLoaderListener loads the root WebApplicationContext and puts it in the ServletContext where you can then look it up from any Servlet or Filter.

      A WebApplicationContext isn't just going to create itself, obviously, you're responsible for creating it. If you have defined a Spring MVC DispatcherServlet in web.xml, it will also create its own local WebApplicationContext so it is possible to rely on that one only. However, you'll find that some frameworks, such as Spring Security, require a root WebApplicationContext bound to the ServletContext with the ContextLoaderListener to function.

      Keith
      Thank you so much keith...Thanks lot....

      Comment

      Working...
      X