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

  • Finding applicationContext.xml

    Hi,

    We are using Spring in our backend code, but not our web (Struts), although it all runs within Tomcat. I am using org.springframework.web.context.ContextLoaderListe ner to configure everything when the app starts up, and the applicationContext.xml is located in the WEB-INF directory.

    I need to to load the BeanFactory in my code, but don't want to hardcode the location of the applicationContext.xml so I can run the backend code outside the container.

    Apparently the WEB-INF directory is not in the classpath - any suggestions on how to find the applicationContext.xml file?

    thanks!

    David

  • #2
    Why do you need to load the BeanFactory in your code? Can you post some code of what you're trying to do - with your desired results and why it's not working?

    Matt

    Comment


    • #3
      Related to the scripting question I posted - we need the scripts to be able to access the beans managed by Spring, so I have a ObjectLocator class that loads the BeanFactory and returns the object from a GetObject() call.

      Everything is working, but I have to hard-code the location of the applicationContext.xml which I want to avoid.

      Any suggestions?

      thanks,

      David

      Comment


      • #4
        David,

        I'm not 100% clear on what you want to do, but I can think of two techniques that may be useful:

        1.WebApplicationContextUtils.getRequiredWebApplica tionContext(ServletContext) if you are in the web tier
        2. Make any object that needs to get at the ApplicationContext implement ApplicationContextAware, if it's managed by Spring itself.

        Either of these is independent of the context location. If you explicitly load the context again by location, you will have two copies of all your "singletons," also.

        Rgds
        Rod

        Comment


        • #5
          We've the same problem. I set up the back end with Spring, which runs in a Servlet Container (Tomcat). To load the context I do this:
          Code:
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/conf/myComponent-servlet.xml</param-value>
          </context-param>
          
          <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          The servlet looks like this:
          Code:
              public void init(ServletConfig servletConfig) throws ServletException {
                  super.init(servletConfig);
                  ApplicationContext appContext = WebApplicationContextUtils
                          .getRequiredWebApplicationContext(servletConfig.getServletContext());
                  exchanger = (ICWExchanger) appContext.getBean("cwExchanger");
                  webServices = (IWebServices)appContext.getBean("webServicesCW");
                  //Don't know if this is a good solution
                  ExchangerFactory.setApplicationContext(appContext);
              }
          As hay7777 already mentioned, he want to access Spring managed beans in Scripts. We work with Beanshell. My workaround is the ExchangerFactory, which gets the application Context.
          Code:
          public class ExchangerFactory {
              private static ApplicationContext appContext;
              public static void setApplicationContext(ApplicationContext ctx){
                  appContext = ctx;
              }
              
              public static Object getService(String service){
                  return appContext.getBean(service);   
              }
          The Exchanger has a static method to access the Context and returns the required Bean. If I use a Spring managed bean in the script I call:
          Code:
          Exchanger.getService("myService").
          This method delegates to the appContext and then again gets the required object. I think this way is similar to the one of hay7777?

          Is it the best way?

          markus

          Comment

          Working...
          X