Results 1 to 5 of 5

Thread: Finding applicationContext.xml

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    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

  3. #3

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  5. #5
    Join Date
    Jul 2005
    Location
    Austria
    Posts
    105

    Default

    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

Similar Threads

  1. Replies: 6
    Last Post: Mar 29th, 2005, 12:25 PM
  2. Replies: 6
    Last Post: Oct 31st, 2004, 10:58 AM

Posting Permissions

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