Announcement Announcement Module
No announcement yet.
Discovering the location of applicationContext.xml Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Discovering the location of applicationContext.xml


    I have problem with one of my tests. I'm testing an upload DAO that talks to the file system, and stores all the files it gets in a certain folder, which is always /webapp/uploads. I know that the location of applicationContext.xml will always be /webapp/WEB-INF/applicationContext.xml. So my question is, is there any way to "discover" the location of the applicationContext.xml file, so I can use it to construct the path of my uploads folder?

    When my webapp is deployed I can use the Servlet container for this, but I'm testing my classes outside of the containter. The getConfigLocations method that's part of the ApplicationContext family of classes seemed perfect, but it's protected and I don't want to do any custom extension of the framework.

    Does anyone have any ideas? Cause now I've got the path for the folder hardcoded into my app, and that's just ugly.


  • #2
    If you put the applicationContext.xml in WEB-INF/classes instead you could use ClassPathResource.getFile() in both the application and the tests, as long as you could run the application from an exploded war (which surely must be the case if you are relying on a file system for your uploads). Would that work for you? If so then just identify your config file as classpath:applicationContext.xml. If not I guess you can use Resource.getFile() generically, but then how is the test going to locate the file, unless you hard wire the location anyway?


    • #3
      problem with applicationContext.xml path

      Can any one tell me, where should I place my applicationContext.xml
      in my tomcat.

      Actually I don't want this file get it from servletContext because, I have a generic ServiceLocator class which creates the applicationContext object. Which may not have reference to servletContext always.

      Ideally I want to put this applicationContext.xml in tomcat's "WEB-INF\config" folder and load it.

      I have tried ClassPathXmlApplicationContext("config\application Context.xml") this is giving an error saying cannot locate the file . FileIO error

      I have tried FileSystemXmlApplicationContext("config\applicatio nContext.xml") this is giving an error saying cannot locate the file . FileIO error because it is looking at tomcat/bin directory.

      can anyone please tell me if how to resolve this.


      • #4
        In general you should use ContextLoaderListener, and shouldn't need to instantiate the context yourself.

        You need to use
        /WEB-INF/config/application Context.xml
        The path is resolved relative to the root of the WAR.


        • #5
          Hi Rod Johnson,

          Glad to see your reply. :o

          Now I have changed my code to use Listner.

          I have another question here, Is it a good idea to have a serviceLocator class to locate ApplicationContext and cache the bean got from beanFactory.

          Would there be any thread safty issue?

          Or with spring frame work, no need for a ServiceLocator class?

          Thanks in advance