Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContext

    Hi All,

    I hope my problem has been solved on other thread. please guide to the thread. I had problem on retrieve spring applicationContext. I loaded the applicationContext in web.xml:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/conf/applicationContext.xml,
    /WEB-INF/conf/gk_applicationContext.xml
    </param-value>
    </context-param>


    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListe ner
    </listener-class>
    </listener>


    the spring context aboves are loaded properly. Now, I need to access certain beans in my pojo classes and I tried to use the following utils:
    WebApplicationContextUtils.getWebApplicationContex t(ServletContext)

    but at this stage, i don't have servletcontext object... Is there anyway to access the bean? of course, I can do injection to my pojo. but if i can get spring context, then it'll be easy to get other different bean.

    look forward to hear from you..


    Tony
    Utilisoft Pty Ltd

  • #2
    The best approach is to wire everything together in your application context, so you do not need to access the context afterwards explicitly.

    If that is not feasible I recommend setting up a ServletContextListener (after the ContextLoaderListener) to access the context and store it in a well known location (e.g. a static variable) to be accessed later on.

    Regards,
    Andreas

    Comment


    • #3
      Hi Andreas,

      Thanks for quick reply ...

      Is it possible to give me an sample of ServletContextListener code?

      Thank you.


      Cheer,

      Tony
      Utilisoft Pty Ltd

      Comment


      • #4
        Originally posted by cormet View Post
        Is it possible to give me an sample of ServletContextListener code?
        It could be like that (note: this is just written down and not tested):
        Code:
        public class ContextStroreListener implements ServletContextListener {
            public void contextInitialized(ServletContextEvent evt) {
                ServletContext servletCtx = evt.getServletContext();
                ApplicationContext ctx = WebApplicationUtils.getWebApplicationContext(servletCtx);
        
                // store the context somewhere
            }
        
            public void contextDestroyed(ServletContextEvent evt) {}
        }
        Regards,
        Andreas

        Comment


        • #5
          Thanks...problem solved ...

          Cheers,

          -Tony
          Utilisoft Pty Ltd

          Comment

          Working...
          X