Results 1 to 5 of 5

Thread: ApplicationContext

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  3. #3

    Default

    Hi Andreas,

    Thanks for quick reply ...

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

    Thank you.


    Cheer,

    Tony
    Utilisoft Pty Ltd

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

  5. #5

    Default

    Thanks...problem solved ...

    Cheers,

    -Tony
    Utilisoft Pty Ltd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •